今天就跟大家聊聊有關(guān)SpringBoot中怎么利用easyexcel導(dǎo)出Excel,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
網(wǎng)站的建設(shè)創(chuàng)新互聯(lián)建站專注網(wǎng)站定制,經(jīng)驗豐富,不做模板,主營網(wǎng)站定制開發(fā).小程序定制開發(fā),H5頁面制作!給你煥然一新的設(shè)計體驗!已為成都塑料袋等企業(yè)提供專業(yè)服務(wù)。
首先我們創(chuàng)建一個springboot(版本是 2.1.4.RELEASE)項目,在此就不過多的啰嗦,創(chuàng)建好之后,首先需要引入easyexcel的maven坐標(biāo)。
com.alibaba easyexcel 1.1.2-beta5
導(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。 * 可以通過實例獲取該值 */ private List
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 實體類映射,繼承 BaseRowModel 類 * @return Excel 數(shù)據(jù) list */ public static ListreadExcel(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 實體類映射,繼承 BaseRowModel 類 * @param sheetNo sheet 的序號 從1開始 * @return Excel 數(shù)據(jù) list */ public static List readExcel(MultipartFile excel, BaseRowModel rowModel, int sheetNo) { return readExcel(excel, rowModel, sheetNo, 1); } /** * 讀取某個 sheet 的 Excel * * @param excel 文件 * @param rowModel 實體類映射,繼承 BaseRowModel 類 * @param sheetNo sheet 的序號 從1開始 * @param headLineNum 表頭行數(shù),默認(rèn)為1 * @return Excel 數(shù)據(jù) list */ public static List 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 映射實體類,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 映射實體類,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)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。