關於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; }

中間設置行高的代碼是因為 之前沒有拆分單元格 后來拆開后趕時間就沒管他 准備等果斷時間再優化 但是一直沒空

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM