chen_123yang
chen_123yang
2012-12-11 16:08
浏览 409
已采纳

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));

 

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • u011606457
    _1_1_7_ 2012-12-12 10:07
    已采纳

    简单测试了一下没有问题:
    [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]
    是不是你把参数的行号、列号搞错了

    点赞 评论

相关推荐