当前位置:   article > 正文

poi导出excel工具类_poi-ooxml-3.1.7

poi-ooxml-3.1.7


poi版本

  1. <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
  2. <dependency>
  3. <groupId>org.apache.poi</groupId>
  4. <artifactId>poi</artifactId>
  5. <version>3.17</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
  8. <dependency>
  9. <groupId>org.apache.poi</groupId>
  10. <artifactId>poi-ooxml</artifactId>
  11. <version>3.17</version>
  12. </dependency>


  1. /**
  2. * Excel导出工具类
  3. * @author zsc
  4. * @datetime 2017年12月14日 下午8:01:32
  5. */
  6. public class ExcelUtil {
  7. // 将需要的单元格式样式放到Map集合中,使用时直接从Map中获取,如果在使用时创建,那当数据量很大时严重影响性能
  8. public static ThreadLocal<Map<String, XSSFCellStyle>> styles = new ThreadLocal<>();
  9. /** 构造方法私有,禁止用户new对象 */
  10. private ExcelUtil() {super();}
  11. /**
  12. * 导出工作簿,将工作簿写响应(response)输出流实现浏览器下载
  13. * @param response
  14. * @param workbook
  15. * @param fileName
  16. * @throws Exception
  17. */
  18. public static void exportExcel(HttpServletResponse response, XSSFWorkbook workbook, String fileName) throws Exception {
  19. OutputStream os = null;
  20. try {
  21. os = response.getOutputStream();
  22. response.reset();
  23. response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xls");
  24. response.setContentType("application/msexcel");
  25. workbook.write(os);
  26. } finally {
  27. if(null != workbook) {
  28. workbook.close();
  29. }
  30. if(null != os) {
  31. os.flush();
  32. os.close();
  33. }
  34. }
  35. }
  36. /**
  37. * 创建工作簿
  38. * @param sheetNames
  39. * @param headNames
  40. * @param titles
  41. * @param contents
  42. * @return
  43. * @throws Exception
  44. */
  45. public static XSSFWorkbook createExcel(String[] sheetNames, String[] headNames,
  46. List<String[]> titles, List<List<Object[]>> contents) throws Exception {
  47. XSSFWorkbook workbook = new XSSFWorkbook();
  48. int sheetCount = sheetNames.length;
  49. XSSFSheet sheet = null;
  50. String headName = "";
  51. for(int i = 0; i < sheetCount; i++) {
  52. sheet = workbook.createSheet(sheetNames[i]);
  53. headName = (null != headNames && StringUtils.isNotBlank(headNames[i])) ? headNames[i] : sheetNames[i];
  54. createExcel(workbook, sheet, headName, titles.get(i), contents.get(i));
  55. }
  56. return workbook;
  57. }
  58. public static XSSFWorkbook createExcel(String sheetName, String headName,
  59. String[] titles, List<Object[]> contents) throws Exception {
  60. XSSFWorkbook workbook = new XSSFWorkbook();
  61. XSSFSheet sheet = workbook.createSheet(sheetName);
  62. createExcel(workbook, sheet, headName, titles, contents);
  63. return workbook;
  64. }
  65. /**
  66. * 创建工作簿
  67. * @param workbook 导出工作簿
  68. * @param sheet 导出工作表
  69. * @param titles 标题列表
  70. * @param contents 数据列表
  71. * @throws Exception
  72. */
  73. public static void createExcel(XSSFWorkbook workbook, XSSFSheet sheet, String[] titles, List<Object[]> contents) throws Exception {
  74. createExcel(workbook, sheet, sheet.getSheetName(), titles, contents);
  75. }
  76. /**
  77. * 导出Excel
  78. * @param os 输出流
  79. * @param workbook 导出工作簿
  80. * @param sheet 导出工作表
  81. * @param headName 表头名
  82. * @param titles 标题列表
  83. * @param contents 数据列表
  84. * @throws Exception
  85. */
  86. public static void createExcel(XSSFWorkbook workbook, XSSFSheet sheet, String headName,
  87. String[] titles, List<Object[]> contents) throws Exception {
  88. try {
  89. if(null == contents || contents.size() <= 0) {
  90. return;
  91. }
  92. // 创建单元格式样式集合
  93. styles.set(styleMap(workbook));
  94. // 创建工作表头
  95. createSheetHead(workbook, sheet, titles, headName);
  96. // 填充工作表数据
  97. createSheetData(sheet, contents, 2);
  98. } finally {
  99. if(null != styles.get()) {
  100. styles.get().clear();
  101. }
  102. styles.remove();
  103. }
  104. }
  105. /**
  106. * 构建sheet表头
  107. * @param sheet
  108. * @param heads
  109. */
  110. private static void createSheetHead(XSSFWorkbook workbook, XSSFSheet sheet, String[] heads, String headName) {
  111. sheet.createFreezePane(0, 2, 0, 2);// 冻结前2行
  112. sheet.setDefaultColumnWidth((short) 20);// 设置表格默认列宽度为20个字节
  113. XSSFCellStyle tilteStyle = styles.get().get("head");
  114. XSSFCell cell = null;
  115. XSSFRow rowFirst = sheet.createRow(0);// 创建第一行(报表名称)
  116. cell = rowFirst.createCell(0);
  117. cell.setCellValue(StringUtils.isBlank(headName) ? sheet.getSheetName() : headName);
  118. cell.setCellStyle(tilteStyle);
  119. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, heads.length -1));
  120. XSSFRow row = sheet.createRow(1);// 创建第二行(列名)
  121. if (heads != null && heads.length > 0) {
  122. XSSFCellStyle cellStyle = styles.get().get("title");
  123. for (int i = 0; i < heads.length; i++) {
  124. cell = row.createCell(i);
  125. if (heads[i] != null) {
  126. cell.setCellValue(heads[i]);
  127. cell.setCellStyle(cellStyle);
  128. }
  129. }
  130. }
  131. }
  132. /**
  133. * 构建sheet数据内容
  134. * @param sheet
  135. * @param contents
  136. * @param index
  137. */
  138. private static void createSheetData(XSSFSheet sheet, List<Object[]> contents, int index) {
  139. XSSFCellStyle contentStyle = ExcelUtil.styles.get().get("content");
  140. XSSFCellStyle integerStyle = ExcelUtil.styles.get().get("integer");
  141. XSSFCellStyle doubleStyle = ExcelUtil.styles.get().get("double");
  142. Iterator<Object[]> it = contents.iterator();
  143. Row nextrow;
  144. Cell cell2;
  145. Object[] obj;
  146. // 遍历数据
  147. while (it.hasNext()) {
  148. nextrow = sheet.createRow(index++);
  149. obj = it.next();
  150. if (obj != null) {
  151. int objLen = obj.length;
  152. for (int i = 0; i < objLen; i++) {
  153. cell2 = nextrow.createCell(i);
  154. if (obj[i] != null) {
  155. if(obj[i] instanceof Float || obj[i] instanceof Double || StringUtil.isNumeric1(obj[i].toString())){
  156. cell2.setCellValue(Double.parseDouble(obj[i].toString()));
  157. cell2.setCellStyle(doubleStyle);
  158. }else if(obj[i] instanceof Integer || (obj[i] instanceof Long && obj[i].toString().length() <=10)
  159. || (StringUtil.isNumeric(obj[i].toString()) && obj[i].toString().length() <= 10)){
  160. cell2.setCellValue(Integer.parseInt(obj[i].toString()));
  161. cell2.setCellStyle(integerStyle);
  162. }else{
  163. cell2.setCellValue(obj[i].toString());
  164. cell2.setCellStyle(contentStyle);
  165. }
  166. } else {
  167. cell2.setCellValue("");
  168. cell2.setCellStyle(contentStyle);
  169. }
  170. }
  171. }
  172. }
  173. }
  174. /**
  175. * 创建单元格表头样式
  176. *
  177. * @param workbook 工作薄
  178. */
  179. private static XSSFCellStyle createCellHeadStyle(XSSFWorkbook workbook) {
  180. XSSFCellStyle style = workbook.createCellStyle();
  181. // 设置边框样式
  182. style.setBorderBottom(BorderStyle.THIN);
  183. style.setBorderLeft(BorderStyle.THIN);
  184. style.setBorderRight(BorderStyle.THIN);
  185. style.setBorderTop(BorderStyle.THIN);
  186. //设置对齐样式
  187. style.setAlignment(HorizontalAlignment.CENTER);
  188. // 生成字体
  189. XSSFFont font = workbook.createFont();
  190. font.setFontHeightInPoints((short) 20);
  191. font.setBold(true);
  192. // 把字体应用到当前的样式
  193. style.setFont(font);
  194. return style;
  195. }
  196. /**
  197. * 创建单元格表头标题样式
  198. *
  199. * @param workbook 工作薄
  200. */
  201. private static XSSFCellStyle createCellTitleStyle(XSSFWorkbook workbook) {
  202. XSSFCellStyle style = workbook.createCellStyle();
  203. // 设置边框样式
  204. style.setBorderBottom(BorderStyle.THIN);
  205. style.setBorderLeft(BorderStyle.THIN);
  206. style.setBorderRight(BorderStyle.THIN);
  207. style.setBorderTop(BorderStyle.THIN);
  208. //设置对齐样式
  209. style.setAlignment(HorizontalAlignment.CENTER);
  210. // 生成字体
  211. XSSFFont font = workbook.createFont();
  212. // 表头样式
  213. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  214. style.setFillForegroundColor(new XSSFColor(Color.CYAN));
  215. font.setBold(true);
  216. // 把字体应用到当前的样式
  217. style.setFont(font);
  218. return style;
  219. }
  220. /**
  221. * 创建单元格正文样式
  222. *
  223. * @param workbook 工作薄
  224. */
  225. private static XSSFCellStyle createCellContentStyle(XSSFWorkbook workbook) {
  226. XSSFCellStyle style = workbook.createCellStyle();
  227. // 设置边框样式
  228. style.setBorderBottom(BorderStyle.THIN );
  229. style.setBorderLeft(BorderStyle.THIN);
  230. style.setBorderRight(BorderStyle.THIN);
  231. style.setBorderTop(BorderStyle.THIN);
  232. // 生成字体
  233. XSSFFont font = workbook.createFont();
  234. // 正文样式
  235. style.setVerticalAlignment(VerticalAlignment.CENTER);
  236. font.setBold(false);
  237. // 把字体应用到当前的样式
  238. style.setFont(font);
  239. return style;
  240. }
  241. /**
  242. * 单元格样式(Integer)列表
  243. */
  244. private static XSSFCellStyle createCellContent4IntegerStyle(XSSFWorkbook workbook) {
  245. XSSFCellStyle style = workbook.createCellStyle();
  246. // 设置边框样式
  247. style.setBorderBottom(BorderStyle.THIN );
  248. style.setBorderLeft(BorderStyle.THIN);
  249. style.setBorderRight(BorderStyle.THIN);
  250. style.setBorderTop(BorderStyle.THIN);
  251. // 生成字体
  252. XSSFFont font = workbook.createFont();
  253. // 正文样式
  254. style.setVerticalAlignment(VerticalAlignment.CENTER);
  255. font.setBold(false);
  256. // 把字体应用到当前的样式
  257. style.setFont(font);
  258. style.setDataFormat(workbook.createDataFormat().getFormat("#,##0"));//数据格式只显示整数
  259. return style;
  260. }
  261. /**
  262. * 单元格样式(Double)列表
  263. */
  264. private static XSSFCellStyle createCellContent4DoubleStyle(XSSFWorkbook workbook) {
  265. XSSFCellStyle style = workbook.createCellStyle();
  266. // 设置边框样式
  267. style.setBorderBottom(BorderStyle.THIN );
  268. style.setBorderLeft(BorderStyle.THIN);
  269. style.setBorderRight(BorderStyle.THIN);
  270. style.setBorderTop(BorderStyle.THIN);
  271. // 生成字体
  272. XSSFFont font = workbook.createFont();
  273. // 正文样式
  274. style.setVerticalAlignment(VerticalAlignment.CENTER);
  275. font.setBold(false);
  276. // 把字体应用到当前的样式
  277. style.setFont(font);
  278. style.setDataFormat(workbook.createDataFormat().getFormat("#,##0.00"));//保留两位小数点
  279. return style;
  280. }
  281. /**
  282. * 单元格样式列表
  283. */
  284. private static Map<String, XSSFCellStyle> styleMap(XSSFWorkbook workbook) {
  285. Map<String, XSSFCellStyle> styleMap = new LinkedHashMap<>();
  286. styleMap.put("head", createCellHeadStyle(workbook));
  287. styleMap.put("title", createCellTitleStyle(workbook));
  288. styleMap.put("content", createCellContentStyle(workbook));
  289. styleMap.put("integer", createCellContent4IntegerStyle(workbook));
  290. styleMap.put("double", createCellContent4DoubleStyle(workbook));
  291. return styleMap;
  292. }
  293. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/170195
推荐阅读
相关标签
  

闽ICP备14008679号