POI 兼并单元格ITeye - 威尼斯人

POI 兼并单元格ITeye

2019年04月03日12时44分42秒 | 作者: 昊嘉 | 标签: 单元格,兼并,设置 | 浏览: 847

import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExportUtil { private XSSFWorkbook wb = null; private XSSFSheet sheet = null; * @param wb * @param sheet public ExportUtil(XSSFWorkbook wb, XSSFSheet sheet) { this.wb = wb; this.sheet = sheet; * 兼并单元格后给兼并后的单元格加边框 * @param region * @param cs public void setRegionStyle(CellRangeAddress region, XSSFCellStyle cs) { int toprowNum = region.getFirstRow(); for (int i = toprowNum; i = region.getLastRow(); i++) { XSSFRow row = sheet.getRow(i); for (int j = region.getFirstColumn(); j = region.getLastColumn(); j++) { XSSFCell cell = row.getCell(j);// XSSFCellUtil.getCell(row, // (short) j); cell.setCellStyle(cs); * 设置表头的单元格款式 * @return public XSSFCellStyle getHeadStyle() { // 创立单元格款式 XSSFCellStyle cellStyle = wb.createCellStyle(); // 设置单元格的布景色彩为淡蓝色 cellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index); cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); // 设置单元格居中对齐 cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 设置单元格笔直居中对齐 cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); // 创立单元格内容显现不下时主动换行 cellStyle.setWrapText(true); // 设置单元格字体款式 XSSFFont font = wb.createFont(); // 设置字体加粗 font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); font.setFontName("宋体"); font.setFontHeight((short) 200); cellStyle.setFont(font); // 设置单元格边框为细线条 cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN); cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN); cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN); return cellStyle; * 设置表体的单元格款式 * @return public XSSFCellStyle getBodyStyle() { // 创立单元格款式 XSSFCellStyle cellStyle = wb.createCellStyle(); // 设置单元格居中对齐 cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 设置单元格笔直居中对齐 cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); // 创立单元格内容显现不下时主动换行 cellStyle.setWrapText(true); // 设置单元格字体款式 XSSFFont font = wb.createFont(); // 设置字体加粗 // font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL); font.setFontName("宋体"); font.setFontHeight((short) 200); cellStyle.setFont(font); // 设置单元格边框为细线条 cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN); cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN); cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN); return cellStyle; * @param sheet * @param colIndex 兼并的第几列 (下标从0开端) * @param startRowIndex 从第几行开端兼并(算上标题,从0开端算) * @param endRowIndex 从第几行完毕兼并 public Map String,String mergerData(XSSFSheet sheet,int colIndex, int startRowIndex , int endRowIndex) { Map String,String map = new HashMap (); breakFor: for (int i = startRowIndex; i = endRowIndex; i++) { Cell cell = sheet.getRow(i).getCell(colIndex); for (int j = i + 1; j = endRowIndex; j++) { Cell celltemp = sheet.getRow(j).getCell(colIndex); // 假如下一行与被比较行持平,则持续该循环,直到不等才跳出 if (!celltemp.getStringCellValue().equals(cell.getStringCellValue())) { int temp = j-1; if (temp i) { // 兼并单元格 map.put(cell.getStringCellValue(), i+1+","+j); sheet.addMergedRegion(new CellRangeAddress(i, temp, colIndex, colIndex)); i = temp; break; if (j endRowIndex) { map.put(cell.getStringCellValue(), i+1+","+j); sheet.addMergedRegion(new CellRangeAddress(i, endRowIndex, colIndex, colIndex)); break breakFor; return map ;

 

pom.xml 引进包:

 

 ! poi 导出Excel包  
 dependency 
 groupId org.apache.poi /groupId 
 artifactId poi-ooxml /artifactId 
 version 3.14 /version 
 /dependency 

 

School类字段 ,参阅代码就不逐个贴出来了;

 

参阅文章:

http://bbs.csdn.net/topics/390420021

http://blog.csdn.net/hehexiaoyou/article/details/37873131

 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表威尼斯人立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章