poi3.8 操作excel2007合并单元格没效果

/**定性清单表头
  * @author 杨松涛
  */
 private void dxqdHeadLine(Sheet sheet,XSSFWorkbook wb) {
  Row header = sheet.createRow(0);//第一行  
//  //第一行内容
  createCell(wb,header,0,"基础字段");createCell(wb,header,27,"回访环节情况");createCell(wb,header,32,"预处理情况");
  createCell(wb,header,40,"处理环节情况");createCell(wb,header,48,"定性审核环节");createCell(wb,header,56,"定性复核环节");
  createCell(wb,header,64,"分公司细化定责环节");createCell(wb,header,69,"原始服务产品类型");createCell(wb,header,71,"最终定性结果");
  createCell(wb,header,82,"客户属性");createCell(wb,header,89,"关联省公司考核班组");createCell(wb,header,90,"关联分公司考核班组");
  
  sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0,0,0,26));
  sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0,27,0,31));
  sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0,32,0,39));
  sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0,40,0,47));
  sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0,48,0,55));
  sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0,56,0,63));
  sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0,64,0,68));
  sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0,69,0,70));
  sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0,71,0,81));
  sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0,82,0,88));
  sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0,89,1,89));
  sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0,90,1,90));

 

只有第一个,"基础字段"合并了有效果,其他后面的字段都没合并

poi

1个回答

简单测试了一下没有问题:
[code="java"]
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class TestMerge {
public void test()throws IOException {
XSSFWorkbook wb=new XSSFWorkbook();
Sheet sheet=wb.createSheet();
dxqdHeadLine(sheet, wb);
wb.write(new FileOutputStream("d:/1.xlsx"));
}

private void dxqdHeadLine(Sheet sheet, XSSFWorkbook wb) {
    Row header = sheet.createRow(0);// 第一行
    // //第一行内容
    createCell(wb, header, 0, "基础字段");
    createCell(wb, header, 27, "回访环节情况");
    createCell(wb, header, 32, "预处理情况");
    createCell(wb, header, 40, "处理环节情况");
    createCell(wb, header, 48, "定性审核环节");
    createCell(wb, header, 56, "定性复核环节");
    createCell(wb, header, 64, "分公司细化定责环节");
    createCell(wb, header, 69, "原始服务产品类型");
    createCell(wb, header, 71, "最终定性结果");
    createCell(wb, header, 82, "客户属性");
    createCell(wb, header, 89, "关联省公司考核班组");
    createCell(wb, header, 90, "关联分公司考核班组");

    sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0, 0, 0, 26));
    sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0, 27, 0, 31));
    sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0, 32, 0, 39));
    sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0, 40, 0, 47));
    sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0, 48, 0, 55));
    sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0, 56, 0, 63));
    sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0, 64, 0, 68));
    sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0, 69, 0, 70));
    sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0, 71, 0, 81));
    sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0, 82, 0, 88));
    sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0, 89, 1, 89));
    sheet.addMergedRegion(ExcelTool.getCellRangeAddress(0, 90, 1, 90));
}

private void createCell(XSSFWorkbook wb, Row header, int column, String text) {
    Cell cell=header.createCell(column);
    cell.setCellValue(text);
}

public static void main(String[] args)throws Exception {
    new TestMerge().test();
}

}[/code]

[code="java"]import org.apache.poi.ss.util.CellRangeAddress;

public class ExcelTool {

public static CellRangeAddress getCellRangeAddress(int firstRow,
        int firstCol, int lastRow, int lastCol) {
    return new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
}

}[/code]
是不是你把参数的行号、列号搞错了

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问