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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

easyexcel复杂模板导出(合并行列,列统计汇总)

發(fā)布時間:2024/3/13 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 easyexcel复杂模板导出(合并行列,列统计汇总) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

easyexcel復(fù)雜模板導(dǎo)出(合并行列,統(tǒng)計匯總)

  • 為什么使用easyexcel
    • 1. easyexcel可以通過模板導(dǎo)出(符合項目使用習慣)
    • 2. easyexcel支持大數(shù)據(jù)量導(dǎo)出,性能較好(滿足業(yè)務(wù)導(dǎo)出需求)
  • 切換時一個業(yè)務(wù)導(dǎo)出需求
    • 定義easyexcel模板
    • 實現(xiàn)效果
    • 代碼實現(xiàn)
  • 使用easyexcel遇到的問題
    • poi依賴沖突

為什么使用easyexcel

easyexcel官網(wǎng)地址: https://easyexcel.opensource.alibaba.com/docs/current/
項目之前一直使用Jxls進行excel導(dǎo)出,通過定義模板,導(dǎo)出時傳入對應(yīng)數(shù)據(jù)即可導(dǎo)出excel,使用起來還比較方便,項目上線1年之后,數(shù)據(jù)量越來越多,導(dǎo)出excel越來越慢,數(shù)據(jù)量再多點還會導(dǎo)致內(nèi)存溢出服務(wù)重啟,亟需優(yōu)化。
調(diào)研之后,發(fā)現(xiàn)easyexcel滿足如下兩點

1. easyexcel可以通過模板導(dǎo)出(符合項目使用習慣)

2. easyexcel支持大數(shù)據(jù)量導(dǎo)出,性能較好(滿足業(yè)務(wù)導(dǎo)出需求)

切換時一個業(yè)務(wù)導(dǎo)出需求

定義easyexcel模板

實現(xiàn)效果

代碼實現(xiàn)

pom依賴導(dǎo)入

<!-- excel --><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.2.1</version></dependency><!-- 解決jxls中poi版本沖突問題引入 --><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>4.1.2</version></dependency>

版本說明

合并策略類
目前只支持行合并,列合并對merge方法進行擴展即可

package com.servingcloud.factoring.utils.excel;import com.alibaba.excel.metadata.Head; import com.alibaba.excel.write.merge.AbstractMergeStrategy; import lombok.Data; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.util.CellRangeAddress;import java.util.List;/*** @author pengdy* @desc easyexcel合并行列導(dǎo)出*/ @Data public class ExcelFillCellMergeStrategy extends AbstractMergeStrategy {/*** 分組,每幾行合并一次*/private List<Integer> exportFieldGroupCountList;/*** 目標合并列index*/private Integer targetColumnIndex;// 需要開始合并單元格的首行indexprivate Integer rowIndex;public ExcelFillCellMergeStrategy(){}// exportDataList為待合并目標列的值public ExcelFillCellMergeStrategy(List<Integer> exportFieldGroupCountList, Integer targetColumnIndex) {this.exportFieldGroupCountList = exportFieldGroupCountList;this.targetColumnIndex = targetColumnIndex;}@Overrideprotected void merge(Sheet sheet, Cell cell, Head head, Integer integer) {if (null == rowIndex) {rowIndex = cell.getRowIndex();}// 只有合并的行與標記行相對應(yīng)時才進行合并if (cell.getRowIndex() == rowIndex && cell.getColumnIndex() == targetColumnIndex) {mergeGroupColumn(sheet);}}private void mergeGroupColumn(Sheet sheet) {int rowCount = rowIndex;for(Integer count:exportFieldGroupCountList){// 1行調(diào)用合并方法會報錯if(count == 1){rowCount += 1;continue;}// 合并單元格CellRangeAddress cellAddresses = new CellRangeAddress(rowCount,rowCount+count-1,targetColumnIndex,targetColumnIndex);sheet.addMergedRegion(cellAddresses);rowCount += count;}} }

實體對象:

package com.servingcloud.factoring.dto.response.vo.installment;import com.alibaba.excel.annotation.format.DateTimeFormat; import com.alibaba.excel.annotation.format.NumberFormat; import com.xintech.spacexcockroach.common.exception.BizException; import io.swagger.annotations.ApiModelProperty; import lombok.Data;import java.math.BigDecimal; import java.util.Date;/*** 中登登記發(fā)票轉(zhuǎn)讓清單* @author pengdy*/ @Data public class PledgeInvoiceITransferVO implements Cloneable {@ApiModelProperty("序號")private Integer index;@ApiModelProperty("資產(chǎn)編號")private String assetCode;@ApiModelProperty("應(yīng)收賬款轉(zhuǎn)讓編號")private String transferNumber;@ApiModelProperty("基礎(chǔ)交易合同名稱")private String contractName;@ApiModelProperty("基礎(chǔ)交易合同編號")private String contractCode;@ApiModelProperty("項目公司")private String projectOrgName;@ApiModelProperty("債權(quán)人")private String supplierName;@ApiModelProperty("發(fā)票編號")private String invoiceNo;@NumberFormat("#.##%")@ApiModelProperty("發(fā)票金額")private BigDecimal invoiceAmount;@NumberFormat("#.##%")@ApiModelProperty("發(fā)票轉(zhuǎn)讓金額")private BigDecimal invoiceTransAmount;@NumberFormat("#.##%")@ApiModelProperty("應(yīng)收賬款金額")private BigDecimal financeMoney;@DateTimeFormat("yyyy/MM/dd")@ApiModelProperty("賬款到期日")private Date applyDueDate;public PledgeInvoiceITransferVO clone(){PledgeInvoiceITransferVO transferVO;try {transferVO = (PledgeInvoiceITransferVO)super.clone();} catch (CloneNotSupportedException e) {throw new BizException("對象復(fù)制失敗。");}return transferVO;}}

service實現(xiàn)

@Overridepublic ResponseDTO<Void> downloadInstallmentMaterial(String code, String name, String shortName) {// 基礎(chǔ)數(shù)據(jù)List<PledgeInvoiceITransferVO> transferVOList = new ArrayList();transferVOList.add(data);// 合并行標記List<Integer> exportFieldGroupCountList = new ArrayList<>();List<PledgeInvoiceITransferVO> batchTransferVOS = assembleInvoice(transferVOList,exportFieldGroupCountList);String title = "中登附件表格-" + shortName + bizBatchGroupBO.getRefactoringContractSerialNumber() + "-" + bizBatchGroupBO.getGroupSerialNumber() + ".xlsx";Map<String, Object> map = new HashMap<>();// 統(tǒng)計對象PledgeInvoiceITransferVO count = new PledgeInvoiceITransferVO();count.setInvoiceNo("合計");// 發(fā)票金額合計count.setInvoiceAmount(batchTransferVOS.stream().map(PledgeInvoiceITransferVO::getInvoiceAmount).reduce(BigDecimal.ZERO,BigDecimal::add));// 發(fā)票轉(zhuǎn)讓金額合計count.setInvoiceTransAmount(batchTransferVOS.stream().map(PledgeInvoiceITransferVO::getInvoiceTransAmount).reduce(BigDecimal.ZERO,BigDecimal::add));// 應(yīng)收賬款金額合計count.setFinanceMoney(batchTransferVOS.stream().map(PledgeInvoiceITransferVO::getFinanceMoney).reduce(BigDecimal.ZERO,BigDecimal::add));batchTransferVOS.add(count);map.put("list", batchTransferVOS);String templatePath = "template/group/installmentInvoiceTransferList.xlsx";EasyExcelUtil.commonExportTest(templatePath, map, title, response,exportFieldGroupCountList,Arrays.asList(0,1,2,3,4,5,9,10));return new ResponseDTO<>(ResponseCode.OK);}/*** 組裝資產(chǎn)發(fā)票參數(shù)* @param transferVOList*/private List<PledgeInvoiceITransferVO> assembleInvoice(List<PledgeInvoiceITransferVO> transferVOList,List<Integer> exportFieldGroupCountList){List<PledgeInvoiceITransferVO> batchTransferVOS = new ArrayList<>();List<String> assetCodes = transferVOList.stream().map(transferVO -> transferVO.getAssetCode()).collect(Collectors.toList());List<SelectInvoiceListDTO> invoiceListDTOList = mock(assetCodes);Map<String,List<SelectInvoiceListDTO>> invoiceMap = invoiceListDTOList.stream().collect(Collectors.groupingBy(SelectInvoiceListDTO::getAssetCode,LinkedHashMap::new,Collectors.toList()));AtomicInteger index = new AtomicInteger(1);transferVOList.forEach(transferVO -> {List<SelectInvoiceListDTO> invoiceList = invoiceMap.get(transferVO.getAssetCode());for (SelectInvoiceListDTO selectInvoiceListDTO : invoiceList) {PledgeInvoiceITransferVO newVO = transferVO.clone();newVO.setIndex(index.get());newVO.setInvoiceNo(selectInvoiceListDTO.getInvoiceNo());newVO.setInvoiceAmount(selectInvoiceListDTO.getAmountWithTax());newVO.setInvoiceTransAmount(selectInvoiceListDTO.getAmountTransferred());batchTransferVOS.add(newVO);}index.getAndIncrement();exportFieldGroupCountList.add(invoiceList.size());});return batchTransferVOS;}/*** mock數(shù)據(jù)**/private List<SelectInvoiceListDTO> mock(List<String> assetCodes){List<SelectInvoiceListDTO> list = new ArrayList<>();for (String assetCode:assetCodes) {int num = RandomUtil.randomInt(10) + 1;for(int i=0;i<num;i++){SelectInvoiceListDTO dto = new SelectInvoiceListDTO();dto.setAssetCode(assetCode);dto.setInvoiceNo(assetCode + i);dto.setAmountWithTax(new BigDecimal(i));dto.setAmountTransferred(new BigDecimal(i));list.add(dto);}}return list;}

easyexcel導(dǎo)出工具類

/*** 合并導(dǎo)出* @param templatePath 模板地址* @param dataMap 數(shù)據(jù)* @param fileName 文件名* @param exportFieldGroupCountList 合并行長度集合* @param mergeColumn 合并列標記*/public static void mergeExport(String templatePath, Map<String, Object> dataMap, String fileName, HttpServletResponse response,List<Integer> exportFieldGroupCountList,List<Integer> mergeColumn){response.setContentType("application/octet-stream; charset=utf-8");response.setHeader("Content-Disposition", String.format("attachment;filename=%s",fileName));InputStream in = EasyExcelUtil.class.getClassLoader().getResourceAsStream(templatePath);try(ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(in).build()){ExcelWriterSheetBuilder builder = new ExcelWriterSheetBuilder();// 設(shè)置合并的列for(Integer col:mergeColumn){builder.registerWriteHandler(new ExcelFillCellMergeStrategy(exportFieldGroupCountList,col));}WriteSheet writeSheet = builder.build();excelWriter.fill(dataMap.get("list"),writeSheet);} catch (IOException e) {log.error("獲取文件流失敗",e);throw new BizException("文件下載失敗。");}}

使用easyexcel遇到的問題

poi依賴沖突

使用時出現(xiàn):NoSuchMethodException , ClassNotFoundException,
NoClassDefFoundError

解決:根據(jù)上文的版本說明進行匹配即可解決

總結(jié)

以上是生活随笔為你收集整理的easyexcel复杂模板导出(合并行列,列统计汇总)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日韩经典中文字幕 | 巨物撞击尤物少妇呻吟 | 亚洲情网 | 超污视频在线观看 | 韩国黄色大片 | 国产精品免费一区二区区 | 欧美a∨亚洲欧美亚洲 | 亚欧在线播放 | 99精彩视频 | 国产精品视频网址 | 中国爆后菊女人的视频 | 五月天综合 | 天天干夜夜艹 | 国产日韩亚洲 | jiz亚洲 | 欧美一级免费观看 | 亚洲911精品成人18网站 | 制服丝袜在线一区 | 国产精品乱码一区 | 免费一级特黄 | 国产香蕉尹人视频在线 | 亚洲中文字幕一区二区 | wwwxxxx在线观看 | 欧美激情一区二区在线 | 三年在线观看视频 | 秘密爱大尺度做爰呻吟 | 超碰视屏 | 欧美绿帽合集xxxxx | 国产精品夜色一区二区三区 | 国产男女猛烈无遮挡 | 少妇闺蜜换浪荡h肉辣文 | 欧美人一级淫片a免费播放 西方av在线 | 极品91尤物被啪到呻吟喷水 | 久久男人 | av丝袜在线观看 | 欧美人日b| 欧美日韩一区电影 | 国产精品久久网站 | 亚洲精品免费在线观看视频 | 日本欧美国产 | 中文字幕日韩在线视频 | 伊人网中文字幕 | 伊久久 | 911香蕉视频 | 超碰在线图片 | 久久婷婷影视 | 国产精品毛片一区二区在线看 | av影院在线| 黄色一极视频 | 制服丝袜一区在线 | 青青啪啪| 男男在线观看 | 草久久| 久草视频在线资源 | 日韩欧美网址 | 日韩性生活视频 | 久久国产精品国语对白 | 国产99色 | 久久久成人免费 | 青娱乐在线视频观看 | 日韩国产小视频 | 国产日韩91| 亚洲精品无码不卡在线播he | 粉嫩av一区二区三区免费观看 | 久久久青青 | 国产精品综合久久 | 色呦呦在线免费观看 | 中文字幕免费高清在线观看 | 亚拍一区 | 成人av社区 | jizz中国少妇高潮出水 | 激情爱爱网| 中文字幕亚洲欧美日韩在线不卡 | 亚洲xxxx视频 | 一本色道久久88 | 狠狠干在线 | 久久777| 男人撒尿视频xvideos | 欧美一级影院 | 久久精品大全 | 黄色片怎么看 | gav在线| 一级性爱视频 | 国产毛片自拍 | a视频免费 | 妖精视频污| 亚洲男人天堂网站 | 中文字幕成人av | www.狠狠爱 | 毛片www| 欧美乱强伦 | 青青草久久伊人 | 动漫美女视频 | 日韩一级久久 | 自拍偷拍一区 | 超碰国产91 | 欧美aaa级 | 精品视频在线一区 | av免费高清|