当前位置: 首页 > 后端技术 > Java

【笑小枫的SpringBoot系列】【十三】JAVA使用EasyExcel导出excel

时间:2023-04-01 16:49:52 Java

鍔熻兘鑳屾櫙绠€鍗曠殑璇翠笅杩欎釜鍔熻兘鐨勮儗鏅渶姹傚惂锛屾湁绫讳技闇€姹傜殑鍙互澶嶇敤锛屾灉鐒跺鍏ヨ繕娌″啓瀹岋紝瀵煎嚭鐨勫姛鑳芥帴韪佃€屾潵锛屼竴鍧楀啓浜嗗惂瀹炵幇excel瀵煎嚭锛堜緷鏃у簾璇?..锛夊涓猻heet椤典竴璧峰鍑虹涓€涓猻heet椤垫暟鎹〃澶翠俊鎭湁涓よ鏍峰紡绋嶅井缇庤锛屽垪瀹藉彲浠ヨ嚜瀹氫箟绛夋暟鎹噺绋嶅井鏈変簺澶э紙澶氫釜sheet椤垫€婚噺50w宸﹀彸锛夐」鐩紩鍏ヤ緷璧栧鏋滄槸浠庝笂涓€绡囩湅杩囨潵鐨勶紝灏变笉鐢ㄧ湅椤圭洰寮曞叆浜嗮煓坓radle:compile "com.alibaba:easyexcel:3.1.0"maven:<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.1.0</version></dependency>娉ㄦ剰锛?3+鐗堟湰鐨勭殑easyexcel锛屼娇鐢╬oi 5+鐗堟湰鏃讹紝闇€瑕佹墜鍔ㄦ帓闄わ細poi-ooxml-schemas锛屼緥濡傦細<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.1.0</version> <exclusions> <exclusion> <artifactId>poi-ooxml-schemas</artifactId> <groupId>org.apache.poi</groupId> </exclusion> </exclusions></dependency>椤圭洰缂栫爜濡傚涓〃鏍兼牱寮忕浉鍚岋紝鍙紪鍐欎竴涓埗绫伙紝灏嗘牱寮忓畾涔夊湪鐖剁被涓婏紝瀛愮被缁ф壙鐖剁被鍗冲彲銆傚湪config.bean.excel鍖呬笅瀹氫箟缁忛攢鍟嗕俊鎭璞xportCompany.java锛屼唬鐮佸涓嬸煈噋ackage com.maple.demo.config.bean.excel;import com.alibaba.excel.annotation.ExcelProperty;import com.alibaba.excel.annotation.write.style.ColumnWidth;import com.alibaba.excel.annotation.write.style.ContentFontStyle;import com.alibaba.excel.annotation.write.style.HeadFontStyle;import com.alibaba.excel.annotation.write.style.HeadStyle;import com.alibaba.excel.enums.poi.FillPatternTypeEnum;import lombok.AllArgsConstructor;import lombok.Builder;import lombok.Data;import lombok.NoArgsConstructor;import java.util.Date;/** * @author 绗戝皬鏋?* @date 2022/7/22 */@Data@Builder@NoArgsConstructor@AllArgsConstructor@HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 40)@HeadFontStyle(fontHeightInPoints = 12)@ContentFontStyle(fontHeightInPoints = 11)@ColumnWidth(20)public class ExportCompany { // -------------------- 鍩烘湰淇℃伅 start ------------- @ExcelProperty({"鍩烘湰淇℃伅", "鍏徃鍚嶇О"}) private String companyName; @ExcelProperty({"鍩烘湰淇℃伅", "鐪佷唤"}) private String province; @ExcelProperty({"鍩烘湰淇℃伅", "鎴愮珛鏃堕棿"}) private Date startDate; @ExcelProperty({"鍩烘湰淇℃伅", "浼佷笟鐘舵€?quot;}) private String entStatus; @ColumnWidth(30) @ExcelProperty({"鍩烘湰淇℃伅", "鍗氬鍦板潃"}) private String csdnAddress; // ---------------- 鍩烘湰淇℃伅 end --------------------- // ---------------- 缁忚惀淇℃伅 start --------------------- @ExcelProperty({"缁忚惀淇℃伅", "鍛樺伐鏁?quot;}) private String employeeMaxCount; @ExcelProperty({"缁忚惀淇℃伅", "缃戠珯鍦板潃"}) private String netAddress; @ExcelProperty({"缁忚惀淇℃伅", "鎵€灞炲尯鍩熺渷"}) private String businessProvinceName; @ExcelProperty({"缁忚惀淇℃伅", "鎵€灞炲尯鍩熷競"}) private String businessCityName; @ExcelProperty({"缁忚惀淇℃伅", "鎵€灞炲尯鍩熷尯鍘?quot;}) private String businessAreaName; // ---------------- 缁忚惀淇℃伅 end ---------------------}鍦╟onfig.bean.excel鍖呬笅瀹氫箟鑱旂郴浜轰俊鎭璞xportContact.java锛屼唬鐮佸涓嬸煈噋ackage com.maple.demo.config.bean.excel;import com.alibaba.excel.annotation.ExcelProperty;import com.alibaba.excel.annotation.write.style.ColumnWidth;import com.alibaba.excel.annotation.write.style.ContentFontStyle;import com.alibaba.excel.annotation.write.style.HeadFontStyle;import com.alibaba.excel.annotation.write.style.HeadStyle;import com.alibaba.excel.enums.poi.FillPatternTypeEnum;import lombok.AllArgsConstructor;import lombok.Builder;import lombok.Data;import lombok.NoArgsConstructor;/** * @author 绗戝皬鏋?* @date 2022/7/22 */@Data@Builder@NoArgsConstructor@AllArgsConstructor@HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 40)@HeadFontStyle(fontHeightInPoints = 12)@ContentFontStyle(fontHeightInPoints = 11)@ColumnWidth(20)public class ExportContact { @ExcelProperty("鍏徃鍚嶇О") private String companyName; @ExcelProperty("濮撳悕") private String name; @ExcelProperty("韬唤璇佸彿鐮?quot;) private String idCard; @ExcelProperty("鐢佃瘽鍙风爜") private String mobile; @ExcelProperty("鑱屼綅") private String contactPostName;}鍦╟ontroller鍖呬笅缂栧啓TestExportExcelController.java杩涜娴嬭瘯锛屼唬鐮佸涓嬶細package com.maple.demo.controller;import com.alibaba.excel.EasyExcelFactory;import com.alibaba.excel.ExcelWriter;import com.alibaba.excel.write.metadata.WriteSheet;import com.maple.demo.config.bean.excel.ExportCompany;import com.maple.demo.config.bean.excel.ExportContact;import io.swagger.annotations.Api;import lombok.extern.slf4j.Slf4j;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletResponse;import java.io.OutputStream;import java.net.URLEncoder;import java.util.ArrayList;import java.util.Date;import java.util.List;/** * @author 绗戝皬鏋?* @date 2022/7/22 */@Slf4j@RestController@RequestMapping("/example")@Api(tags = "瀹炰緥婕旂ず-瀵煎嚭Excel")public class TestExportExcelController { @GetMapping("/exportExcel") public void exportExcel(HttpServletResponse response) { try (OutputStream out = response.getOutputStream()) { response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); String fileName = URLEncoder.encode("绗戝皬鏋祴璇曞鍑?quot;, "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); handleExcel(out); out.flush(); } catch (Exception e) { log.error(e.getMessage()); } } private void handleExcel(OutputStream out) { try (ExcelWriter excelWriter = EasyExcelFactory.write(out).build()) { WriteSheet dealerSheet = EasyExcelFactory.writerSheet(0, "缁忛攢鍟嗕俊鎭?quot;).head(ExportCompany.class).build(); WriteSheet contactSheet = EasyExcelFactory.writerSheet(1, "鑱旂郴浜?quot;).head(ExportContact.class).build(); excelWriter.write(getCompany(), dealerSheet); excelWriter.write(getContact(), contactSheet); } } private List<ExportCompany> getCompany() { List<ExportCompany> companyList = new ArrayList<>(); for (int i = 0; i < 5; i++) { companyList.add(ExportCompany.builder() .companyName("绗戝皬鏋叕鍙?quot; + i) .province("涓婃捣甯?quot;) .businessProvinceName("灞变笢鐪?quot;) .businessCityName("涓存矀甯?quot;) .businessAreaName("娌充笢鍖?quot;) .entStatus("钀ヤ笟") .netAddress("www.xiaoxiaofeng.site") .csdnAddress("https://zhangfz.blog.csdn.net") .employeeMaxCount("100") .startDate(new Date()) .build()); } return companyList; } private List<ExportContact> getContact() { List<ExportContact> contactList = new ArrayList<>(); for (int i = 0; i < 5; i++) { contactList.add(ExportContact.builder() .companyName("绗戝皬鏋叕鍙?quot; + i) .name("绗戝皬鏋?quot; + i) .mobile("183000000000") .idCard("371324199011111111") .contactPostName("鍚庣") .build()); } return contactList; }}娴嬭瘯缁撴灉娴忚鍣ㄨ姹傦細http://localhost:8080/example/exportExcel缁忛攢鍟唖heet椤典俊鎭細鑱旂郴浜簊heet椤典俊鎭細鐩稿叧灞炴€цВ璇绘敞瑙xcelProperty index 鎸囧畾鍐欏埌绗嚑鍒楋紝榛樿鏍规嵁鎴愬憳鍙橀噺鎺掑簭銆倂alue鎸囧畾鍐欏叆鐨勫悕绉帮紝榛樿鎴愬憳鍙橀噺鐨勫悕瀛楋紝澶氫釜value鍙互鍙傜収蹇€熷紑濮嬩腑鐨勫鏉傚ごExcelIgnore 榛樿鎵€鏈夊瓧娈甸兘浼氬啓鍏xcel锛岃繖涓敞瑙d細蹇界暐杩欎釜瀛楁DateTimeFormat 鏃ユ湡杞崲锛屽皢Date鍐欏埌excel浼氳皟鐢ㄨ繖涓敞瑙c€傞噷闈㈢殑value鍙傜収java.text.SimpleDateFormatNumberFormat 鏁板瓧杞崲锛岀敤Number鍐檈xcel浼氳皟鐢ㄨ繖涓敞瑙c€傞噷闈㈢殑value鍙傜収java.text.DecimalFormatExcelIgnoreUnannotated 榛樿涓嶅姞ExcelProperty 鐨勬敞瑙g殑閮戒細鍙備笌璇诲啓锛屽姞浜嗕笉浼氬弬涓庡弬鏁伴€氱敤鍙傛暟WriteWorkbook,WriteSheet ,WriteTable閮戒細鏈夌殑鍙傛暟锛屽鏋滀负绌猴紝榛樿浣跨敤涓婄骇銆俢onverter 杞崲鍣紝榛樿鍔犺浇浜嗗緢澶氳浆鎹㈠櫒銆備篃鍙互鑷畾涔夈€倃riteHandler 鍐欑殑澶勭悊鍣ㄣ€傚彲浠ュ疄鐜癢orkbookWriteHandler,SheetWriteHandler,RowWriteHandler,CellWriteHandler锛屽湪鍐欏叆excel鐨勪笉鍚岄樁娈典細璋冪敤relativeHeadRowIndex 璺濈澶氬皯琛屽悗寮€濮嬨€備篃灏辨槸寮€澶寸┖鍑犺needHead 鏄惁瀵煎嚭澶磆ead 涓巆lazz浜岄€変竴銆傚啓鍏ユ枃浠剁殑澶村垪琛紝寤鸿浣跨敤class銆俢lazz 涓巋ead浜岄€変竴銆傚啓鍏ユ枃浠剁殑澶村搴旂殑class锛屼篃鍙互浣跨敤娉ㄨВ銆俛utoTrim 瀛楃涓层€佽〃澶寸瓑鏁版嵁鑷姩trimWriteWorkbook锛堢悊瑙f垚excel瀵硅薄锛夊弬鏁癳xcelType 褰撳墠excel鐨勭被鍨?榛樿xlsxoutputStream 涓巉ile浜岄€変竴銆傚啓鍏ユ枃浠剁殑娴乫ile 涓巓utputStream浜岄€変竴銆傚啓鍏ョ殑鏂囦欢templateInputStream 妯℃澘鐨勬枃浠舵祦templateFile 妯℃澘鏂囦欢autoCloseStream 鑷姩鍏抽棴娴併€俻assword 鍐欑殑鏃跺€欐槸鍚﹂渶瑕佷娇鐢ㄥ瘑鐮乽seDefaultStyle 鍐欑殑鏃跺€欐槸鍚︽槸浣跨敤榛樿澶碬riteSheet锛堝氨鏄痚xcel鐨勪竴涓猄heet锛夊弬鏁皊heetNo 闇€瑕佸啓鍏ョ殑缂栫爜銆傞粯璁?sheetName 闇€瑕佷簺鐨凷heet鍚嶇О锛岄粯璁ゅ悓sheetNoWriteTable锛堝氨鎶奺xcel鐨勪竴涓猄heet,涓€鍧楀尯鍩熺湅涓€涓猼able锛夊弬鏁皌ableNo 闇€瑕佸啓鍏ョ殑缂栫爜銆傞粯璁?鍐欏湪鏈€鍚庢湰鏂囧彧鏄敤鍒伴儴鍒嗗姛鑳斤紝绠€鍗曠殑鍋氫簡涓€涓嬫€荤粨锛屾洿澶氱殑鍔熻兘锛屽彲浠ュ幓瀹樼綉鏌ラ槄銆傚畼鏂规枃妗o細https://www.yuque.com/easyexcel/doc/read浣跨敤EasyExcel瀵煎叆excel锛歨ttps://www.xiaoxiaofeng.com/archives/springboot12鍏充簬绗戝皬鏋煉曟湰绔犲埌杩欓噷缁撴潫浜嗭紝鍠滄鐨勬湅鍙嬪叧娉ㄤ竴涓嬫垜鍛︷煒橉煒樺井淇″叕浼楀彿锛氱瑧灏忔灚鏈枃婧愮爜锛歨ttps://github.com/hack-feng/maple-demo