使用easyExcel导出excel数据案例
生活随笔
收集整理的這篇文章主要介紹了
使用easyExcel导出excel数据案例
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
easyExcel簡(jiǎn)介:
Java領(lǐng)域解析、生成Excel比較有名的框架有Apache poi、jxl等。但他們都存在一個(gè)嚴(yán)重的問(wèn)題就是非常的耗內(nèi)存。如果你的系統(tǒng)并發(fā)量不大的話可能還行,但是一旦并發(fā)上來(lái)后一定會(huì)OOM或者JVM頻繁的full gc。
easyExcel是阿里巴巴開(kāi)源的一個(gè)excel處理框架,以使用簡(jiǎn)單、節(jié)省內(nèi)存著稱。
easyExcel采用一行一行的解析模式,并將一行的解析結(jié)果以觀察者的模式通知處理
easyExcel能大大減少占用內(nèi)存的主要原因是在解析Excel時(shí)沒(méi)有將文件數(shù)據(jù)一次性全部加載到內(nèi)存中,而是從磁盤(pán)上一行行讀取數(shù)據(jù),逐個(gè)解析。
1.導(dǎo)入依賴【poi不能低于3.17,不然可能會(huì)報(bào)錯(cuò)】
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.17</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>1.1.2-beta5</version></dependency>2.控制層
/*** 導(dǎo)出學(xué)生繳費(fèi)信息* @param response*/@RequestMapping("/exportExcel")public void easyDownload(HttpServletResponse response,String gradeBid){//查詢學(xué)生信息List<ExportModel> list = importExcelService.getStudentExportList(gradeBid);try {OutputStream out = getOutputStream(response, "淮南二中學(xué)籍管理學(xué)生繳費(fèi)信息", ExcelTypeEnum.XLSX);//這里指定需要表頭,因?yàn)閙odel通常包含表信頭息ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);//寫(xiě)第一個(gè)sheet,數(shù)據(jù)全是List<String> 無(wú)模型映射關(guān)系Sheet sheet = new Sheet(1, 0,ExportModel.class);//設(shè)置自適應(yīng)寬度sheet.setAutoWidth(Boolean.TRUE);//設(shè)置表格樣式sheet.setTableStyle(createTableStyle());//設(shè)置sheetNamesheet.setSheetName("淮南二中學(xué)籍管理學(xué)生繳費(fèi)信息");long start = System.currentTimeMillis() / 1000;//單位秒//寫(xiě)數(shù)據(jù)writer.write(list, sheet);//關(guān)閉writer的輸出流writer.finish();long end = System.currentTimeMillis() / 1000;System.out.println("導(dǎo)出耗時(shí):" + (end - start) +" 秒");} catch (Exception e) {}}/*** 設(shè)置表格樣式* @return*/private TableStyle createTableStyle() {TableStyle tableStyle = new TableStyle();Font headFont = new Font();headFont.setBold(true);headFont.setFontHeightInPoints((short) 20);headFont.setFontName("楷體");tableStyle.setTableHeadFont(headFont);tableStyle.setTableHeadBackGroundColor(IndexedColors.LIGHT_GREEN);Font contentFont = new Font();contentFont.setFontHeightInPoints((short) 12);contentFont.setFontName("黑體");tableStyle.setTableContentFont(contentFont);return tableStyle;}/*** 得到流* @param response 響應(yīng)* @param fileName 文件名* @param excelTypeEnum excel類型* @return*/private OutputStream getOutputStream(HttpServletResponse response, String fileName,ExcelTypeEnum excelTypeEnum) {try {// 設(shè)置響應(yīng)輸出的頭類型if (Objects.equals(".xls", excelTypeEnum.getValue())) {//導(dǎo)出xls格式response.setContentType("application/vnd.ms-excel;charset=GBK");} else if (Objects.equals(".xlsx", excelTypeEnum.getValue())) {//導(dǎo)出xlsx格式response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=GBK");}// 設(shè)置下載文件名稱(注意中文亂碼)response.addHeader("Content-Disposition","attachment;filename=" + new String((fileName).getBytes("GB2312"), "ISO8859-1") + excelTypeEnum.getValue());response.addHeader("Pragma", "No-cache");response.addHeader("Cache-Control", "No-cache");response.setCharacterEncoding("utf8");return response.getOutputStream();} catch (IOException e) {//LOGGER.error("EasyExcelUtil-->getOutputStream exception:", e);}return null;}3.導(dǎo)出模型
package com.iflytek.edu.hnezxjgl.model;import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.metadata.BaseRowModel; import lombok.Data;@Data public class ExportModel extends BaseRowModel{/*** 賬號(hào)*/@ExcelProperty(value = {"賬號(hào)"}, index = 0)private String platformNum;/*** 姓名*/@ExcelProperty(value = {"姓名"}, index = 1)private String name;/*** 身份證號(hào)*/@ExcelProperty(value = {"身份證號(hào)"}, index = 2)private String idCardNum;/*** 性別*/@ExcelProperty(value = {"性別"}, index = 3)private String sexName;/*** 年級(jí)*/@ExcelProperty(value = {"年級(jí)"}, index = 4)private String gradeName;/*** 班級(jí)*/@ExcelProperty(value = {"班級(jí)"}, index = 5)private String className;/*** 學(xué)費(fèi)繳費(fèi)狀態(tài)名稱*/@ExcelProperty(value = "學(xué)費(fèi)繳費(fèi)狀態(tài)名稱",index = 6)private String studyFeeStatusName;/*** 書(shū)本費(fèi)繳費(fèi)狀態(tài)名稱*/@ExcelProperty(value = "書(shū)本費(fèi)繳費(fèi)狀態(tài)名稱",index = 7)private String bookFeeStatusName;}4.幾萬(wàn)條數(shù)據(jù)實(shí)現(xiàn)秒導(dǎo)
總結(jié)
以上是生活随笔為你收集整理的使用easyExcel导出excel数据案例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 电脑知识:磁盘分区相关知识笔记!
- 下一篇: Flash Builder 4 正式版序