ExcelUtil.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.welampiot.utils;
  2. import org.apache.poi.hssf.usermodel.HSSFRow;
  3. import org.apache.poi.hssf.usermodel.HSSFSheet;
  4. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  5. import org.apache.poi.ss.usermodel.CellStyle;
  6. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  7. import org.apache.poi.ss.usermodel.VerticalAlignment;
  8. import org.apache.poi.xssf.usermodel.XSSFCell;
  9. import org.apache.poi.xssf.usermodel.XSSFRow;
  10. import org.apache.poi.xssf.usermodel.XSSFSheet;
  11. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  12. import org.springframework.web.multipart.MultipartFile;
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. public class ExcelUtil {
  21. /**
  22. * 文件导出到excel
  23. *
  24. * @param titleList 标题列表 一维列表
  25. * @param contentList 数据列表 二维列表
  26. * @return
  27. */
  28. public static String outExcel(List<String> titleList, List<List<String>> contentList){
  29. XSSFWorkbook sheets = new XSSFWorkbook();
  30. XSSFSheet sheet = sheets.createSheet();
  31. // 冻结第一行
  32. sheet.createFreezePane(0,1,0,1);
  33. // 创建居中显示的单元格样式
  34. CellStyle style = sheets.createCellStyle();
  35. style.setAlignment(HorizontalAlignment.CENTER);
  36. style.setVerticalAlignment(VerticalAlignment.CENTER);
  37. // 创建表头,也就是第一行
  38. XSSFRow row = sheet.createRow(0);
  39. for (int i = 0; i < titleList.size(); i++) {
  40. XSSFCell cell = row.createCell(i);
  41. cell.setCellValue(titleList.get(i));
  42. cell.setCellStyle(style); // 设置单元格为居中显示样式
  43. }
  44. //创建表内容,从第二行开始
  45. for (int i = 0; i < contentList.size(); i++) {
  46. row = sheet.createRow(i + 1);
  47. for (int j = 0; j < contentList.get(i).size(); j++) {
  48. XSSFCell cell = row.createCell(j);
  49. cell.setCellValue(contentList.get(i).get(j));
  50. cell.setCellStyle(style); // 设置单元格为居中显示样式
  51. }
  52. }
  53. long l = System.currentTimeMillis();
  54. String filePath = "/upload/temp/"+l+".xls";
  55. File file = new File(".."+filePath);
  56. //将文件保存到指定的位置
  57. try {
  58. File file2 = new File("../upload/temp/");
  59. if (!file2.exists()) file2.mkdirs();
  60. sheets.write(new FileOutputStream(file));
  61. System.out.println("写入成功");
  62. sheets.close();
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. }
  66. return filePath;
  67. }
  68. public static List<Map> inExcel(MultipartFile file){
  69. // String contentType = file.getContentType();
  70. // String fileName = file.getOriginalFilename();
  71. List<Map> list = new ArrayList<>();
  72. try {
  73. //根据路径获取这个操作excel的实例
  74. HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());
  75. //根据页面index 获取sheet页
  76. HSSFSheet sheet = wb.getSheetAt(0);
  77. HSSFRow row = null;
  78. //循环sesheet页中数据从第二行开始,第一行是标题
  79. for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
  80. //获取每一行数据
  81. row = sheet.getRow(i);
  82. Map<Integer, String> stringMap = new HashMap<>();
  83. System.out.println(row.getRowNum());
  84. for (int j = 0; j < row.getRowNum(); j++) {
  85. if (row.getCell(j) == null) continue;
  86. String string = row.getCell(j).toString();
  87. stringMap.put(j,string);
  88. }
  89. list.add(stringMap);
  90. }
  91. } catch (Exception e) {
  92. // e.printStackTrace();
  93. }
  94. return list;
  95. }
  96. }