色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

SpringBoot快速集成jxls-poi(自定義模板,支持本地文件導出,在線文件導出)

瀏覽:8日期:2023-04-27 16:11:19

在項目持續集成的過程中,有時候需要實現報表導出和文檔導出,類似于excel中這種文檔的導出,在要求不高的情況下,有人可能會考慮直接導出csv文件來簡化導出過程。但是導出xlsx文件,其實過程相對更復雜。解決方案就是使用poi的jar包。使用源生的poi來操作表格,代碼冗余,處理復雜,同時poi的相關聯的依賴還會存在版本兼容問題。所以直接使用poi來實現表格導出,維護成本大,不易于拓展。

我們需要學會站在巨人的肩膀上解決問題,jxls-poi這個就很好解決這個excel表格導出的多樣化的問題。類似jsp和thymealf的模板定義,使得表格導出變得簡單可控。

不多BB上代碼

1.引入關鍵依賴包

<!-- jxls-api依賴 --><dependency><groupId>org.jxls</groupId><artifactId>jxls-poi</artifactId><version>1.0.15</version></dependency><dependency><groupId>org.jxls</groupId><artifactId>jxls</artifactId><version>2.4.6</version></dependency>

這里只需要兩個依賴便操作excel表格了。

2.定義模板文件

SpringBoot快速集成jxls-poi(自定義模板,支持本地文件導出,在線文件導出)

新建一個excel文件,后綴名為.xlsx,在resources目錄下新增一個jxls的文件夾,把模板文件放在這個文件夾下,便于后續的spring-boot的集成。

3.導出工具類

/** * @author machenike */public class ExcelUtils { /*** * excel導出到response * @param fileName 導出文件名 * @param templateFile 模板文件地址 * @param params 數據集合 * @param response response */ public static void exportExcel(String fileName, InputStream templateFile, Map<String, Object> params, HttpServletResponse response) throws IOException { response.reset(); response.setHeader('Accept-Ranges', 'bytes'); OutputStream os = null; response.setHeader('Content-disposition', String.format('attachment; filename='%s'', fileName)); response.setContentType('application/octet-stream;charset=UTF-8'); try { os = response.getOutputStream(); exportExcel(templateFile, params, os); } catch (IOException e) { throw e; } } /** * 導出excel到輸出流中 * @param templateFile 模板文件 * @param params 傳入參數 * @param os 輸出流 * @throws IOException */ public static void exportExcel(InputStream templateFile, Map<String, Object> params, OutputStream os) throws IOException { try { Context context = new Context(); Set<String> keySet = params.keySet(); for (String key : keySet) {//設置參數變量context.putVar(key, params.get(key)); } Map<String, Object> myFunction = new HashMap<>(); myFunction.put('fun', new ExcelUtils()); // 啟動新的jxls-api 加載自定義方法 Transformer trans = TransformerFactory.createTransformer(templateFile, os); JexlExpressionEvaluator evaluator = (JexlExpressionEvaluator) trans.getTransformationConfig().getExpressionEvaluator(); evaluator.getJexlEngine().setFunctions(myFunction); // 載入模板、處理導出 AreaBuilder areaBuilder = new XlsCommentAreaBuilder(trans); List<Area> areaList = areaBuilder.build(); areaList.get(0).applyAt(new CellRef('sheet1!A1'), context); trans.write(); } catch (IOException e) { throw e; } finally { try {if (os != null) { os.flush(); os.close();}if (templateFile != null) { templateFile.close();} } catch (IOException e) {throw e; } } } /** * 格式化時間 */ public Object formatDate(Date date) { if (date != null) { SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss'); String dateStr = sdf.format(date); return dateStr; } return '--'; } /** * 設置超鏈接方法 */ public WritableCellValue getLink(String address, String title) { return new WritableHyperlink(address, title); }}

這個工具類中我定義兩個導出用的方法一個是直接是HttpServletResponse 導出在線下載文件一個使用輸入流導出同時模板中還支持方法傳入。

4.定義導出服務類

全局配置jxls模板的基礎路徑

#jxls模板的基礎路徑jxls.template.path: classpath:jxls/

定義服務接口

/** * excel用service */public interface ExcelService { /** * 導出excel,寫入輸出流中 * @param templateFile * @param params * @param os * @return */ boolean getExcel(String templateFile,Map<String,Object> params, OutputStream os); /** * 導出excel,寫入response中 * @param templateFile * @param fileName * @param params * @param response * @return */ boolean getExcel(String templateFile,String fileName, Map<String,Object> params, HttpServletResponse response); /** * 導出excel,寫入文件中 * @param templateFile * @param params * @param outputFile * @return */ boolean getExcel(String templateFile, Map<String,Object> params, File outputFile);}

excel導出用服務實現類

/** * excel用serviceImpl */@Servicepublic class ExcelServiceImppl implements ExcelService { private static final Logger logger = LoggerFactory.getLogger(ExcelServiceImppl.class); /** * 模板文件的基礎路徑 */ @Value('${jxls.template.path}') private String templatePath; @Override public boolean getExcel(String templateFile, Map<String, Object> params, OutputStream os) { FileInputStream inputStream = null; try { //獲取模板文件的輸入流 inputStream = new FileInputStream(ResourceUtils.getFile(templatePath + templateFile)); //導出文件到輸出流 ExcelUtils.exportExcel(inputStream, params, os); } catch (IOException e) { logger.error('excel export has error' + e); return false; } return true; } @Override public boolean getExcel(String templateFile, String fileName, Map<String, Object> params, HttpServletResponse response) { FileInputStream inputStream = null; try { //獲取模板文件的輸入流 inputStream = new FileInputStream(ResourceUtils.getFile(templatePath + templateFile)); //導出文件到response ExcelUtils.exportExcel(fileName,inputStream,params,response); } catch (IOException e) { logger.error('excel export has error' + e); return false; } return true; } @Override public boolean getExcel(String templateFile, Map<String, Object> params, File outputFile) { FileInputStream inputStream = null; try { //獲取模板文件的輸入流 inputStream = new FileInputStream(ResourceUtils.getFile(templatePath + templateFile)); File dFile = outputFile.getParentFile(); //文件夾不存在時創建文件夾 if(dFile.isDirectory()){if(!dFile.exists()){ dFile.mkdir();} } //文件不存在時創建文件 if(!outputFile.exists()){outputFile.createNewFile(); } //導出excel文件 ExcelUtils.exportExcel(inputStream, params, new FileOutputStream(outputFile)); } catch (IOException e) { logger.error('excel export has error' + e); return false; } return true; }}

如上,服務類提供了,三種導出的實現方法

導出excel寫入response中 導出excel寫入輸出流中 導出excel寫入文件中

這三種方法足以覆蓋所有的業務需求

5.編輯jxls模板

這里為方便導出最好定義與模板匹配的實體類,便于數據的裝載和導出

public class UserModel { private Integer id; private String name; private String sex; private Integer age; private String remark; private Date date; private String link; }

SpringBoot快速集成jxls-poi(自定義模板,支持本地文件導出,在線文件導出)

插入標注定義模板,定義迭代對象jx:each

jx:each(items='list' var='item' lastCell='G3')

上面G3 模板中迭代的結束位置,然后用類似EL表達式的方式填充到模板當中

SpringBoot快速集成jxls-poi(自定義模板,支持本地文件導出,在線文件導出)

填寫范圍jx:area

jx:area(lastCell='G3')

模板編輯完成后保存即可,

6.代碼測試使用

導出為本地文件

@SpringBootTestclass BlogJxlsApplicationTests { @Autowired ExcelService excelService; @Test void contextLoads() { Map<String, Object> params = new HashMap(); List<UserModel> list = new ArrayList<>(); list.add(new UserModel(1, 'test01', '男', 25, 'tttttttttt',new Date(),'htpp://wwww.baidu.com')); list.add(new UserModel(2, 'test02', '男', 20, 'tttttttttt',new Date(),'htpp://wwww.baidu.com')); list.add(new UserModel(3, 'test04', '女', 25, 'ttttddddasdadatttttt',new Date(),'htpp://wwww.baidu.com')); list.add(new UserModel(4, 'test08', '男', 20, 'ttttttdasdatttt',new Date(),'htpp://wwww.baidu.com')); list.add(new UserModel(5, 'test021', '女', 25, 'ttttdatttttt',new Date(),'htpp://wwww.baidu.com')); list.add(new UserModel(7, 'test041', '男', 25, 'ttdadatttttttt',new Date(),'htpp://wwww.baidu.com'));params.put('list', list); excelService.getExcel('t1.xlsx', params, new File('D:test05.xlsx')); }}

導出成功

SpringBoot快速集成jxls-poi(自定義模板,支持本地文件導出,在線文件導出)

在線導出文件

@RestControllerpublic class TestController { @Autowired ExcelService excelService; @RequestMapping('test') public void testFile(HttpServletResponse response){ Map<String, Object> params = new HashMap(); List<UserModel> list = new ArrayList<>(); list.add(new UserModel(1, 'test01', '男', 25, 'tttttttttt',new Date(),'htpp://wwww.baidu.com')); list.add(new UserModel(2, 'test02', '男', 20, 'tttttttttt',new Date(),'htpp://wwww.baidu.com')); list.add(new UserModel(3, 'test04', '女', 25, 'ttttddddasdadatttttt',new Date(),'htpp://wwww.baidu.com')); list.add(new UserModel(4, 'test08', '男', 20, 'ttttttdasdatttt',new Date(),'htpp://wwww.baidu.com')); list.add(new UserModel(5, 'test021', '女', 25, 'ttttdatttttt',new Date(),'htpp://wwww.baidu.com')); list.add(new UserModel(7, 'test041', '男', 25, 'ttdadatttttttt',new Date(),'htpp://wwww.baidu.com')); params.put('list', list); excelService.getExcel('t1.xlsx',System.currentTimeMillis()+'.xlsx', params,response); }}

SpringBoot快速集成jxls-poi(自定義模板,支持本地文件導出,在線文件導出)

導出成功

SpringBoot快速集成jxls-poi(自定義模板,支持本地文件導出,在線文件導出)

SpringBoot快速集成jxls-poi(自定義模板,支持本地文件導出,在線文件導出)

源碼地址https://github.com/DavidLei08/BlogJxls.git

到此這篇關于SpringBoot快速集成jxls-poi(自定義模板,支持本地文件導出,在線文件導出)的文章就介紹到這了,更多相關SpringBoot集成jxls-poi內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
主站蜘蛛池模板: 亚洲影院中文字幕 | 亚洲国产成人影院播放 | 亚洲成人在线免费视频 | 国产欧美精品一区二区三区四区 | 亚洲精品亚洲人成人网 | 成人国产精品毛片 | 欧美成人亚洲高清在线观看 | 欧美在线黄 | 在线观看国产一区 | 性欧美video另类bd | 国产精品福利午夜h视频 | 美女的被男人桶爽网站 | 亚洲日韩aⅴ在线视频 | 欧美成人在线免费观看 | 欧美精品三区 | 另类自拍 | 亚洲系列第一页 | 国产高清在线精品一区 | 亚洲精品天堂一区在线观看 | 日本色中色 | 99久久精品国产亚洲 | 国产欧美日本亚洲精品五区 | 国产日韩欧美另类 | 经典国产一级毛片 | 亚洲高清视频免费 | 在线亚洲欧美日韩 | 99re热视频这里只精品 | 国产成人高清一区二区私人 | 特黄特黄aaaa级毛片免费看 | 日韩a级片| 亚洲久草在线 | 视频一区免费 | 成人综合在线视频 | 亚洲色欧美 | 免费看a毛片 | 日韩一级伦理片 | 久久在线影院 | 一级在线观看视频 | 欧美成人免费公开播放 | 玖玖精品视频在线观看 | 日本 亚洲 欧美 |