b070b3cdddbf6e7ae2c40aba3c5d36480ac0d6e9.svn-base 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package com.cockpit.util;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.UUID;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.ws.rs.core.Context;
  10. import org.apache.log4j.Logger;
  11. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  12. import org.apache.poi.hssf.usermodel.HSSFRow;
  13. import org.apache.poi.hssf.usermodel.HSSFSheet;
  14. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  15. import org.apache.poi.hssf.util.HSSFColor;
  16. import com.cockpit.vo.FiveHundredkVExport;
  17. public class ExportUtil {
  18. @Context
  19. private org.apache.cxf.jaxrs.ext.MessageContext mc;
  20. private final Logger log = Logger.getLogger(getClass());
  21. public String createExcel(Map<String, List<FiveHundredkVExport>> sheetMap){
  22. HttpServletRequest request = mc.getHttpServletRequest();
  23. String filePath = request.getRealPath("excels");
  24. String fileName = UUID.randomUUID().toString()+".xls";
  25. FileOutputStream out = null;
  26. try {
  27. out = new FileOutputStream(filePath+"/"+fileName);
  28. HSSFWorkbook wb = new HSSFWorkbook();
  29. HSSFCellStyle style = wb.createCellStyle();//创建样式对象
  30. style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
  31. style.setBottomBorderColor(HSSFColor.BLACK.index);
  32. style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
  33. style.setLeftBorderColor(HSSFColor.BLACK.index);
  34. style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
  35. style.setRightBorderColor(HSSFColor.BLACK.index);
  36. for(String key : sheetMap.keySet()){
  37. int i = 0;
  38. HSSFSheet sheet = wb.createSheet(key);
  39. HSSFRow row = null;
  40. //表头
  41. for(String header : sheetMap.get(key).get(0).getHeader()){
  42. row = sheet.createRow(0);
  43. row.createCell(i).setCellValue(header);
  44. i++;
  45. }
  46. i = 1;
  47. //内容
  48. for(FiveHundredkVExport element : sheetMap.get(key)){
  49. row = sheet.createRow(i);
  50. row.createCell(0).setCellValue(element.getName());
  51. row.createCell(1).setCellValue(element.getFirstTrip());
  52. row.createCell(2).setCellValue(element.getLastRegain());
  53. row.createCell(3).setCellValue(element.getAddUpTrip());
  54. };
  55. };
  56. wb.write(out);
  57. out.close();
  58. } catch (FileNotFoundException e) {
  59. log.error("驾驶舱Excel报表导出错误,文件找不到错误(FileNotFoundException)");
  60. } catch (IOException e) {
  61. log.error("驾驶舱Excel报表导出错误,文件输出错误(IOException)");
  62. }
  63. System.out.println("==========================================================");
  64. System.out.println(filePath+"/"+fileName);
  65. return fileName;
  66. };
  67. }