Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize jxls Multi-template and Multi-sheet Export excel File

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the "how to achieve jxls multi-template multi-sheet export excel file" related knowledge, in the actual case operation process, many people will encounter such a dilemma, then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

1. Tool class

Public static boolean exportMultSheetExcel (InputStream is, OutputStream os, Map model, Map sheet) {Context context = PoiTransformer.createInitialContext (); JxlsHelper jxlsHelper = JxlsHelper.getInstance (); Transformer transformer = jxlsHelper.createTransformer (is, os); AreaBuilder areaBuilder = new XlsCommentAreaBuilder (transformer); if (model! = null) {for (String key: model.keySet ()) {context.putVar (key, model.get (key)) }} try {/ / get configuration JexlExpressionEvaluator evaluator = (JexlExpressionEvaluator) transformer.getTransformationConfig (). GetExpressionEvaluator (); Map functionMap = new HashMap (); functionMap.put ("utils", new JxlsUtils ()); evaluator.getJexlEngine () .setFunctions (functionMap); List xlsAreaList = areaBuilder.build () If (sheet! = null) {for (Integer index: sheet.keySet ()) {Area xlsArea = xlsAreaList.get (index); xlsArea.applyAt (new CellRef (sheet.get (index) + "! A1"), context);} transformer.write (); is.close () } catch (Exception e) {log.error ("bulk write file error", e); return false;} return true;}

two。 Interface call

/ / get template file String tplfileName = "multi_sheet_tpl.xlsx"; String tmpFileName = FileUtil.getTmpFileName (cName+ "and" + gName); String tmpFilePath = FileUtils.getTempDirectoryPath () + File.separator + tmpFileName; / / write target file log.info ("start writing target file tplfileName: {}, tmpFileName: {}", tplfileName, tmpFileName) OutputStream os = new FileOutputStream (tmpFilePath); InputStream inputStream = FileUtil.getFileTplInputStream (tplfileName); if (os! = null & & tplfileName! = null) {Map model = new HashMap (); / / sheet1 model.put ("cGasOrders", list) / / sheet2 model.put ("cName", cName); Map sheet = new HashMap (); sheet.put (0, "reconciliation"); sheet.put (1, "reconciliation details"); JxlsUtils.exportMultSheetExcel (inputStream, os, model, sheet) Os.close ();}

Construction of 3.area

JXLS builds Area,jXLS using the AreaBuilder interface to provide two implementation classes: XmlAreaBuilder and XlsCommentAreaBuilder.

XmlAreaBuilder is based on the XML configuration file to build Area,XlsCommentAreaBuilder is to build Area based on cell comments in the Excel template. By default, jXLS uses XlsCommentAreaBuilder to build Area. You can toggle the way Area is built through the JxlsHelper.setAreaBuilder () method. In fact, both build methods use Java API to build Area based on parsing their respective configurations.

4. Summary

The most important point of the implementation is the writing of the tool class, which can be directly called after writing the tool class. Multi-template is mainly based on a single template with more area division, and each sheet has different regional division of different templates. After writing, it can directly transmit data to achieve multi-template and multi-sheet export.

"how to achieve jxls multi-template multi-sheet export excel file" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report