Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the Export function of complex excel File by Easypoi

Shulou Source: shulou.com Published: 2022-06-02 09:48:25 09月12日 Update

This article introduces the knowledge of "how Easypoi implements the export function of complex excel files". In the operation of actual cases, many people will encounter such a dilemma, so 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!

The reason why I use Easypoi is that it can directly fill the data based on the existing template to achieve excel generation, and the template product has been given, so you only need to make minor modifications to use it. The modified template is shown in the figure:

Note that {{$fe: is to loop through listmap variables to populate data rows, while others are similar placeholders, which are easy to understand

/ / the code simply corresponds the placeholder of the template to the variable Map map = new HashMap (); map.put ("code", customer.getCode ()); map.put ("name", customer.getName ()); map.put ("mobile", customer.getMobile ()); map.put ("membershipGrade", customer.getMembershipGrade ()); map.put ("zzyMembershipGrade", customer.getZzyMembershipGrade ()) Map.put ("addressComplete", customer.getAddressComplete ()); List listMap = new ArrayList (); map.put ("maplist", listMap); SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); for (MainOrderSubOrderFinanceResult financeResult: mainOrderSubOrderFinanceResults) {Map lm = new HashMap (); lm.put ("orderCode", financeResult.getOrderCode ()) Lm.put ("payStatusDesc", financeResult.getPayStatusDesc ()); lm.put ("settlementTypeDesc", financeResult.getSettlementTypeDesc ()); lm.put ("billDate", dateFormat.format (financeResult.getBillDate (); lm.put ("totalAmount", financeResult.getTotalAmount (). StripTrailingZeros (). ToPlainString ()); lm.put ("discountAmount", financeResult.getDiscountAmount (). StripTrailingZeros (). ToPlainString ()) Lm.put ("amount", financeResult.getAmount (). StripTrailingZeros (). ToPlainString ()); lm.put ("payAmount", financeResult.getPayAmount (). StripTrailingZeros (). ToPlainString ()); lm.put ("amountPaid", financeResult.getAmountPaid (). StripTrailingZeros (). ToPlainString ()); listMap.add (lm); sumTotalAmount = sumTotalAmount.add (financeResult.getTotalAmount ()) SumDiscountAmount = sumDiscountAmount.add (financeResult.getDiscountAmount ()); sumAmount = sumAmount.add (financeResult.getAmount ()); sumPayAmount = sumPayAmount.add (financeResult.getPayAmount ()); sumAmountPaid = sumAmountPaid.add (financeResult.getAmountPaid ());} map.put ("sumTotalAmount", sumTotalAmount); map.put ("sumDiscountAmount", sumDiscountAmount); map.put ("sumAmount", sumAmount) Map.put ("sumPayAmount", sumPayAmount); map.put ("sumAmountPaid", sumAmountPaid); / / generate temporary template files String url= ""; try {File temp = File.createTempFile ("exportCreditBillResult", ".xlsx"); FileUploadHelper.inputStream2File (this.getClass (). GetClassLoader (). GetResourceAsStream ("exportCreditBillResult.xlsx"), temp); url=temp.getAbsolutePath () Temp.deleteOnExit ();} catch (IOException e) {} TemplateExportParams params = new TemplateExportParams (url); Workbook workbook = ExcelExportUtil.exportExcel (params, map)

There is a key point to do this function:

Since the TemplateExportParams constructor only supports the incoming file path, and the template file path directly passed into the resource will have a problem in the online environment (no problem locally), so you need to use getResourceAsStream to read and generate a temporary file, and then pass in the temporary file url

This is the end of the introduction of "how Easypoi implements the export function of complex excel files". Thank you for your 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!

Tags: File template function complexity next content variable data more knowledge path problem generation utility achievement reason product code key key point Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL vpn Microsoft Xiaomi Linux