XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 居中
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);//垂直
int currnetRow = 5;//开始查找的行
for (int p = 5; p < totalRow; p++) {//totalRow 总行数
XSSFCell currentCell = sheet.getRow(p).getCell(0);
String current = getStringCellValue(currentCell);
log.info(" current = "+current);
XSSFCell nextCell = null;
String next = "";
if(p < totalRow+1){
XSSFRow nowRow = sheet.getRow(p+1);
if(nowRow != null){
nextCell = nowRow.getCell(0);
next = getStringCellValue(nextCell);
}else{
next = "";
}
}else{
next = "";
}
log.info(" next = "+next);
if(current.equals(next)){//比对是否相同
currentCell.setCellValue("");
continue;
}
else{
sheet.addMergedRegion(new CellRangeAddress(currnetRow, p, 0, 0));//合并单元格
XSSFCell nowCell = sheet.getRow(currnetRow).getCell(0);
nowCell.setCellValue(current);
nowCell.setCellStyle(cellStyle);
currnetRow = p + 1;
}
}
private String getStringCellValue(XSSFCell cell) {
String strCell = "";
if (cell != null) {
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_STRING:
strCell = cell.getStringCellValue();
break;
case XSSFCell.CELL_TYPE_NUMERIC:
strCell = String.valueOf(cell.getNumericCellValue());
break;
case XSSFCell.CELL_TYPE_BOOLEAN:
strCell = String.valueOf(cell.getBooleanCellValue());
break;
case XSSFCell.CELL_TYPE_BLANK:
strCell = "";
break;
default:
strCell = "";
break;
}
if (strCell.equals("") || strCell == null) {
return "";
}
if (cell == null) {
return "";
}
}
return strCell;
}
分享到:
相关推荐
java POI导出Excel文件多表头文件
poi获取合并单元格,Java 对excel 文档处理。
java excel poi合并单元格
利用poi进行excel相同内容合并单元格,只摘要了传入部分功能,请关注addMergedRegion方法
poi获取exccel有合并单元格的数据,存入List
POI操作Excel 合并单元格 边框问题解决方法,这个方法是我亲自测试,并运用于项目中的,可以放心使用,还一并写出了很多注释,其中有写poi的bug的解决方法
poi生成excel及合并单元格示例,详见文章http://blog.csdn.net/geloin/article/details/17219885
poi读取合并单元格帮助类
poi导入/导出Excel表格,合并单元格的读取和设置
POI 合并单元格 及 注意事项
easyuiPoi 通过模板导出Excel、支持纵向合并单元格(合并重复内容)。压缩包包含公共工具类、示例模板、调用截图。
关于java EXCEL poi 合并后无法显示或无法设置边框问题 【本人亲测】 excel poi 新手! 坑了1.5天!
相同格式的excel进行合并,或者对多个word文档进行合并,word文档包含图片。
Java Poi 导出excel(支持各种设置字体、颜色、垂直居中)
java poi 填充word(合并单元格,添加图片,设置字号)(csdn)————程序
POI导出Excel工具类,自动设置标题 列名 文件名,可插入图片,合并单元格
【JAVA】easypoi根据Excel模板导出 循环遍历合并单元格处理
一般在导出Excel中,在创建工作表sheet后,初始化的sheet中row都是一行一行的,每个单元格都是最小单位,所以问题1什么时候将单元格合并? 方案:先将数据组装成json格式方便插入Excel时进行遍历,接着以巡检项为每...
POI excel单元格顔色设定
Java操纵POI解析Excel表,根据表头将内容储存。通过实体类set、get方便其它方法引用