关于POI导出EXCEL(单元格操作 行列操作)

传统项目多会有关于到处excel的操作 而到处的excel总体来看分两种

  一种就是简单结构 表头 行列分明

  还有一种稍微复杂些带单元格合并以及行列的增加删除等

第一种很容易找到例子 简单说下第二种情况

首先jar包

一、maven:

     <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-examples</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-excelant</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.9</version>
        </dependency>

二、模版

  通常这种操作都会有一个通用模版 而这个模版就是我们操作的源头

如下图:(为了省事直接用的项目的模版 就做了下处理 大家看结构就好)

  

有了模版 正常情况我们只需要读出模版 新建一个文件写入模版写入数据即可 但是这么做的话会有一个小问题 那就是如果有增加删除行的时候会使行的高度还原 样式还原

这样的话模版也就没有了存在的意义 故而 这个操作需要一个前置工作——修改模版

改完后的模版如下图:

没错 我们把合并列的单元格拆开了 这样就有效规避了这个问题的存在 (当然如果还有的话那你只能用代码控制了)

代码:

因为水平有限代码写的比较臃肿 见谅.

@SuppressWarnings("deprecation")
    public static void excel(String xlsFile, String outXlsFile, TOrderExcel Tore, int rows) throws FileNotFoundException, IOException {

        int i = 0;
        ArrayList<TOrderSell> arr = new ArrayList<TOrderSell>();
        String filename = xlsFile;
        XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(filename));
        XSSFSheet sheet = workbook.getSheetAt(0);
        XSSFRow row = null, row1 = null;
        XSSFCell cell, cell1;
     // 读取模版
for (int icount = 1; icount < 38; icount++) { row = sheet.getRow(icount); cell = row.getCell((short) 2); System.out.println(cell); for (int j = 1; j < 10; j++) { cell1 = row.getCell((short) 1); System.out.println(cell1); if (cell.toString().equals(cell1.toString())) { getCell(row, 3).setCellValue(cell1.toString()); } if (icount==2 && j==3) { if (!Utility.isEmpty(Tore.getName())) { getCell(row, j).setCellValue(Tore.getName()); }else { getCell(row, j).setCellValue(""); } } if (icount==2 && j==5) { if (!Utility.isEmpty(Tore.getSex())) { getCell(row, j).setCellValue(Tore.getSex()); }else { getCell(row, j).setCellValue(""); } } if (icount==2 && j==7) { if (!Utility.isEmpty(Tore.getBrithday()+"")) { getCell(row, j).setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(Tore.getBrithday())); }else { getCell(row, j).setCellValue(""); } } if (icount==2 && j==9) { if (!Utility.isEmpty(Tore.getAaddress())) { getCell(row, j).setCellValue(Tore.getAcity()); }else { getCell(row, j).setCellValue(""); } } if (icount==3 && j==3) { if (!Utility.isEmpty(Tore.getEdu())) { System.out.println(getCell(row, 2).getRichStringCellValue()); getCell(row, j).setCellValue(Tore.getEdu()); }else { getCell(row, j).setCellValue(""); } } if (icount==3 && j==5) { if (!Utility.isEmpty(Tore.getMarry())) { getCell(row, j).setCellValue(Tore.getMarry()); }else { getCell(row, j).setCellValue(""); } } if (icount==3 && j==7) { if (!Utility.isEmpty(Tore.getHmarry())) { getCell(row, j).setCellValue(Tore.getHmarry()); }else { getCell(row, j).setCellValue(""); } } if (icount==3 && j==9) { if (!Utility.isEmpty(Tore.getIndustry())) { getCell(row, j).setCellValue(Tore.getIndustry()); }else { getCell(row, j).setCellValue(""); } } if (icount==4 && j==3) { if (!Utility.isEmpty(Tore.getCredit_industry())) { getCell(row, j).setCellValue(Tore.getCredit_industry()); }else { getCell(row, j).setCellValue(""); } } if (icount==5 && j==3) { if (!Utility.isEmpty(Tore.getXaddress())) { getCell(row, j).setCellValue(Tore.getXprovince()+Tore.getXcity()+Tore.getXarea()+Tore.getXaddress()); }else { getCell(row, j).setCellValue(""); } } if (icount==6 && j==3) { if (!Utility.isEmpty(Tore.getPro_type())) { String protype = ""; if ("1".equals(Tore.getPro_type())) protype = "一抵"; else protype = "二抵"; getCell(row, j).setCellValue(protype); }else { getCell(row, j).setCellValue(""); } } if (icount==6 && j==5) { if (!Utility.isEmpty(Tore.getHaddress())) { getCell(row, j).setCellValue(Tore.getHprovince()+Tore.getHcity()+Tore.getHarea()+Tore.getHaddress()); }else { getCell(row, j).setCellValue(""); } } if (icount==7 && j==3) { if (!Utility.isEmpty(Tore.getBuildyear())) { getCell(row, j).setCellValue(Tore.getBuildyear()+"年"); }else { getCell(row, j).setCellValue(""); } } if (icount==7 && j==5) { if (!Utility.isEmpty(Tore.getNums_floor()+"")) { getCell(row, j).setCellValue(Tore.getNums_floor()); }else { getCell(row, j).setCellValue(""); } } if (icount==7 && j==7) { if (!Utility.isEmpty(Tore.getFloor()+"")) { getCell(row, j).setCellValue(Tore.getFloor()); }else { getCell(row, j).setCellValue(""); } } if (icount==7 && j==9) { if (!Utility.isEmpty(Tore.getBuildarea()+"")) { getCell(row, j).setCellValue(Tore.getBuildarea()+"平方米"); }else { getCell(row, j).setCellValue(""); } } if (icount==8 && j==3) { if (!Utility.isEmpty(Tore.getMoney()+"")) { getCell(row, j).setCellValue(Tore.getMoney()+"万元"); }else { getCell(row, j).setCellValue(""); } } if (icount==8 && j==5) { if (!Utility.isEmpty(Tore.getTimes()+"")) { getCell(row, j).setCellValue(Tore.getTimes()+"个月"); }else { getCell(row, j).setCellValue(""); } } if (icount==8 && j==7) { if (!Utility.isEmpty(Tore.getHome_status())) { getCell(row, j).setCellValue(Tore.getEstate_status()); }else { getCell(row, j).setCellValue(""); } } if (icount==8 && j==9) { if (!Utility.isEmpty(Tore.getMoney_month()+"")) { getCell(row, j).setCellValue(Tore.getMoney_month()+"元"); }else { getCell(row, j).setCellValue(""); } } if (Tore.getPeoList().size()>0) { if (Tore.getPeoList().size()%2==1) { i = (Tore.getPeoList().size()+1)/2; }else { i = Tore.getPeoList().size()/2; } int w = Tore.getPeoList().size(); if (w==1) { if (icount==9 && j==3) { getCell(row, j).setCellValue(Tore.getPeoList().get(0).getName()); } if (icount==9 && j==5) { getCell(row, j).setCellValue(Tore.getPeoList().get(0).getTotal_of_proportion()+"%"); } }else if (w==2) { if (icount==9 && j==3) { getCell(row, j).setCellValue(Tore.getPeoList().get(0).getName()); } if (icount==9 && j==5) { getCell(row, j).setCellValue(Tore.getPeoList().get(0).getTotal_of_proportion()+"%"); } if (icount==9 && j==7) { getCell(row, j).setCellValue(Tore.getPeoList().get(1).getName()); } if (icount==9 && j==9) { getCell(row, j).setCellValue(Tore.getPeoList().get(1).getTotal_of_proportion()+"%"); } }else if (w==3) { if (icount==9 && j==3) { getCell(row, j).setCellValue(Tore.getPeoList().get(0).getName()); } if (icount==9 && j==5) { getCell(row, j).setCellValue(Tore.getPeoList().get(0).getTotal_of_proportion()+"%"); } if (icount==9 && j==7) { getCell(row, j).setCellValue(Tore.getPeoList().get(1).getName()); } if (icount==9 && j==9) { getCell(row, j).setCellValue(Tore.getPeoList().get(1).getTotal_of_proportion()+"%"); } if (icount==10 && j==3) { getCell(row, j).setCellValue(Tore.getPeoList().get(2).getName()); } if (icount==10 && j==5) { getCell(row, j).setCellValue(Tore.getPeoList().get(2).getTotal_of_proportion()+"%"); } }else if (w==4) { if (icount==9 && j==3) { getCell(row, j).setCellValue(Tore.getPeoList().get(0).getName()); } if (icount==9 && j==5) { getCell(row, j).setCellValue(Tore.getPeoList().get(0).getTotal_of_proportion()+"%"); } if (icount==9 && j==7) { getCell(row, j).setCellValue(Tore.getPeoList().get(1).getName()); } if (icount==9 && j==9) { getCell(row, j).setCellValue(Tore.getPeoList().get(1).getTotal_of_proportion()+"%"); } if (icount==10 && j==3) { getCell(row, j).setCellValue(Tore.getPeoList().get(2).getName()); } if (icount==10 && j==5) { getCell(row, j).setCellValue(Tore.getPeoList().get(2).getTotal_of_proportion()+"%"); } if (icount==10 && j==7) { getCell(row, j).setCellValue(Tore.getPeoList().get(3).getName()); } if (icount==10 && j==9) { getCell(row, j).setCellValue(Tore.getPeoList().get(3).getTotal_of_proportion()+"%"); } }else { if (icount==9 && j==3) { getCell(row, j).setCellValue(Tore.getPeoList().get(0).getName()); } if (icount==9 && j==5) { getCell(row, j).setCellValue(Tore.getPeoList().get(0).getTotal_of_proportion()+"%"); } if (icount==9 && j==7) { getCell(row, j).setCellValue(Tore.getPeoList().get(1).getName()); } if (icount==9 && j==9) { getCell(row, j).setCellValue(Tore.getPeoList().get(1).getTotal_of_proportion()+"%"); } if (icount==10 && j==3) { getCell(row, j).setCellValue(Tore.getPeoList().get(2).getName()); } if (icount==10 && j==5) { getCell(row, j).setCellValue(Tore.getPeoList().get(2).getTotal_of_proportion()+"%"); } if (icount==10 && j==7) { getCell(row, j).setCellValue(Tore.getPeoList().get(3).getName()); } if (icount==10 && j==9) { getCell(row, j).setCellValue(Tore.getPeoList().get(3).getTotal_of_proportion()+"%"); } if (icount==11 && j==3) { getCell(row, j).setCellValue(Tore.getPeoList().get(4).getName()); } if (icount==11 && j==5) { getCell(row, j).setCellValue(Tore.getPeoList().get(4).getTotal_of_proportion()+"%"); } } } if (Tore.getSellList().size()>0) { for (int r = 0,z = 0; r < Tore.getSellList().size(); r++) { if (icount==12+z && j==3) { if (!Utility.isEmpty(Tore.getSellList().get(r).getCompany()+"")&&"2".equals(Tore.getSellList().get(r).getFrom_type())) { getCell(row, j).setCellValue(Tore.getSellList().get(r).getCompany()); }else { getCell(row, j).setCellValue(""); } } if (icount==12+z && j==5) { if (!Utility.isEmpty(Tore.getSellList().get(r).getPrice()+"")&&"2".equals(Tore.getSellList().get(r).getFrom_type())) { getCell(row, j).setCellValue(Tore.getSellList().get(r).getPrice()+"元/平方米"); }else { getCell(row, j).setCellValue(""); } } if (icount==12+z && j==7) { if (!Utility.isEmpty(Tore.getSellList().get(r).getPrice_total()+"")&&"2".equals(Tore.getSellList().get(r).getFrom_type())) { getCell(row, j).setCellValue(Tore.getSellList().get(r).getPrice_total()+"万元"); z++; }else { getCell(row, j).setCellValue(""); } } // if (icount==13+r && j==3) { // if (!Utility.isEmpty(Tore.getSellList().get(r).getSecond_audit_money()+"")&&"2".equals(Tore.getSellList().get(r).getFrom_type())) { // getCell(row, j).setCellValue(Tore.getSellList().get(r).getPrice_guide()+"万元"); // }else { // getCell(row, j).setCellValue(""); // } // } // if (icount==13+r && j==5) { // if (!Utility.isEmpty(Tore.getSellList().get(r).getMortgage()+"")&&"2".equals(Tore.getSellList().get(r).getFrom_type())) { // getCell(row, j).setCellValue(Tore.getSellList().get(r).getMortgage()+"%"); // }else { // getCell(row, j).setCellValue(""); // } // } } } if (icount==22 && j==3) { if (!Utility.isEmpty(Tore.getMoney()+"")) { getCell(row, j).setCellValue(Tore.getMoney()+"万元"); }else { getCell(row, j).setCellValue(""); } } if (icount==22 && j==5) { if (!Utility.isEmpty(Tore.getMortgage()+"")) { getCell(row, j).setCellValue(Tore.getMortgage()+"%"); }else { getCell(row, j).setCellValue(""); } } if (icount==23 && j==3) { if (!Utility.isEmpty(Tore.getFirst_type_have())) { getCell(row, j).setCellValue(Tore.getFirst_type_have()); }else { getCell(row, j).setCellValue(""); } } if (icount==23 && j==5) { if (!Utility.isEmpty(Tore.getFirst_type_money()+"")) { getCell(row, j).setCellValue(Tore.getFirst_type_money()+"万元"); }else { getCell(row, j).setCellValue(""); } } if (icount==23 && j==7) { if (!Utility.isEmpty(Tore.getFirst_type_bank())) { getCell(row, j).setCellValue(Tore.getFirst_type_bank()); }else { getCell(row, j).setCellValue(""); } } if (icount==23 && j==9) { if (!Utility.isEmpty(Tore.getFirst_type_bank())) { getCell(row, j).setCellValue("一抵"); }else { getCell(row, j).setCellValue(""); } } if (icount==24 && j==3) { if (!Utility.isEmpty(Tore.getRun_flag())) { String flag = ""; if ("0".equals(Tore.getRun_flag())) { flag = "无"; }else { flag = "有"; } getCell(row, j).setCellValue(flag); }else { getCell(row, j).setCellValue(""); } } if (icount==24 && j==5) { String desc = ""; if (!Utility.isEmpty(Tore.getRun_desc())) { desc = "借款人:"+Tore.getRun_desc(); }else { desc = "无"; } getCell(row, j).setCellValue(desc); } if (icount==25 && j==3) { if (!Utility.isEmpty(Tore.getMrun_flag())) { String flag = ""; if ("0".equals(Tore.getMrun_flag())) { flag = "无"; }else { flag = "有"; } getCell(row, j).setCellValue(flag); }else { getCell(row, j).setCellValue(""); } } if (icount==25 && j==5) { String desc = ""; if (!Utility.isEmpty(Tore.getRun_desc())) { desc = "配偶:"+Tore.getMrun_desc(); }else { desc = "无"; } getCell(row, j).setCellValue(desc); } if (icount==26 && j==3) {//征信说明 if (!Utility.isEmpty(Tore.getCredit_desc())) { getCell(row, j).setCellValue(Tore.getCredit_desc()); }else { getCell(row, j).setCellValue("无"); } } if (icount==27 && j==3) { if (!Utility.isEmpty(Tore.getBef_org())) { getCell(row, j).setCellValue(Tore.getBef_org()); }else { getCell(row, j).setCellValue(""); } } if (icount==27 && j==5) { if (!Utility.isEmpty(Tore.getBef_money()+"")) { getCell(row, j).setCellValue(Tore.getBef_money()); }else { getCell(row, j).setCellValue(""); } } if (icount==27 && j==7) { if (!Utility.isEmpty(Tore.getBef_times()+"")) { getCell(row, j).setCellValue(Tore.getBef_times()); }else { getCell(row, j).setCellValue(""); } } if (icount==27 && j==9) { if (!Utility.isEmpty(Tore.getBef_end_time()+"")) { getCell(row, j).setCellValue(Tore.getBef_end_time()); }else { getCell(row, j).setCellValue(""); } } if (icount==28 && j==8) {//内审人 if (!Utility.isEmpty(Tore.getOrder_user_code())) { getCell(row, j).setCellValue(Tore.getOrder_user_name()); }else { getCell(row, j).setCellValue(""); } } if (icount==30 && j==3) { if (!Utility.isEmpty(Tore.getHealth())) { getCell(row, j).setCellValue(Tore.getHealth()); }else { getCell(row, j).setCellValue(""); } } if (icount==30 && j==5) { if (!Utility.isEmpty(Tore.getHmarry())) { getCell(row, j).setCellValue(Tore.getHmarry()); }else { getCell(row, j).setCellValue(""); } } if (icount==30 && j==7) { if (!Utility.isEmpty(Tore.getDesc_debt())) { getCell(row, j).setCellValue(Tore.getDesc_debt()); }else { getCell(row, j).setCellValue(""); } } if (icount==30 && j==9) { if (!Utility.isEmpty(Tore.getFlag_only())) { getCell(row, j).setCellValue("1".equals(Tore.getFlag_only())?"唯一":"不唯一"); }else { getCell(row, j).setCellValue(""); } } ArrayList<TOrderSell> arrayList = new ArrayList<TOrderSell>(); int q = 0; if(Tore.getSellList().size()>0) { List<TOrderSell> list = Tore.getSellList(); for (TOrderSell tOrderSell : list) { if ("1".equals(tOrderSell.getFrom_type())) { arrayList.add(tOrderSell); } } q = arrayList.size(); } if (q>0) { if (q==1) { if (icount==31 && j==3) { getCell(row, j).setCellValue(arrayList.get(0).getCompany()); } if (icount==31 && j==5) { getCell(row, j).setCellValue(arrayList.get(0).getPrice_final()+"万元"); } }else if (q==2) { if (icount==31 && j==3) { getCell(row, j).setCellValue(arrayList.get(0).getCompany()); } if (icount==31 && j==5) { getCell(row, j).setCellValue(arrayList.get(0).getPrice_final()+"万元"); } if (icount==32 && j==3) { getCell(row, j).setCellValue(arrayList.get(1).getCompany()); } if (icount==32 && j==5) { getCell(row, j).setCellValue(arrayList.get(1).getPrice_final()+"万元"); } }else { if (icount==31 && j==3) { getCell(row, j).setCellValue(arrayList.get(0).getCompany()); } if (icount==31 && j==5) { getCell(row, j).setCellValue(arrayList.get(0).getPrice_final()+"万元"); } if (icount==32 && j==3) { getCell(row, j).setCellValue(arrayList.get(1).getCompany()); } if (icount==32 && j==5) { getCell(row, j).setCellValue(arrayList.get(1).getPrice_final()+"万元"); } if (icount==32 && j==7) { getCell(row, j).setCellValue(arrayList.get(2).getCompany()); } if (icount==32 && j==9) { getCell(row, j).setCellValue(arrayList.get(2).getPrice_final()+"万元"); } } } if (icount==31 && j==7) { if (!Utility.isEmpty(Tore.getClose_down())) { getCell(row, j).setCellValue("1".equals(Tore.getClose_down())?"是":"否"); }else { getCell(row, j).setCellValue("无"); } } if (icount==33 && j==3) { if (!Utility.isEmpty(Tore.getDesc_repay())) { getCell(row, j).setCellValue(Tore.getDesc_repay()); }else { getCell(row, j).setCellValue(""); } } if (icount==34 && j==3) { if (!Utility.isEmpty(Tore.getDesc_use())) { getCell(row, j).setCellValue(Tore.getDesc_use()); }else { getCell(row, j).setCellValue(""); } } if (icount==35 && j==8) { if (!Utility.isEmpty(Tore.getHome_user_code())) { getCell(row, j).setCellValue(Tore.getHome_user_name()); }else { getCell(row, j).setCellValue(""); } } if (icount==36 && j==8) { if (!Utility.isEmpty(Tore.getSecond_user_name())) { getCell(row, j).setCellValue(Tore.getSecond_user_name()); }else { getCell(row, j).setCellValue(""); } } if (icount==37 && j==8) { getCell(row, j).setCellValue(new SimpleDateFormat("yyyy年MM月dd日").format(new Date())); } } } //删除多余的行 //共有人 int n = 3-i,l = i; if (i==0) { while (i <= 2) { sheet.shiftRows(10+l, sheet.getLastRowNum()+1, -1); System.err.println(i); i++; } }else { while (i <= n) { sheet.shiftRows(10+l, sheet.getLastRowNum()+1, -1); System.err.println(i); i++; } } //快卖家 int m = 0; List<TOrderSell> sellList = Tore.getSellList(); for (TOrderSell tOrderSell : sellList) { if ("2".equals(tOrderSell.getFrom_type())) { m++; } } int j = 10 - m,t = 0; while (t<j) { sheet.shiftRows(10+l+m, sheet.getLastRowNum()+1, -1); System.out.println(t+"---------"+10+l+m); t++; } System.err.println(10+l); System.err.println("i="+n+"---t="+t); if(Tore.getSellList().size()>0) { List<TOrderSell> list = Tore.getSellList(); for (TOrderSell tOrderSell : list) { if ("1".equals(tOrderSell.getFrom_type())) { arr.add(tOrderSell); } } } int size = arr.size(); if (size<=1) { sheet.shiftRows(32-n-t, sheet.getLastRowNum()+1, -1); } //设置单元格行高 // if (i<5) { for (int k = 0; k < i && k < 3; k++) { XSSFRow xssfRow = sheet.getRow(9+k); xssfRow.setHeight((short) (32*20)); } // } // // if (t<10) { for (int k = 0; k < t && k < 10; k++) { XSSFRow xssfRow = sheet.getRow(9+k+i); xssfRow.setHeight((short) (48*20)); } // } for (int k = 25-n-t; k < sheet.getLastRowNum()+1; k++) { XSSFRow xssfRow = sheet.getRow(k); xssfRow.setHeight((short) (28*20)); } //合并单元格 sheet.addMergedRegion(new CellRangeAddress(2,27-n-t,0,0)); getCell(sheet.getRow(2), 0).setCellValue("内审"); sheet.addMergedRegion(new CellRangeAddress(2,5,1,1)); getCell(sheet.getRow(2), 1).setCellValue("借款人信息"); sheet.addMergedRegion(new CellRangeAddress(6,23-n-t,1,1)); getCell(sheet.getRow(6), 1).setCellValue("抵押物信息"); sheet.addMergedRegion(new CellRangeAddress(24-n-t,25-n-t,1,1)); getCell(sheet.getRow(24-n-t), 1).setCellValue("被执行情况"); sheet.addMergedRegion(new CellRangeAddress(24-n-t,25-n-t,4,4)); getCell(sheet.getRow(24-n-t), 4).setCellValue("情况说明"); System.err.println(24-n-t); if (size>1) { sheet.addMergedRegion(new CellRangeAddress(30-n-t,34-n-t,0,0)); getCell(sheet.getRow(30-n-t), 0).setCellValue("外审"); sheet.addMergedRegion(new CellRangeAddress(31-n-t,32-n-t,1,1)); getCell(sheet.getRow(31-n-t), 1).setCellValue("抵押房情况"); sheet.addMergedRegion(new CellRangeAddress(33-n-t,34-n-t,1,1)); getCell(sheet.getRow(33-n-t), 1).setCellValue("借款情况"); }else { sheet.addMergedRegion(new CellRangeAddress(30-n-t,34-n-t-1,0,0)); getCell(sheet.getRow(30-n-t), 0).setCellValue("外审"); sheet.addMergedRegion(new CellRangeAddress(31-n-t,32-n-t-1,1,1)); getCell(sheet.getRow(31-n-t), 1).setCellValue("抵押房情况"); sheet.addMergedRegion(new CellRangeAddress(33-n-t-1,34-n-t-1,1,1)); getCell(sheet.getRow(33-1-n-t), 1).setCellValue("借款情况"); } // 打印读取值 // System.out.println(cell.getStringCellValue()); // 新建一输出流 FileOutputStream fout = new FileOutputStream(outXlsFile); // PS:filename 是你另存为的路径,不处理直接写入模版文件 // 存盘 workbook.write(fout); fout.flush(); // 结束关闭 fout.close(); System.err.println("文件已导出..."); } public static XSSFCell getCell(XSSFRow row, int index) { // 取得分发日期单元格 XSSFCell cell = row.getCell(index); // 如果单元格不存在 if (cell == null) { // 创建单元格 cell = row.createCell(index); } // 返回单元格 return cell; } public static XSSFSheet doLoop(XSSFSheet sheet, int flag) { for (int i = 0; i < sheet.getLastRowNum() + 1; i++) { if (i < (sheet.getLastRowNum() - flag)) { XSSFRow row = sheet.getRow(i); if (row == null) { int lastRowNum = sheet.getLastRowNum() + 1; sheet.shiftRows(i + 1, lastRowNum, -1); doLoop(sheet, ++flag); } else if (row.getCell((short) 0) == null) { int lastRowNum = sheet.getLastRowNum() + 1; sheet.shiftRows(i + 1, lastRowNum, -1); doLoop(sheet, ++flag); } } else { break; } } return sheet; }

中间设置行高的代码是因为 之前没有拆分单元格 后来拆开后赶时间就没管他 准备等果断时间再优化 但是一直没空

原文地址:https://www.cnblogs.com/yuztmt/p/9470281.html