From 880118a8b949e35c9b5034db42a60b09fda0ff00 Mon Sep 17 00:00:00 2001 From: jueyue Date: Thu, 13 Jun 2019 09:59:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dclone=20wb=20=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E7=B4=A2=E5=BC=95=E4=BD=8D=E7=BD=AE=E9=94=99=E4=BD=8D?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../template/ExcelExportOfTemplateUtil.java | 8 ++- .../view/EasypoiBigExcelExportView.java | 65 +++++++------------ .../easypoi/view/EasypoiMapExcelView.java | 15 +---- .../easypoi/view/EasypoiSingleExcelView.java | 15 +---- .../view/EasypoiTemplateExcelView.java | 15 +---- .../easypoi/view/MapGraphExcelView.java | 15 +---- .../easypoi/view/MiniAbstractExcelView.java | 48 ++++++++++---- 7 files changed, 71 insertions(+), 110 deletions(-) diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/template/ExcelExportOfTemplateUtil.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/template/ExcelExportOfTemplateUtil.java index fc773f8..4ace541 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/template/ExcelExportOfTemplateUtil.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/template/ExcelExportOfTemplateUtil.java @@ -219,6 +219,10 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { wb = ExcelCache.getWorkbook(teplateParams.getTemplateUrl(), teplateParams.getSheetNum(), true); int oldSheetNum = wb.getNumberOfSheets(); + List oldSheetName = new ArrayList<>(); + for (int i = 0; i < oldSheetNum; i++) { + oldSheetName.add(wb.getSheetName(i)); + } // 把所有的KEY排个顺序 List> mapList; List sheetNumList = new ArrayList<>(); @@ -232,8 +236,8 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { wb.cloneSheet(sheetNum); } } - for (int i = 0; i < oldSheetNum; i++) { - wb.removeSheetAt(i); + for (int i = 0; i < oldSheetName.size(); i++) { + wb.removeSheetAt(wb.getSheetIndex(oldSheetName.get(i))); } // 创建表格样式 setExcelExportStyler((IExcelExportStyler) teplateParams.getStyle() diff --git a/easypoi-web/src/main/java/cn/afterturn/easypoi/view/EasypoiBigExcelExportView.java b/easypoi-web/src/main/java/cn/afterturn/easypoi/view/EasypoiBigExcelExportView.java index 32dc329..5505ba0 100644 --- a/easypoi-web/src/main/java/cn/afterturn/easypoi/view/EasypoiBigExcelExportView.java +++ b/easypoi-web/src/main/java/cn/afterturn/easypoi/view/EasypoiBigExcelExportView.java @@ -1,13 +1,13 @@ /** * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and @@ -15,22 +15,18 @@ */ package cn.afterturn.easypoi.view; -import java.util.Collections; -import java.util.List; -import java.util.Map; - -import javax.servlet.ServletOutputStream; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.ss.usermodel.Workbook; -import org.springframework.stereotype.Controller; - import cn.afterturn.easypoi.entity.vo.BigExcelConstants; import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.ExportParams; import cn.afterturn.easypoi.handler.inter.IExcelExportServer; +import org.apache.poi.ss.usermodel.Workbook; +import org.springframework.stereotype.Controller; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Collections; +import java.util.List; +import java.util.Map; /** * @author JueYue on 14-3-8. Excel 生成解析器,减少用户操作 @@ -47,36 +43,23 @@ public class EasypoiBigExcelExportView extends MiniAbstractExcelView { HttpServletResponse response) throws Exception { String codedFileName = "临时文件"; Workbook workbook = ExcelExportUtil.exportBigExcel( - (ExportParams) model.get(BigExcelConstants.PARAMS), - (Class) model.get(BigExcelConstants.CLASS), Collections.EMPTY_LIST); + (ExportParams) model.get(BigExcelConstants.PARAMS), + (Class) model.get(BigExcelConstants.CLASS), Collections.EMPTY_LIST); IExcelExportServer server = (IExcelExportServer) model.get(BigExcelConstants.DATA_INTER); - int page = 1; + int page = 1; List list = server - .selectListForExcelExport(model.get(BigExcelConstants.DATA_PARAMS), page++); + .selectListForExcelExport(model.get(BigExcelConstants.DATA_PARAMS), page++); while (list != null && list.size() > 0) { workbook = ExcelExportUtil.exportBigExcel( - (ExportParams) model.get(BigExcelConstants.PARAMS), - (Class) model.get(BigExcelConstants.CLASS), list); + (ExportParams) model.get(BigExcelConstants.PARAMS), + (Class) model.get(BigExcelConstants.CLASS), list); list = server.selectListForExcelExport(model.get(BigExcelConstants.DATA_PARAMS), - page++); + page++); } ExcelExportUtil.closeExportBigExcel(); if (model.containsKey(BigExcelConstants.FILE_NAME)) { codedFileName = (String) model.get(BigExcelConstants.FILE_NAME); } - if (workbook instanceof HSSFWorkbook) { - codedFileName += HSSF; - } else { - codedFileName += XSSF; - } - if (isIE(request)) { - codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF8"); - } else { - codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1"); - } - response.setHeader("content-disposition", "attachment;filename=" + codedFileName); - ServletOutputStream out = response.getOutputStream(); - workbook.write(out); - out.flush(); + out(workbook, codedFileName, request, response); } } diff --git a/easypoi-web/src/main/java/cn/afterturn/easypoi/view/EasypoiMapExcelView.java b/easypoi-web/src/main/java/cn/afterturn/easypoi/view/EasypoiMapExcelView.java index 86c5944..d5e7fa3 100644 --- a/easypoi-web/src/main/java/cn/afterturn/easypoi/view/EasypoiMapExcelView.java +++ b/easypoi-web/src/main/java/cn/afterturn/easypoi/view/EasypoiMapExcelView.java @@ -57,20 +57,7 @@ public class EasypoiMapExcelView extends MiniAbstractExcelView { if (model.containsKey(MapExcelConstants.FILE_NAME)) { codedFileName = (String) model.get(MapExcelConstants.FILE_NAME); } - if (workbook instanceof HSSFWorkbook) { - codedFileName += HSSF; - } else { - codedFileName += XSSF; - } - if (isIE(request)) { - codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF8"); - } else { - codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1"); - } - response.setHeader("content-disposition", "attachment;filename=" + codedFileName); - ServletOutputStream out = response.getOutputStream(); - workbook.write(out); - out.flush(); + out(workbook, codedFileName, request, response); } } diff --git a/easypoi-web/src/main/java/cn/afterturn/easypoi/view/EasypoiSingleExcelView.java b/easypoi-web/src/main/java/cn/afterturn/easypoi/view/EasypoiSingleExcelView.java index 80f9ea9..d56570a 100644 --- a/easypoi-web/src/main/java/cn/afterturn/easypoi/view/EasypoiSingleExcelView.java +++ b/easypoi-web/src/main/java/cn/afterturn/easypoi/view/EasypoiSingleExcelView.java @@ -73,19 +73,6 @@ public class EasypoiSingleExcelView extends MiniAbstractExcelView { if (model.containsKey(NormalExcelConstants.FILE_NAME)) { codedFileName = (String) model.get(NormalExcelConstants.FILE_NAME); } - if (workbook instanceof HSSFWorkbook) { - codedFileName += HSSF; - } else { - codedFileName += XSSF; - } - if (isIE(request)) { - codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF8"); - } else { - codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1"); - } - response.setHeader("content-disposition", "attachment;filename=" + codedFileName); - ServletOutputStream out = response.getOutputStream(); - workbook.write(out); - out.flush(); + out(workbook, codedFileName, request, response); } } diff --git a/easypoi-web/src/main/java/cn/afterturn/easypoi/view/EasypoiTemplateExcelView.java b/easypoi-web/src/main/java/cn/afterturn/easypoi/view/EasypoiTemplateExcelView.java index 6672706..df0806f 100644 --- a/easypoi-web/src/main/java/cn/afterturn/easypoi/view/EasypoiTemplateExcelView.java +++ b/easypoi-web/src/main/java/cn/afterturn/easypoi/view/EasypoiTemplateExcelView.java @@ -58,19 +58,6 @@ public class EasypoiTemplateExcelView extends MiniAbstractExcelView { if (model.containsKey(NormalExcelConstants.FILE_NAME)) { codedFileName = (String) model.get(NormalExcelConstants.FILE_NAME); } - if (workbook instanceof HSSFWorkbook) { - codedFileName += HSSF; - } else { - codedFileName += XSSF; - } - if (isIE(request)) { - codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF8"); - } else { - codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1"); - } - response.setHeader("content-disposition", "attachment;filename=" + codedFileName); - ServletOutputStream out = response.getOutputStream(); - workbook.write(out); - out.flush(); + out(workbook, codedFileName, request, response); } } diff --git a/easypoi-web/src/main/java/cn/afterturn/easypoi/view/MapGraphExcelView.java b/easypoi-web/src/main/java/cn/afterturn/easypoi/view/MapGraphExcelView.java index 5bea314..40aa068 100644 --- a/easypoi-web/src/main/java/cn/afterturn/easypoi/view/MapGraphExcelView.java +++ b/easypoi-web/src/main/java/cn/afterturn/easypoi/view/MapGraphExcelView.java @@ -63,20 +63,7 @@ public class MapGraphExcelView extends MiniAbstractExcelView { if (model.containsKey(MapExcelGraphConstants.FILE_NAME)) { codedFileName = (String) model.get(MapExcelGraphConstants.FILE_NAME); } - if (workbook instanceof HSSFWorkbook) { - codedFileName += HSSF; - } else { - codedFileName += XSSF; - } - if (isIE(request)) { - codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF8"); - } else { - codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1"); - } - response.setHeader("content-disposition", "attachment;filename=" + codedFileName); - ServletOutputStream out = response.getOutputStream(); - workbook.write(out); - out.flush(); + out(workbook, codedFileName, request, response); } } diff --git a/easypoi-web/src/main/java/cn/afterturn/easypoi/view/MiniAbstractExcelView.java b/easypoi-web/src/main/java/cn/afterturn/easypoi/view/MiniAbstractExcelView.java index 0901547..5740693 100644 --- a/easypoi-web/src/main/java/cn/afterturn/easypoi/view/MiniAbstractExcelView.java +++ b/easypoi-web/src/main/java/cn/afterturn/easypoi/view/MiniAbstractExcelView.java @@ -1,13 +1,13 @@ /** * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and @@ -15,6 +15,13 @@ */ package cn.afterturn.easypoi.view; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Workbook; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + /** * 基础抽象Excel View * @author JueYue @@ -22,13 +29,32 @@ package cn.afterturn.easypoi.view; */ public abstract class MiniAbstractExcelView extends PoiBaseView { - private static final String CONTENT_TYPE = "text/html;application/vnd.ms-excel"; + private static final String CONTENT_TYPE = "text/html;application/vnd.ms-excel"; - protected static final String HSSF = ".xls"; - protected static final String XSSF = ".xlsx"; + protected static final String HSSF = ".xls"; + protected static final String XSSF = ".xlsx"; public MiniAbstractExcelView() { setContentType(CONTENT_TYPE); } + + public void out(Workbook workbook, String codedFileName, HttpServletRequest request, + HttpServletResponse response) throws Exception { + if (workbook instanceof HSSFWorkbook) { + codedFileName += HSSF; + } else { + codedFileName += XSSF; + } + if (isIE(request)) { + codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF8"); + } else { + codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1"); + } + response.setHeader("content-disposition", "attachment;filename=" + codedFileName); + ServletOutputStream out = response.getOutputStream(); + workbook.write(out); + out.flush(); + } + }