您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)SpringBoot中怎么利用easyexcel導(dǎo)出Excel,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
首先我們創(chuàng)建一個springboot(版本是 2.1.4.RELEASE)項(xiàng)目,在此就不過多的啰嗦,創(chuàng)建好之后,首先需要引入easyexcel的maven坐標(biāo)。
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>1.1.2-beta5</version> </dependency>
導(dǎo)入好了之后,我們接下來需要創(chuàng)建一個導(dǎo)出的模板類,首先要集成BaseRowModel,set、get省略,@ExcelProperty注解中的value就是表頭的信息,index是在第幾列,沒有加注解的不會導(dǎo)出。
public class OrderExcelBO extends BaseRowModel { @ExcelProperty(value = {"訂單ID"}, index = 0) private String id; /** * 訂單描述 */ @ExcelProperty(value = {"訂單描述"}, index = 2) private String description; /** * 訂單對應(yīng)產(chǎn)品id */ @ExcelProperty(value = {"產(chǎn)品ID"}, index = 2) private Integer productId; /** * 支付方式描述,如:apple pay */ @ExcelProperty(value = {"支付方式"}, index = 3) private String payMethod; /** * create_time */ @ExcelProperty(value = {"時間"}, index = 4) private String createTime; /** * update_time */ private String updateTime; /** * 產(chǎn)生訂單的用戶 */ @ExcelProperty(value = {"用戶ID"}, index = 5) private Integer userId; /** * 支付狀態(tài):0 未支付、1支付成功支付完成、-1支付失敗 */ @ExcelProperty(value = {"支付狀態(tài)"}, index = 6) private String status; /** * 訂單來源描述,如:ios 安卓 */ @ExcelProperty(value = {"手機(jī)型號"}, index = 7) private String platform; /** * 訂單流水 */ @ExcelProperty(value = {"訂單流水號"}, index = 8) private String flowNum; /** * 訂單金額 */ @ExcelProperty(value = {"金額"}, index = 9) private BigDecimal price; // @ExcelProperty(value = {"收據(jù)字段"}, index = 10) private String receipt; @ExcelProperty(value = {"APP來源"}, index = 10) private String sources; }
導(dǎo)出的模板定義好之后,接下來就是一些封裝好的工具類的調(diào)用
查出我們需要導(dǎo)出的數(shù)據(jù);
生成Excel文件名和sheet名稱;
直接調(diào)用封裝好的工具類導(dǎo)出文件即可;
我們來看下導(dǎo)出的效果
如果你的表頭比較復(fù)雜,那么根據(jù)需求,你也可自行定義,例如如下這種復(fù)雜的表頭,應(yīng)該如何設(shè)置
首先要修改模板類,如果合并的單元格最大為2,那么所有的表格都需要設(shè)置為2,不合并的單元格用空字符串填充,需要合并的單元格將合并部分寫上相同的名稱,并且排列的序號要連續(xù),不能分開。
我們來看下導(dǎo)出的效果,這樣就可以滿足我們平時開發(fā)需要的excel導(dǎo)出功能。簡單易上手。
工具類:
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import java.util.ArrayList; import java.util.List; public class ExcelListener extends AnalysisEventListener { /** * 自定義用于暫時存儲data。 * 可以通過實(shí)例獲取該值 */ private List<Object> datas = new ArrayList<>(); /** * 通過 AnalysisContext 對象還可以獲取當(dāng)前 sheet,當(dāng)前行等數(shù)據(jù) */ @Override public void invoke(Object object, AnalysisContext context) { //數(shù)據(jù)存儲到list,供批量處理,或后續(xù)自己業(yè)務(wù)邏輯處理。 datas.add(object); //根據(jù)業(yè)務(wù)自行 do something doSomething(); /* 如數(shù)據(jù)過大,可以進(jìn)行定量分批處理 if(datas.size()<=100){ datas.add(object); }else { doSomething(); datas = new ArrayList<Object>(); } */ } /** * 根據(jù)業(yè)務(wù)自行實(shí)現(xiàn)該方法 */ private void doSomething() { } @Override public void doAfterAllAnalysed(AnalysisContext context) { /* datas.clear(); 解析結(jié)束銷毀不用的資源 */ } public List<Object> getDatas() { return datas; } public void setDatas(List<Object> datas) { this.datas = datas; } }
import com.alibaba.excel.ExcelReader; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.metadata.BaseRowModel; import com.alibaba.excel.metadata.Font; import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.metadata.TableStyle; import com.alibaba.excel.support.ExcelTypeEnum; import com.mochu.exception.ExcelException; import org.apache.poi.poifs.filesystem.FileMagic; import org.apache.poi.ss.usermodel.IndexedColors; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.List; public class ExcelUtil { /** * 讀取 Excel(多個 sheet) * * @param excel 文件 * @param rowModel 實(shí)體類映射,繼承 BaseRowModel 類 * @return Excel 數(shù)據(jù) list */ public static List<Object> readExcel(MultipartFile excel, BaseRowModel rowModel) { ExcelListener excelListener = new ExcelListener(); ExcelReader reader = getReader(excel, excelListener); if (reader == null) { return null; } for (Sheet sheet : reader.getSheets()) { if (rowModel != null) { sheet.setClazz(rowModel.getClass()); } reader.read(sheet); } return excelListener.getDatas(); } /** * 讀取某個 sheet 的 Excel * * @param excel 文件 * @param rowModel 實(shí)體類映射,繼承 BaseRowModel 類 * @param sheetNo sheet 的序號 從1開始 * @return Excel 數(shù)據(jù) list */ public static List<Object> readExcel(MultipartFile excel, BaseRowModel rowModel, int sheetNo) { return readExcel(excel, rowModel, sheetNo, 1); } /** * 讀取某個 sheet 的 Excel * * @param excel 文件 * @param rowModel 實(shí)體類映射,繼承 BaseRowModel 類 * @param sheetNo sheet 的序號 從1開始 * @param headLineNum 表頭行數(shù),默認(rèn)為1 * @return Excel 數(shù)據(jù) list */ public static List<Object> readExcel(MultipartFile excel, BaseRowModel rowModel, int sheetNo, int headLineNum) { ExcelListener excelListener = new ExcelListener(); ExcelReader reader = getReader(excel, excelListener); if (reader == null) { return null; } reader.read(new Sheet(sheetNo, headLineNum, rowModel.getClass())); return excelListener.getDatas(); } /** * 導(dǎo)出 Excel :一個 sheet,帶表頭 * * @param response HttpServletResponse * @param list 數(shù)據(jù) list,每個元素為一個 BaseRowModel * @param fileName 導(dǎo)出的文件名 * @param sheetName 導(dǎo)入文件的 sheet 名 * @param object 映射實(shí)體類,Excel 模型 */ public static void writeExcel(HttpServletResponse response, List<? extends BaseRowModel> list, String fileName, String sheetName, BaseRowModel object) { ExcelWriter writer = new ExcelWriter(getOutputStream(fileName, response), ExcelTypeEnum.XLSX); Sheet sheet = new Sheet(1, 0, object.getClass()); sheet.setSheetName(sheetName); TableStyle tableStyle = new TableStyle(); tableStyle.setTableContentBackGroundColor(IndexedColors.WHITE); Font font = new Font(); font.setFontHeightInPoints((short) 9); tableStyle.setTableHeadFont(font); tableStyle.setTableContentFont(font); sheet.setTableStyle(tableStyle); writer.write(list, sheet); writer.finish(); } /** * 導(dǎo)出 Excel :多個 sheet,帶表頭 * * @param response HttpServletResponse * @param list 數(shù)據(jù) list,每個元素為一個 BaseRowModel * @param fileName 導(dǎo)出的文件名 * @param sheetName 導(dǎo)入文件的 sheet 名 * @param object 映射實(shí)體類,Excel 模型 */ public static ExcelWriterFactory writeExcelWithSheets(HttpServletResponse response, List<? extends BaseRowModel> list, String fileName, String sheetName, BaseRowModel object) { ExcelWriterFactory writer = new ExcelWriterFactory(getOutputStream(fileName, response), ExcelTypeEnum.XLSX); Sheet sheet = new Sheet(1, 0, object.getClass()); sheet.setSheetName(sheetName); sheet.setTableStyle(getTableStyle()); writer.write(list, sheet); return writer; } /** * 導(dǎo)出融資還款情況表 * * @param response * @param list * @param fileName * @param sheetName * @param object */ public static void writeFinanceRepayment(HttpServletResponse response, List<? extends BaseRowModel> list, String fileName, String sheetName, BaseRowModel object) { ExcelWriter writer = new ExcelWriter(getOutputStream(fileName, response), ExcelTypeEnum.XLSX); Sheet sheet = new Sheet(1, 0, object.getClass()); sheet.setSheetName(sheetName); sheet.setTableStyle(getTableStyle()); writer.write(list, sheet); for (int i = 1; i <= list.size(); i += 4) { writer.merge(i, i + 3, 0, 0); writer.merge(i, i + 3, 1, 1); } writer.finish(); } /** * 導(dǎo)出文件時為Writer生成OutputStream */ private static OutputStream getOutputStream(String fileName, HttpServletResponse response) { //創(chuàng)建本地文件 fileName = fileName + ".xls"; try { fileName = new String(fileName.getBytes(), "ISO-8859-1"); response.addHeader("Content-Disposition", "filename=">
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.metadata.BaseRowModel; import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.support.ExcelTypeEnum; import java.io.IOException; import java.io.OutputStream; import java.util.List; public class ExcelWriterFactory extends ExcelWriter { private OutputStream outputStream; private int sheetNo = 1; public ExcelWriterFactory(OutputStream outputStream, ExcelTypeEnum typeEnum) { super(outputStream, typeEnum); this.outputStream = outputStream; } public ExcelWriterFactory write(List<? extends BaseRowModel> list, String sheetName, BaseRowModel object) { this.sheetNo++; try { Sheet sheet = new Sheet(sheetNo, 0, object.getClass()); sheet.setSheetName(sheetName); this.write(list, sheet); } catch(Exception ex) { ex.printStackTrace(); try { outputStream.flush(); } catch(IOException e) { e.printStackTrace(); } } return this; } @Override public void finish() { super.finish(); try { outputStream.flush(); } catch(IOException e) { e.printStackTrace(); } } }
看完上述內(nèi)容,你們對SpringBoot中怎么利用easyexcel導(dǎo)出Excel有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。