ExcelUtil.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package com.fdkankan.agent.util;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.poi.hssf.usermodel.HSSFDateUtil;
  4. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  5. import org.apache.poi.ss.usermodel.*;
  6. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  7. import org.springframework.web.multipart.MultipartFile;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.text.SimpleDateFormat;
  11. import java.time.LocalDate;
  12. import java.time.ZoneId;
  13. import java.util.ArrayList;
  14. import java.util.Date;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. @Slf4j
  18. public class ExcelUtil {
  19. public static List<HashMap<Integer,String>> getExcelRowList(Workbook workbook) throws IOException {
  20. //行List,也是最终要返回的List
  21. List<HashMap<Integer,String>> rowList=new ArrayList<>();
  22. Sheet sheet = workbook.getSheetAt(0);
  23. if (sheet == null) {
  24. throw new IOException("创建Sheet失败!");
  25. }
  26. //开始遍历行
  27. for (int i=0;i<= sheet.getLastRowNum();i++){
  28. Row row = sheet.getRow(i);
  29. //列List
  30. HashMap<Integer,String> map = new HashMap<>();
  31. if(row == null){
  32. rowList.add(map);
  33. continue;
  34. }
  35. //转换为List数组
  36. for (int cellNum=0;cellNum<= row.getLastCellNum();cellNum++){
  37. Cell cell = row.getCell(cellNum);
  38. if (cell != null && cell.getCellTypeEnum() != CellType.STRING && HSSFDateUtil.isCellDateFormatted(cell)) {
  39. Date date = cell.getDateCellValue();
  40. if (HSSFDateUtil.isValidExcelDate(date.getTime())) {
  41. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  42. String value = sdf.format(date);
  43. map.put(cellNum,value);
  44. } else {
  45. map.put(cellNum,"");
  46. }
  47. continue;
  48. }
  49. if(cell != null){
  50. DataFormatter formatter = new DataFormatter();
  51. String phoneNumber = formatter.formatCellValue(cell);
  52. String value = fommartNum(phoneNumber.trim());
  53. map.put(cellNum,value);
  54. }
  55. }
  56. rowList.add(map);
  57. }
  58. return rowList;
  59. }
  60. public static List<HashMap<Integer,String>> getExcelRowList(MultipartFile multipartFile) throws IOException {
  61. return getExcelRowList(getExcelWorkBook(multipartFile));
  62. }
  63. public static List<HashMap<Integer,String>> getExcelRowList(InputStream inputStream) throws IOException {
  64. return getExcelRowList(getExcelWorkBook(inputStream));
  65. }
  66. private static String fommartNum(String value){
  67. try {
  68. if(isNumeric2(value) && value.contains(".")){
  69. return Double.valueOf(value).intValue() +"";
  70. }
  71. }catch (Exception e){
  72. return value;
  73. }
  74. return value;
  75. }
  76. public static boolean isNumeric2(String str) {
  77. return str != null && str.matches("-?\\d+(\\.\\d+)?");
  78. }
  79. // 综合解决方案示例
  80. public static LocalDate getCellLocalDate(Cell cell) {
  81. if (cell == null || cell.getCellType() == CellType.BLANK) {
  82. return null;
  83. }
  84. if (cell.getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cell)) {
  85. Date date = cell.getDateCellValue();
  86. if (DateUtil.getExcelDate(date) == 0) {
  87. return null;
  88. }
  89. return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
  90. }
  91. return null;
  92. }
  93. private static Workbook getExcelWorkBook(InputStream inputStream) throws IOException {
  94. return new XSSFWorkbook(inputStream);
  95. }
  96. private static Workbook getExcelWorkBook(MultipartFile multipartFile) throws IOException {
  97. InputStream inputStream=multipartFile.getInputStream();
  98. String originalFileName=multipartFile.getOriginalFilename();
  99. assert originalFileName != null;
  100. String fileType=originalFileName.substring(originalFileName.lastIndexOf(".")+1);
  101. if (fileType.equalsIgnoreCase("xls")) {
  102. //xls格式
  103. return new HSSFWorkbook(inputStream);
  104. } else {
  105. //xlsx格式
  106. return new XSSFWorkbook(inputStream);
  107. }
  108. }
  109. }