New file |
| | |
| | | package com.hx.util; |
| | | |
| | | |
| | | import com.hx.exception.TipsException; |
| | | import jxl.CellType; |
| | | import org.apache.poi.hssf.usermodel.*; |
| | | import org.apache.poi.ss.usermodel.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileOutputStream; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * Excel工具类(兼容poi4.0+) |
| | | */ |
| | | public class ExcelFileUtil { |
| | | |
| | | /** |
| | | * 读取 Excel文件内容 |
| | | * |
| | | * @param file |
| | | * @param header 是否包括表头 |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static List<List<String>> readExcelByeFileData(MultipartFile file, boolean header) throws Exception { |
| | | // 结果集 |
| | | List<List<String>> list = new ArrayList<>(); |
| | | if (file == null){ |
| | | return list; |
| | | } |
| | | String fileName = file.getOriginalFilename(); |
| | | if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) { |
| | | throw new TipsException("上传文件格式不正确"); |
| | | } |
| | | |
| | | Workbook hssfworkbook = WorkbookFactory.create(file.getInputStream()); |
| | | |
| | | int num = hssfworkbook.getNumberOfSheets(); |
| | | if (num == 0){ |
| | | return list; |
| | | } |
| | | List<String> rowList; |
| | | for (int k = 0; k< num ; k++) { |
| | | Sheet sheet = hssfworkbook.getSheetAt(k); |
| | | |
| | | for (int i = 0; i <= sheet.getLastRowNum(); i++) { |
| | | rowList = new ArrayList<>(); |
| | | // 获取第i行的数据 |
| | | Row row = sheet.getRow(i); |
| | | // getLastCellNum() 获取这一行中单元格的数量 |
| | | for (int j = 0; j < row.getLastCellNum(); j++) { |
| | | // 获取第i行第j列的单元格数据 |
| | | rowList.add(row.getCell(j).toString()); |
| | | } |
| | | list.add(rowList); |
| | | } |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 读取 Excel文件内容 |
| | | * |
| | | * @param file |
| | | * @param header 是否包括表头 |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static List<List<String>> readExcelByeFileData(File file, boolean header) throws Exception { |
| | | // 结果集 |
| | | List<List<String>> list = new ArrayList<>(); |
| | | if (file == null){ |
| | | return list; |
| | | } |
| | | String fileName = file.getName(); |
| | | if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) { |
| | | throw new TipsException("上传文件格式不正确"); |
| | | } |
| | | |
| | | Workbook hssfworkbook = WorkbookFactory.create(new FileInputStream(file)); |
| | | |
| | | int num = hssfworkbook.getNumberOfSheets(); |
| | | if (num == 0){ |
| | | return list; |
| | | } |
| | | List<String> rowList; |
| | | for (int k = 0; k< num ; k++) { |
| | | Sheet sheet = hssfworkbook.getSheetAt(k); |
| | | |
| | | for (int i = 0; i <= sheet.getLastRowNum(); i++) { |
| | | rowList = new ArrayList<>(); |
| | | // 获取第i行的数据 |
| | | Row row = sheet.getRow(i); |
| | | // getLastCellNum() 获取这一行中单元格的数量 |
| | | for (int j = 0; j < row.getLastCellNum(); j++) { |
| | | // 获取第i行第j列的单元格数据 |
| | | rowList.add(row.getCell(j).toString()); |
| | | } |
| | | list.add(rowList); |
| | | } |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | |
| | | |
| | | private static String objToString(Object obj) { |
| | | if (obj == null) { |
| | | return ""; |
| | | } else { |
| | | if (obj instanceof String) { |
| | | return (String) obj; |
| | | } else if (obj instanceof Date) { |
| | | return null; |
| | | } else { |
| | | return obj.toString(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | } |