發布時間:2018-11-15
技術:springboot1.5.6 + maven3.0.5 + jdk1.8
概述
Springboot最便捷的Excel導出,只需要一個配置文件即可搞定
詳細
一、准備工作
先在pom文件添加依賴如下圖所示:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 使用Jasper引擎解析JSP --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <!-- jstl標簽 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.8.1</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>27.0-jre</version> </dependency> <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> </dependencies>
二、程序實現
1、添加excel導出的person-export-config文件
<?xml version="1.0" encoding="UTF-8"?> <exportFile> <fileName>exportConfig</fileName> <exportType>0</exportType> <cell> <title>序號</title> <alias>index</alias> </cell> <cell> <title>姓名</title> <alias>name</alias> </cell> <cell> <title>年齡</title> <alias>age</alias> </cell> <cell> <title>性別</title> <alias>sex</alias> </cell> <cell> <title>日期</title> <alias>date</alias> </cell> </exportFile>
其中exportType:0表示導出EXCEL2007,exportType:1表示導出csv文件
title導出展示列的title名,alias表示映射的字段名
2、從classpath獲取person-export-config.xml文件
從classpath獲取person-export-coing.xml文件並轉為inputStresm
ClassPathResource classPathResource = new ClassPathResource("export/person-export-config.xml"); InputStream inputStream = classPathResource.getInputStream(); ExportConfig exportConfig = ExportConfigFactory.getExportConfig(inputStream);
3、解析person-export-config.xml文件
主要代碼如下:
private static ExportConfig getExportCells(InputStream inputStream) throws FileExportException { ExportConfig exportConfig = new ExportConfig(); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = null; Document document = null; try { dBuilder = dbFactory.newDocumentBuilder(); document = dBuilder.parse(inputStream); } catch (ParserConfigurationException | SAXException | IOException e) { throw new FileExportException(e, "pares xml error"); } Element root = document.getDocumentElement(); NodeList elements = root.getElementsByTagName("cell"); List<ExportCell> exportCells = initElement(elements); String fileName = ""; String exportType1 = ""; try { fileName = ConfigParser.getNodeText(root, "fileName"); exportType1 = ConfigParser.getNodeText(root, "exportType"); } catch (FileImportException e) { throw new FileExportException(e); } if (StringUtils.isEmpty(fileName)) { throw new FileExportException("用於導出的xml文檔 <fileName> 為空"); } if (StringUtils.isEmpty(exportType1) || !StringUtils.isNumeric(exportType1)) { throw new FileExportException("用於導出的xml文檔 <exportType> 為空"); } exportConfig.setFileName(fileName); ExportType exportType = ExportType.getExportType(Integer.valueOf(exportType1)); if (exportType == null) { throw new FileExportException("找不到相應的ExportType 解析xml得到的exportType 是" + exportType1); } exportConfig.setExportType(exportType); exportConfig.setExportCells(exportCells); return exportConfig; }
這時我們得到一個ExportConfig對象如下:
public class ExportConfig extends BaseEntity { private String fileName;//輸出的文件名 private ExportType exportType;//0 表示 excel, 1 表示csv private List<ExportCell> exportCells;
4、添加要輸出到excel的list對象
List<Map> lists = new LinkedList<>(); for (int i = 0; i < 10; i++) { Map<String, Object> maps = new HashMap<>(); maps.put("index", i); maps.put("name", "張三" + i); maps.put("age", Float.valueOf(i)); maps.put("sex", "男"); maps.put("date", new Date()); lists.add(maps); }
在實際項目中map可以是具體的實體類對象比如Person,只要對象里面的字段跟person-export-config里的alias標簽對應上即可(如下圖所示)。
5、獲取ExportResult對象
具體代碼如下:
ExportResult exportResult = FileExportor.exportResult(exportConfig, lists);
將exportConfig對象跟要輸出到excel的lists對象傳入
public static ExportResult exportResult(ExportConfig exportConfig, List<?> data) throws FileExportException { ExportType exportType = exportConfig.getExportType(); switch (exportType) { case EXCEL2007: Workbook workbook = new ExcelExportImpl().getExportResult(data, exportConfig.getExportCells()); ExportExcelResult exportExcelResult = new ExportExcelResult(); exportExcelResult.setWorkbook(workbook); exportExcelResult.setFileName(exportConfig.getFileName()); return exportExcelResult; case CSV: StringBuilder stringBuilder = new CSVExportImpl().getExportResult(data, exportConfig.getExportCells()); ExportCSVResult exportCSVResult = new ExportCSVResult(); exportCSVResult.setResult(stringBuilder.toString()); exportCSVResult.setFileName(exportConfig.getFileName()); return exportCSVResult; } throw new FileExportException("找不到對應的export type, export type is " + exportType.getNumber()); }
6、最后將數據通過outputstream導出到excel
String fileName = "person統計" +".xlsx"; setResponseHeader(response, fileName); OutputStream outputStream = response.getOutputStream(); exportResult.export(outputStream);
public void export(OutputStream outputStream) throws FileExportException{ try { workbook.write(outputStream); outputStream.close(); } catch (IOException e) { throw new FileExportException("[Error occurred while export excel message is] " + e); } }
三、項目結構圖
四、運行效果圖
五、補充
本例子主要對poi導出excel進行了一個封裝,通過xml配置文件配置跟實體類一一對應的字段,可靈活配置,在實際項目中非常實用。