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

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条回答 默认 最新

  • _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]
    是不是你把参数的行号、列号搞错了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF动态创建页面内容
  • ¥15 如何对TBSS的结果进行统计学的分析已完成置换检验,如何在最终的TBSS输出结果提取除具体值及如何做进一步相关性分析
  • ¥15 SQL数据库操作问题
  • ¥100 关于lm339比较电路出现的问题
  • ¥15 Matlab安装yalmip和cplex功能安装失败
  • ¥15 加装宝马安卓中控改变开机画面
  • ¥15 STK安装问题问问大家,这种情况应该怎么办
  • ¥15 关于罗技鼠标宏lua文件的问题
  • ¥15 halcon ocr mlp 识别问题
  • ¥15 已知曲线满足正余弦函数,根据其峰值,还原出整条曲线