ExcelUtil.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.hssx.cloudmodel.util;
  2. import org.apache.poi.hssf.usermodel.*;
  3. import org.apache.poi.ss.usermodel.BorderStyle;
  4. import org.apache.poi.ss.usermodel.CellStyle;
  5. import org.apache.poi.ss.usermodel.FillPatternType;
  6. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.File;
  9. import java.io.FileOutputStream;
  10. import java.net.URLEncoder;
  11. import java.util.List;
  12. public class ExcelUtil {
  13. /**
  14. * 简单Excel导出
  15. * @param response
  16. * @param title 标题
  17. * @param list 数据
  18. * @return
  19. */
  20. public static String exportGeneralExcelByTitleAndList(HttpServletResponse response, String title, List<List<String>> list,String downloadPath) {
  21. String result="系统提示:Excel文件导出成功!";
  22. String fileName= title+".xls";
  23. try {
  24. response.reset();
  25. response.setHeader("Content-disposition",
  26. "attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));
  27. //设置文件头编码格式
  28. response.setContentType("APPLICATION/OCTET-STREAM;charset=UTF-8");//设置类型
  29. response.setHeader("Cache-Control","no-cache");//设置头
  30. response.setDateHeader("Expires", 0);//设置日期头
  31. // 创建工作簿
  32. HSSFWorkbook workBook = new HSSFWorkbook();
  33. // 创建工作类
  34. HSSFSheet sheet = workBook.createSheet();
  35. sheet.setDefaultColumnWidth(16);
  36. //设置字体样式
  37. HSSFFont headFont = workBook.createFont();
  38. headFont.setBold(true);
  39. headFont.setFontHeightInPoints((short) 10);
  40. headFont.setFontName("黑体");
  41. HSSFFont titleFont = workBook.createFont();
  42. titleFont.setBold(true);
  43. titleFont.setFontHeightInPoints((short) 10);
  44. titleFont.setFontName("黑体");
  45. HSSFFont font = workBook.createFont();
  46. font.setFontHeightInPoints((short) 10);
  47. font.setFontName("宋体");
  48. //设置单元格样式
  49. CellStyle headStyle = workBook.createCellStyle();
  50. headStyle.setFont(headFont);
  51. headStyle.setAlignment(HorizontalAlignment.CENTER);
  52. headStyle.setVerticalAlignment(org.apache.poi.ss.usermodel.VerticalAlignment.CENTER);
  53. headStyle.setWrapText(true);
  54. headStyle.setBorderBottom(BorderStyle.THIN); //下边框
  55. headStyle.setBorderLeft(BorderStyle.THIN);//左边框
  56. headStyle.setBorderTop(BorderStyle.THIN);//上边框
  57. headStyle.setBorderRight(BorderStyle.THIN);//右边框
  58. String color = "c0c0c0"; //此处得到的color为16进制的字符串
  59. //转为RGB码
  60. int r = Integer.parseInt((color.substring(0,2)),16); //转为16进制
  61. int g = Integer.parseInt((color.substring(2,4)),16);
  62. int b = Integer.parseInt((color.substring(4,6)),16);
  63. //自定义cell颜色
  64. HSSFPalette palette = workBook.getCustomPalette();
  65. //这里的9是索引
  66. palette.setColorAtIndex((short)9, (byte) r, (byte) g, (byte) b);
  67. CellStyle titleStyle = workBook.createCellStyle();
  68. titleStyle.setFont(titleFont);
  69. titleStyle.setAlignment(HorizontalAlignment.CENTER);
  70. titleStyle.setVerticalAlignment(org.apache.poi.ss.usermodel.VerticalAlignment.CENTER);
  71. titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); //填充单元格
  72. titleStyle.setFillForegroundColor((short)9); //填色
  73. titleStyle.setWrapText(true);
  74. titleStyle.setBorderBottom(BorderStyle.THIN); //下边框
  75. titleStyle.setBorderLeft(BorderStyle.THIN);//左边框
  76. titleStyle.setBorderTop(BorderStyle.THIN);//上边框
  77. titleStyle.setBorderRight(BorderStyle.THIN);//右边框
  78. CellStyle cellStyle = workBook.createCellStyle();
  79. cellStyle.setFont(font);
  80. cellStyle.setAlignment(HorizontalAlignment.CENTER);
  81. cellStyle.setVerticalAlignment(org.apache.poi.ss.usermodel.VerticalAlignment.CENTER);
  82. cellStyle.setWrapText(true);
  83. cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
  84. cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
  85. cellStyle.setBorderTop(BorderStyle.THIN);//上边框
  86. cellStyle.setBorderRight(BorderStyle.THIN);//右边框
  87. if(list.size() > 0) {
  88. //标题(如果需要在EXCEL内容最上面加标题,请打开下面的注释,修改start)
  89. /*
  90. HSSFRow titleRow = sheet.createRow(0);
  91. titleRow.setHeightInPoints(30);
  92. HSSFCell titleCell = titleRow.createCell(0);
  93. titleCell.setCellStyle(headStyle);
  94. titleCell.setCellValue(title);
  95. //合并单元格
  96. CellRangeAddress cellRangeAddress = new CellRangeAddress(0,0,0, list.get(0).size() - 1);
  97. //加入合并单元格对象
  98. sheet.addMergedRegion(cellRangeAddress);
  99. //使用RegionUtil类为合并后的单元格添加边框
  100. RegionUtil.setBorderBottom(BorderStyle.THIN, cellRangeAddress, sheet); // 下边框
  101. RegionUtil.setBorderLeft(BorderStyle.THIN, cellRangeAddress, sheet); // 左边框
  102. RegionUtil.setBorderRight(BorderStyle.THIN, cellRangeAddress, sheet); // 有边框
  103. RegionUtil.setBorderTop(BorderStyle.THIN, cellRangeAddress, sheet); // 上边框
  104. */
  105. int start = 0;
  106. for(List<String> rowList : list) {
  107. HSSFRow row = sheet.createRow(start);
  108. row.setHeightInPoints(24);
  109. for(int i = 0; i < rowList.size(); i++) {
  110. HSSFCell cell = row.createCell(i);
  111. if(start == 0) {
  112. cell.setCellStyle(titleStyle);
  113. }else {
  114. cell.setCellStyle(cellStyle);
  115. }
  116. cell.setCellValue(rowList.get(i));
  117. }
  118. start++;
  119. }
  120. }
  121. // OutputStream os = response.getOutputStream();//在线下载
  122. File dir = null;
  123. dir = new File(downloadPath);
  124. // D://cloud/upload 文件上传后所存储的位置,部署到服务器上时配置服务器地址即可
  125. if (!dir.exists()) {
  126. dir.mkdirs();
  127. }
  128. FileOutputStream os = new FileOutputStream(downloadPath+fileName);//保存到本地
  129. workBook.write(os);
  130. os.flush();
  131. os.close();
  132. }catch(Exception e) {
  133. System.out.println(result);
  134. e.printStackTrace();
  135. }
  136. return "/upload/"+fileName;
  137. }
  138. }