package com.cockpit.util; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import java.util.Map; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.core.Context; import org.apache.log4j.Logger; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import com.cockpit.vo.FiveHundredkVExport; public class ExportUtil { @Context private org.apache.cxf.jaxrs.ext.MessageContext mc; private final Logger log = Logger.getLogger(getClass()); public String createExcel(Map> sheetMap){ HttpServletRequest request = mc.getHttpServletRequest(); String filePath = request.getRealPath("excels"); String fileName = UUID.randomUUID().toString()+".xls"; FileOutputStream out = null; try { out = new FileOutputStream(filePath+"/"+fileName); HSSFWorkbook wb = new HSSFWorkbook(); HSSFCellStyle style = wb.createCellStyle();//创建样式对象 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中 style.setBottomBorderColor(HSSFColor.BLACK.index); style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM); style.setLeftBorderColor(HSSFColor.BLACK.index); style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM); style.setRightBorderColor(HSSFColor.BLACK.index); for(String key : sheetMap.keySet()){ int i = 0; HSSFSheet sheet = wb.createSheet(key); HSSFRow row = null; //表头 for(String header : sheetMap.get(key).get(0).getHeader()){ row = sheet.createRow(0); row.createCell(i).setCellValue(header); i++; } i = 1; //内容 for(FiveHundredkVExport element : sheetMap.get(key)){ row = sheet.createRow(i); row.createCell(0).setCellValue(element.getName()); row.createCell(1).setCellValue(element.getFirstTrip()); row.createCell(2).setCellValue(element.getLastRegain()); row.createCell(3).setCellValue(element.getAddUpTrip()); }; }; wb.write(out); out.close(); } catch (FileNotFoundException e) { log.error("驾驶舱Excel报表导出错误,文件找不到错误(FileNotFoundException)"); } catch (IOException e) { log.error("驾驶舱Excel报表导出错误,文件输出错误(IOException)"); } System.out.println("=========================================================="); System.out.println(filePath+"/"+fileName); return fileName; }; }