赞
踩
最近做一个推送服务将系统某些报表,通过邮件附件的形式推送给指定的人,首先我们需要在服务端生成报表的excel文档。生成excel文档采用的是POI的方式。
引入三个poi相关的jar包
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi</artifactId>
- <version>4.1.2</version>
- </dependency>
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi-ooxml</artifactId>
- <version>4.1.2</version>
- </dependency>
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi-ooxml-schemas</artifactId>
- <version>4.1.2</version>
- </dependency>
注意:这三个jar包的版本号一定要保持一致。否则的话会引起异常,无法创建XSSFWorkbook
- import lombok.extern.slf4j.Slf4j;
- import org.apache.http.client.utils.DateUtils;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.ss.usermodel.Workbook;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
-
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.sql.Timestamp;
- import java.util.*;
-
- /**
- * POI导出Excel封装
- * @author lenny
- * @date 20210715
- */
- @Slf4j
- public class ExcelUtil {
- /**
- * @param fileName excel文件名
- * @param headMap 表头map
- * @param dataList 表格数据
- */
- public static String exportExcel(String fileName,Map<String, String> headMap, List<Map<String, Object>> dataList) {
- String fullPath="";
- Workbook workbook = exportXlsx(fileName, headMap, dataList);
- FileOutputStream outputStream=null;
- try {
- //将生成的excel存储在系统根目录下excel文件夹中
- String path=CommonUtil.getRunningDirectory()+"\\excel\\";
- fullPath=path+fileName;
- outputStream = new FileOutputStream(path+fileName);
- workbook.write(outputStream);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (outputStream != null) {
- try {
- outputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- //返回文件所在的目录
- return fullPath;
- }
-
- /**
- * 导出数据
- *
- * @param headMap 表头map
- * @param dataList 导出的数据
- */
- public static Workbook exportXlsx(String sheetName, Map<String, String> headMap, List<Map<String, Object>> dataList) {
- try {
- XSSFWorkbook workbook = new XSSFWorkbook();
- Sheet sheet = workbook.createSheet(sheetName);
- //初始化行列索引
- int rowIndex = 0, columnIndex = 0;
- Set<String> keys = headMap.keySet();
- //表头
- Row row = sheet.createRow(rowIndex++);
- for (String key : keys) {
- Cell cell = row.createCell(columnIndex++);
- cell.setCellValue(headMap.get(key));
- }
- //内容
- if (dataList != null && !dataList.isEmpty()) {
- for (Map<String, Object> map : dataList) {
- row = sheet.createRow(rowIndex++);
- columnIndex = 0;
- for (String key : keys) {
- Cell cell = row.createCell(columnIndex++);
- setCellValue(cell, map.get(key));
- }
- }
- }
- return workbook;
- }catch (Exception e){
- log.error(e.toString());
- return null;
- }
- }
-
- /**
- * 填充单元格内容
- * @param cell
- * @param obj
- */
- private static void setCellValue(Cell cell, Object obj) {
- if (obj == null) {
- return;
- }
- if (obj instanceof String) {
- cell.setCellValue((String) obj);
- } else if (obj instanceof Date) {
- Date date = (Date) obj;
- if (date != null) {
- cell.setCellValue(DateUtils.formatDate(date));
- }
- } else if (obj instanceof Calendar) {
- Calendar calendar = (Calendar) obj;
- if (calendar != null) {
- cell.setCellValue(DateUtils.formatDate(calendar.getTime()));
- }
- } else if (obj instanceof Timestamp) {
- Timestamp timestamp = (Timestamp) obj;
- if (timestamp != null) {
- cell.setCellValue(DateUtils.formatDate(new Date(timestamp.getTime())));
- }
- } else if (obj instanceof Double) {
- cell.setCellValue((Double) obj);
- } else {
- cell.setCellValue(obj.toString());
- }
- }
- }

其中使用到了封装的获取系统根目录的方法,方法实现如下:
- /**
- * 得到程序运行的根目录
- * @return
- */
- public static String getRunningDirectory()
- {
- return System.getProperty("user.dir");
- }
- /**
- * 导出Adas报警
- * @param reportAdasList
- * @param language
- * @param timeDiff
- * @return
- */
- public static String exportAdasReport(List<ReportAdasList> reportAdasList, int language, int timeDiff) {
- //表头
- Map<String, String> headNameMap = new LinkedHashMap<String, String>();
- headNameMap.put("agentName", getHeadName("agentName",language));
- headNameMap.put("vehicleName", getHeadName("vehicleName",language));
- headNameMap.put("assetID", getHeadName("assetID",language));
- headNameMap.put("alarmType", getHeadName("alarmType",language));
- headNameMap.put("alarmLevel", getHeadName("alarmLevel",language));
- headNameMap.put("startTime", getHeadName("startTime",language));
- headNameMap.put("endTime", getHeadName("endTime",language));
- headNameMap.put("driverName", getHeadName("driverName",language));
- headNameMap.put("startAddress", getHeadName("startAddress",language));
- headNameMap.put("endAddress", getHeadName("endAddress",language));
- headNameMap.put("processStatus", getHeadName("processStatus",language));
- headNameMap.put("processContext", getHeadName("processContext",language));
- headNameMap.put("processTime", getHeadName("processTime",language));
- //表格数据
- List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
- for(ReportAdasList item : reportAdasList){
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("agentName", item.getAgentName());
- map.put("vehicleName", item.getVehicleName());
- map.put("assetID", item.getAssetID());
- map.put("alarmType", language==1?item.getCnName():item.getEnName());
- map.put("alarmLevel", getAlarmLevel(item.getAlarmLevel(),language));
- map.put("startTime", CommonUtil.getLocalTime(item.getStartTime(),timeDiff));
- map.put("endTime", CommonUtil.getLocalTime(item.getEndTime(),timeDiff));
- map.put("driverName", item.getDriverName());
- map.put("startAddress", CommonUtil.geoCoding(String.valueOf(item.getStartLatitude()),String.valueOf(item.getStartLongitude()),language));
- map.put("endAddress", CommonUtil.geoCoding(String.valueOf(item.getEndLatitude()),String.valueOf(item.getEndLongitude()),language));
- map.put("processStatus", getProcessStatus(item.getProcessStatus(),language));
- map.put("processContext", item.getProcessContext());
- map.put("processTime", CommonUtil.getLocalTime(item.getProcessTime(),timeDiff));
- dataList.add(map);
- }
- String fileName = "";
- if(language==1) {
- fileName= CommonConstants.CN_REPORT_ADAS;
- }else {
- fileName=CommonConstants.EN_REPORT_ADAS;
- }
- fileName=fileName+".xlsx";
- String fullPath = ExcelUtil.exportExcel(fileName,headNameMap,dataList);
- return fullPath;
- }

这里大家可以根据自己的系统自行调整,最后封装成headMap(表头字段)与List<Map<String, Object>>(数据列表)即可。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。