poi操作excel
<!--poi--> <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>
package com.zdyl.devicemanagement.common.utils; import org.apache.poi.hssf.usermodel.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Map; public class PoiUtils { /** * 导出Excel并下载 * * @param response */ public static void downLoadExcel(HttpServletResponse response, List<Map<String, Object>> deviceList) { SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("设备告警表"); String fileName = "EquipmentAlarm" + format.format(new Date()) + ".xls";//设置要导出的文件的名字 String[] headers = {"场站名称", "告警级别", "设备名称", "告警ID", "告警含义", "信号ID", "关联信号", "原始编码", "开始时间"}; //headers表示excel表中第一行的表头 HSSFRow row = sheet.createRow(0); //在excel表中添加表头 for (int i = 0; i < headers.length; i++) { HSSFCell cell = row.createCell(i); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } //新增数据行,并且设置单元格数据 int rowNum = 1; //在表中存放查询到的数据放入对应的列 for (Map<String, Object> alarm : deviceList) { HSSFRow row1 = sheet.createRow(rowNum); if (alarm.get("StationName") == null || alarm.get("StationName").equals("")) { continue; } row1.createCell(0).setCellValue(alarm.get("StationName").toString()); if (alarm.get("AlarmLevel") == null || alarm.get("AlarmLevel").equals("")) { continue; } row1.createCell(1).setCellValue(alarm.get("AlarmLevel").toString()); if (alarm.get("DeviceName") == null || alarm.get("DeviceName").equals("")) { continue; } row1.createCell(2).setCellValue(alarm.get("DeviceName").toString()); if (alarm.get("AlarmID") == null || alarm.get("AlarmID").equals("")) { continue; } row1.createCell(3).setCellValue(alarm.get("AlarmID").toString()); if (alarm.get("Meanings") == null || alarm.get("Meanings").equals("")) { continue; } row1.createCell(4).setCellValue(alarm.get("Meanings").toString()); if (alarm.get("SignalID") == null || alarm.get("SignalID").equals("")) { continue; } row1.createCell(5).setCellValue(alarm.get("SignalID").toString()); if (alarm.get("SignalName") == null || alarm.get("SignalName").equals("")) { continue; } row1.createCell(6).setCellValue(alarm.get("SignalName").toString()); if (alarm.get("OriginalNo") == null || alarm.get("OriginalNo").equals("")) { continue; } row1.createCell(7).setCellValue(alarm.get("OriginalNo").toString()); if (alarm.get("StartTime") == null || alarm.get("StartTime").equals("")) { continue; } // SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); // SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // Date startTime = format1.parse(alarm.get("StartTime").toString()); row1.createCell(8).setCellValue(alarm.get("StartTime").toString()); rowNum++; } response.setContentType("application/octet-stream"); response.setHeader("Content-disposition", "attachment;filename=" + fileName); try { response.flushBuffer(); workbook.write(response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } } }
文件下载
/** * 图片下载 */ public class ImageDowload { /** * @param imageUrl 图片地址 * @param imageName 图片下载到本地的名称 * @param response */ public static void imageDowload(String imageUrl, String imageName, HttpServletResponse response, String format) throws IOException { File file = new File(imageUrl); FileInputStream fis = new FileInputStream(file); response.setContentType("application/force-download"); response.addHeader("Content-disposition", "attachment;fileName=" + file.getName()); OutputStream os = response.getOutputStream(); byte[] buf = new byte[1024]; int len = 0; while ((len = fis.read(buf)) != -1) { os.write(buf, 0, len); } } }