From cce92983fdef344aad77d6ec68720960d7c4dcc1 Mon Sep 17 00:00:00 2001 From: ggg444 Date: Wed, 15 Sep 2021 08:27:45 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96Excel=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E8=AE=A1=E7=AE=97sheet=E6=95=B0=E7=9B=AE=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=201.=20=E5=AF=BC=E5=87=BA=E6=95=B0=E6=8D=AE=E7=9A=84?= =?UTF-8?q?=E6=80=BB=E6=9D=A1=E6=95=B0=E5=88=9A=E5=A5=BD=E7=AD=89=E4=BA=8E?= =?UTF-8?q?sheet=E6=9C=80=E5=A4=A7=E8=A1=8C=E6=95=B0=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E4=BC=9A=E5=A4=9A=E4=BA=A7=E7=94=9F=E4=B8=80=E4=B8=AA=E7=A9=BA?= =?UTF-8?q?=E7=99=BDsheet=20=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81=E5=A6=82?= =?UTF-8?q?=E4=B8=8B=EF=BC=9A=20=20=20=20=20public=20static=20void=20main(?= =?UTF-8?q?String[]=20args)=20{=20=20=20=20=20=20=20=20=20System.out.print?= =?UTF-8?q?ln("=E6=97=A0=E6=95=B0=E6=8D=AE=E6=97=B6=EF=BC=8C1=E9=A1=B5?= =?UTF-8?q?=EF=BC=9A"=20+=20getTotalPage(sheetSize=20*=200));=20=20=20=20?= =?UTF-8?q?=20=20=20=20=20System.out.println("=E5=88=9A=E5=A5=BD1=E9=A1=B5?= =?UTF-8?q?=EF=BC=9A"=20+=20getTotalPage(sheetSize=20*=201));=20=20=20=20?= =?UTF-8?q?=20=20=20=20=20System.out.println("=E5=88=9A=E5=A5=BD7=E9=A1=B5?= =?UTF-8?q?=EF=BC=9A"=20+=20getTotalPage(sheetSize=20*=207));=20=20=20=20?= =?UTF-8?q?=20=20=20=20=20System.out.println("1=E9=A1=B5=EF=BC=9A"=20+=20g?= =?UTF-8?q?etTotalPage(200));=20=20=20=20=20=20=20=20=20System.out.println?= =?UTF-8?q?("2=E9=A1=B5=EF=BC=9A"=20+=20getTotalPage(sheetSize=20*=201=20+?= =?UTF-8?q?=20200));=20=20=20=20=20=20=20=20=20System.out.println("8?= =?UTF-8?q?=E9=A1=B5=EF=BC=9A"=20+=20getTotalPage(sheetSize=20*=207=20+=20?= =?UTF-8?q?200));=20=20=20=20=20=20=20=20=20System.out.println("=3D=3D=3D?= =?UTF-8?q?=3D=3D=3D=3D=3D=3D");=20=20=20=20=20=20=20=20=20System.out.prin?= =?UTF-8?q?tln("=E6=97=A0=E6=95=B0=E6=8D=AE=E6=97=B6=EF=BC=8C1=E9=A1=B5?= =?UTF-8?q?=EF=BC=9A"=20+=20getTotalPage2(sheetSize=20*=200));=20=20=20=20?= =?UTF-8?q?=20=20=20=20=20System.out.println("=E5=88=9A=E5=A5=BD1=E9=A1=B5?= =?UTF-8?q?=EF=BC=9A"=20+=20getTotalPage2(sheetSize=20*=201));=20=20=20=20?= =?UTF-8?q?=20=20=20=20=20System.out.println("=E5=88=9A=E5=A5=BD7=E9=A1=B5?= =?UTF-8?q?=EF=BC=9A"=20+=20getTotalPage2(sheetSize=20*=207));=20=20=20=20?= =?UTF-8?q?=20=20=20=20=20System.out.println("1=E9=A1=B5=EF=BC=9A"=20+=20g?= =?UTF-8?q?etTotalPage2(200));=20=20=20=20=20=20=20=20=20System.out.printl?= =?UTF-8?q?n("2=E9=A1=B5=EF=BC=9A"=20+=20getTotalPage2(sheetSize=20*=201?= =?UTF-8?q?=20+=20200));=20=20=20=20=20=20=20=20=20System.out.println("8?= =?UTF-8?q?=E9=A1=B5=EF=BC=9A"=20+=20getTotalPage2(sheetSize=20*=207=20+?= =?UTF-8?q?=20200));=20=20=20=20=20}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit private static double getTotalPage(int size) { //原方法。 //ceil方法中是两个int做除法,结果还是int。ceil实际上没有起到作用。 return Math.ceil(size / sheetSize); } private static int getTotalPage2(int size) { //修改后的方法。 //size * 1.0 使int转为double,做除法后还是double。max(1, ...)是为了在size为0时,始终能生成一个sheet。 //需要修改for循环的判断条件 return Math.max(1, (int)Math.ceil(size * 1.0 / sheetSize)); } 测试结果: //预期结果:0,0,6,0,1,7 无数据时,1页:0.0 刚好1页:1.0 刚好7页:7.0 1页:0.0 2页:1.0 8页:7.0 ========= //预期结果:1,1,7,1,2,8 无数据时,1页:1 刚好1页:1 刚好7页:7 1页:1 2页:2 8页:8 2. 有疑问的地方(本次提交没有做修改): 1105行 index是从0开始的,即sheet的后缀名也是从0开始的。如果要使后缀名从1开始,可以考虑 (index + 1) --- .../main/java/com/ruoyi/common/utils/poi/ExcelUtil.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java index 54157f353..9b7e0d56f 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java @@ -465,8 +465,8 @@ public class ExcelUtil public void writeSheet() { // 取出一共有多少个sheet. - double sheetNo = Math.ceil(list.size() / sheetSize); - for (int index = 0; index <= sheetNo; index++) + int sheetNo = Math.max(1, (int)Math.ceil(list.size() * 1.0 / sheetSize)); + for (int index = 0; index < sheetNo; index++) { createSheet(sheetNo, index); @@ -1091,12 +1091,12 @@ public class ExcelUtil * @param sheetNo sheet数量 * @param index 序号 */ - public void createSheet(double sheetNo, int index) + public void createSheet(int sheetNo, int index) { this.sheet = wb.createSheet(); this.styles = createStyles(wb); // 设置工作表的名称. - if (sheetNo == 0) + if (sheetNo == 1) { wb.setSheetName(index, sheetName); }