| @@ -219,6 +219,10 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||||
| wb = ExcelCache.getWorkbook(teplateParams.getTemplateUrl(), teplateParams.getSheetNum(), | wb = ExcelCache.getWorkbook(teplateParams.getTemplateUrl(), teplateParams.getSheetNum(), | ||||
| true); | true); | ||||
| int oldSheetNum = wb.getNumberOfSheets(); | int oldSheetNum = wb.getNumberOfSheets(); | ||||
| List<String> oldSheetName = new ArrayList<>(); | |||||
| for (int i = 0; i < oldSheetNum; i++) { | |||||
| oldSheetName.add(wb.getSheetName(i)); | |||||
| } | |||||
| // 把所有的KEY排个顺序 | // 把所有的KEY排个顺序 | ||||
| List<Map<String, Object>> mapList; | List<Map<String, Object>> mapList; | ||||
| List<Integer> sheetNumList = new ArrayList<>(); | List<Integer> sheetNumList = new ArrayList<>(); | ||||
| @@ -232,8 +236,8 @@ public final class ExcelExportOfTemplateUtil extends BaseExportService { | |||||
| wb.cloneSheet(sheetNum); | 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() | setExcelExportStyler((IExcelExportStyler) teplateParams.getStyle() | ||||
| @@ -1,13 +1,13 @@ | |||||
| /** | /** | ||||
| * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) | * 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 | |||||
| * <p> | |||||
| * 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 | |||||
| * <p> | |||||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||||
| * <p> | |||||
| * Unless required by applicable law or agreed to in writing, software | |||||
| * distributed under the License is distributed on an "AS IS" BASIS, | * distributed under the License is distributed on an "AS IS" BASIS, | ||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
| * See the License for the specific language governing permissions and | * See the License for the specific language governing permissions and | ||||
| @@ -15,22 +15,18 @@ | |||||
| */ | */ | ||||
| package cn.afterturn.easypoi.view; | 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.entity.vo.BigExcelConstants; | ||||
| import cn.afterturn.easypoi.excel.ExcelExportUtil; | import cn.afterturn.easypoi.excel.ExcelExportUtil; | ||||
| import cn.afterturn.easypoi.excel.entity.ExportParams; | import cn.afterturn.easypoi.excel.entity.ExportParams; | ||||
| import cn.afterturn.easypoi.handler.inter.IExcelExportServer; | 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 生成解析器,减少用户操作 | * @author JueYue on 14-3-8. Excel 生成解析器,减少用户操作 | ||||
| @@ -47,36 +43,23 @@ public class EasypoiBigExcelExportView extends MiniAbstractExcelView { | |||||
| HttpServletResponse response) throws Exception { | HttpServletResponse response) throws Exception { | ||||
| String codedFileName = "临时文件"; | String codedFileName = "临时文件"; | ||||
| Workbook workbook = ExcelExportUtil.exportBigExcel( | 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); | IExcelExportServer server = (IExcelExportServer) model.get(BigExcelConstants.DATA_INTER); | ||||
| int page = 1; | |||||
| int page = 1; | |||||
| List<Object> list = server | List<Object> list = server | ||||
| .selectListForExcelExport(model.get(BigExcelConstants.DATA_PARAMS), page++); | |||||
| .selectListForExcelExport(model.get(BigExcelConstants.DATA_PARAMS), page++); | |||||
| while (list != null && list.size() > 0) { | while (list != null && list.size() > 0) { | ||||
| workbook = ExcelExportUtil.exportBigExcel( | 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), | list = server.selectListForExcelExport(model.get(BigExcelConstants.DATA_PARAMS), | ||||
| page++); | |||||
| page++); | |||||
| } | } | ||||
| ExcelExportUtil.closeExportBigExcel(); | ExcelExportUtil.closeExportBigExcel(); | ||||
| if (model.containsKey(BigExcelConstants.FILE_NAME)) { | if (model.containsKey(BigExcelConstants.FILE_NAME)) { | ||||
| codedFileName = (String) model.get(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); | |||||
| } | } | ||||
| } | } | ||||
| @@ -57,20 +57,7 @@ public class EasypoiMapExcelView extends MiniAbstractExcelView { | |||||
| if (model.containsKey(MapExcelConstants.FILE_NAME)) { | if (model.containsKey(MapExcelConstants.FILE_NAME)) { | ||||
| codedFileName = (String) model.get(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); | |||||
| } | } | ||||
| } | } | ||||
| @@ -73,19 +73,6 @@ public class EasypoiSingleExcelView extends MiniAbstractExcelView { | |||||
| if (model.containsKey(NormalExcelConstants.FILE_NAME)) { | if (model.containsKey(NormalExcelConstants.FILE_NAME)) { | ||||
| codedFileName = (String) model.get(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); | |||||
| } | } | ||||
| } | } | ||||
| @@ -58,19 +58,6 @@ public class EasypoiTemplateExcelView extends MiniAbstractExcelView { | |||||
| if (model.containsKey(NormalExcelConstants.FILE_NAME)) { | if (model.containsKey(NormalExcelConstants.FILE_NAME)) { | ||||
| codedFileName = (String) model.get(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); | |||||
| } | } | ||||
| } | } | ||||
| @@ -63,20 +63,7 @@ public class MapGraphExcelView extends MiniAbstractExcelView { | |||||
| if (model.containsKey(MapExcelGraphConstants.FILE_NAME)) { | if (model.containsKey(MapExcelGraphConstants.FILE_NAME)) { | ||||
| codedFileName = (String) model.get(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); | |||||
| } | } | ||||
| } | } | ||||
| @@ -1,13 +1,13 @@ | |||||
| /** | /** | ||||
| * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com) | * 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 | |||||
| * <p> | |||||
| * 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 | |||||
| * <p> | |||||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||||
| * <p> | |||||
| * Unless required by applicable law or agreed to in writing, software | |||||
| * distributed under the License is distributed on an "AS IS" BASIS, | * distributed under the License is distributed on an "AS IS" BASIS, | ||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
| * See the License for the specific language governing permissions and | * See the License for the specific language governing permissions and | ||||
| @@ -15,6 +15,13 @@ | |||||
| */ | */ | ||||
| package cn.afterturn.easypoi.view; | 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 | * 基础抽象Excel View | ||||
| * @author JueYue | * @author JueYue | ||||
| @@ -22,13 +29,32 @@ package cn.afterturn.easypoi.view; | |||||
| */ | */ | ||||
| public abstract class MiniAbstractExcelView extends PoiBaseView { | 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() { | public MiniAbstractExcelView() { | ||||
| setContentType(CONTENT_TYPE); | 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(); | |||||
| } | |||||
| } | } | ||||