日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

easy poi 模板生成多表excel

發布時間:2023/12/10 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 easy poi 模板生成多表excel 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 需求

要求根據查詢,在同一個excel sheet中生成如下并列的四個excel表

2. 模板制作

根據查詢字段,制作如下模板(項目/resources/templates/kpi/TCRR_TEMPLATE.xlsx)

3 代碼

3.1 serviceImpl

/*** TCRR 下載后的文件名稱*/public static final String TCRR_FILE = "TCRR.xlsx";/*** TCRR 下載模板名稱*/public static final String TCRR_PATH = "templates/kpi/TCRR_TEMPLATE.xlsx";/*** TCRR excel報表下載* 1. 模板包含多張表的數據,先獲取多張表的list數據;* 2. 組合數據為poi需要的格式;* 3. 調用poi接口** @param queryParam 查詢參數* @param response 響應參數* @return 下載結果* @author: leiming5*/public void downloadExcel(StTcrrQueryParam queryParam, HttpServletResponse response) {// 1List<OverallTrendVo> overallTrendVoList = getOverallTrend(queryParam);List<GeoSerieVo> geoSerieVoList = getGeoByWeekOrMonth(queryParam);List<OdmProductVo> odmProductVoList = getODMByWeekOrMonth(queryParam);List<PartsWarrantyVo> partsWarrantyVoList = getPartsByWeekOrMonth(queryParam);// 2JSONObject result = new JSONObject();result.put("OverallTCRRTrend", overallTrendVoList);result.put("TCRRByGeo", geoSerieVoList);result.put("TCRRByODM", odmProductVoList);result.put("PartsWarranty", partsWarrantyVoList);// 3commomExportExcel.commonDownLoad(result, TCRR_PATH, TCRR_FILE, response);}

3.2 commomExportExcel

package com.leinovo.qes.portal.modules.report.poi;import org.springframework.stereotype.Component;import javax.servlet.http.HttpServletResponse; import java.util.List; import java.util.Map;/*** Student導出excel工具類*/ @Component public class CommomExportExcel extends AbstractExportTemplate {/*** map格式數據下載** @param excelData 數據* @param templatePath 模板存在相對路勁* @param downFileName 下載后的文件名稱* @param response 相應對象* @param sheetNum 模板列數*/public void commonDownLoad(Map<String, Object> excelData, String templatePath, String downFileName,HttpServletResponse response, Integer... sheetNum) {commonDownLoadExcel(excelData, templatePath, downFileName, response, sheetNum);}/*** 列表下載** @param rowList 數據* @param templatePath 模板存在相對路勁* @param downFileName 下載后的文件名稱* @param response 相應對象*/public <T> void commonDownLoad(List<T> rowList, String templatePath, String downFileName, HttpServletResponse response) {simpleDownLoadExcel(rowList, templatePath, downFileName, response);}public <T> void commonDownLoad(List<T> rowList, String templatePath, String downFileName, HttpServletResponse response,Integer... number) {simpleDownLoadExcel(rowList, templatePath, downFileName, response, number);} }

3.3 AbstractExportTemplate

package com.leinovo.qes.portal.modules.report.poi;import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.TemplateExportParams; import com.leinovo.qes.portal.modules.report.utils.CloseableUtils; import com.leinovo.qes.portal.modules.report.utils.FileUtils; import lombok.Getter; import lombok.Setter; import lombok.ToString; import lombok.extern.slf4j.Slf4j; import org.apache.poi.ss.usermodel.Workbook;import javax.servlet.http.HttpServletResponse; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map;/*** 根據模板導出excel抽象父類*/ @Slf4j @Getter @Setter @ToString public abstract class AbstractExportTemplate {private static final String COMMON_ROWLIST_NAME = "rowList";// 導出的模板protected String templatePath;// 導出的文件名protected String downLoadFileName;// 導出到excel的數據protected Map<String, Object> excelData = new HashMap<>();private InputStream buildExcelInputStream(Map<String, Object> excelData, Integer... sheetNum) {Workbook workbook = null;ByteArrayOutputStream outputStream = null;ByteArrayInputStream inputStream = null;try {TemplateExportParams params = new TemplateExportParams(templatePath, sheetNum);workbook = ExcelExportUtil.exportExcel(params, excelData);// 獲取excel輸出流outputStream = new ByteArrayOutputStream();workbook.write(outputStream);// 根據輸出流程獲取excel的輸入流inputStream = new ByteArrayInputStream(outputStream.toByteArray());} catch (IOException e) {log.error("導出excel出錯", e);} finally {CloseableUtils.close(outputStream, workbook);}return inputStream;}/*** 給excel設置數據** @param* @param sourceListData* @return*/public <T> void setRowList(List<T> sourceListData) {this.excelData.put(COMMON_ROWLIST_NAME, sourceListData);}/*** 下載方式一:這種方式適用于下載列表數據** @param sourceListData* @param templatePath* @param downFileName* @param response* @param <T>*/protected <T> void simpleDownLoadExcel(List<T> sourceListData, String templatePath, String downFileName, HttpServletResponse response) {setRowList(sourceListData);this.commonDownLoadExcel(this.excelData, templatePath, downFileName, response);}protected <T> void simpleDownLoadExcel(List<T> sourceListData, String templatePath, String downFileName,HttpServletResponse response,Integer... number) {setRowList(sourceListData);this.commonDownLoadExcel(this.excelData, templatePath, downFileName, response, number);}/*** 下載方式二:這種方式,需要子類封裝好下載的Map數據*/protected void commonDownLoadExcel(Map<String, Object> excelData, String templatePath, String downFileName,HttpServletResponse response, Integer... sheetNum) {this.excelData = excelData;this.templatePath = templatePath;this.downLoadFileName = downFileName;InputStream inputStream = null;try {setExcelData();inputStream = buildExcelInputStream(this.excelData, sheetNum);FileUtils.writeToResponse(response, inputStream, downFileName);} finally {CloseableUtils.close(inputStream);}}/*** 模板方法,子類設置excel中需要展示的數據*/protected void setExcelData() {// 設置excelData}}

3.4 FileUtils

package com.leinovo.qes.portal.modules.report.utils;import org.apache.commons.compress.utils.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory;import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder;public final class FileUtils {/*** 下載文件到瀏覽器** @param response* @param inputStream* @param fileName 瀏覽器下載的文件名稱*/public static void writeToResponse(HttpServletResponse response, InputStream inputStream, String fileName) {ServletOutputStream outputStream = null;try {outputStream = response.getOutputStream();// 設置強制下載不打開response.setContentType("application/force-download");response.setHeader("Content-Disposition", "attachment;filename*=utf-8'zh_cn'" + URLEncoder.encode(fileName, "UTF-8"));IOUtils.copy(inputStream, outputStream);} catch (Exception e) {log.error("瀏覽器下載文件出錯, fileName:{}", fileName, e);} finally {// close streamCloseableUtils.close(inputStream, outputStream);}}/*** 刪除本地文件** @param filePath*/public static void deleteLocalFile(String filePath) {File file = new File(filePath);file.delete();} }

3.5 CloseableUtils

package com.leinovo.qes.portal.modules.report.utils;import java.io.Closeable; import java.io.IOException;/*** Created by kongkp on 16-8-26.*/ public final class CloseableUtils {private CloseableUtils() {}public static void close(Closeable... closables) {if (closables == null || closables.length == 0) {return;}for (Closeable closable : closables) {try {if (closable != null) {closable.close();}} catch (IOException e) {System.err.println("Close resource exception:" + e.getStackTrace());}}}}

總結

以上是生活随笔為你收集整理的easy poi 模板生成多表excel的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。