| | |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | /** |
| | |
| | | |
| | | @Resource |
| | | private FileService fileService; |
| | | |
| | | |
| | | /** |
| | | * 读取Excel文件内容 |
| | | * |
| | | * @param |
| | | * @return |
| | | */ |
| | | public List<InoutRecord> readExcel(String fileName) { |
| | | |
| | | //获取文件后缀名并判断 |
| | | String fileType = fileName.substring(fileName.lastIndexOf(".") + 1); |
| | | |
| | | //获取保存路径 |
| | | String path = fileService.getFileSavePath("TEMP"); |
| | | path = path + fileName; |
| | | |
| | | Workbook workbook = null; |
| | | FileInputStream inputStream = null; |
| | | |
| | | try { |
| | | // 获取Excel文件 |
| | | File excelFile = new File(path); |
| | | if (!excelFile.exists()) { |
| | | return null; |
| | | } |
| | | // 获取Excel工作簿 |
| | | inputStream = new FileInputStream(excelFile); |
| | | workbook = getWorkbook(inputStream, fileType); |
| | | |
| | | // 读取excel中的数据 |
| | | return parseExcel(workbook); |
| | | } catch (Exception e) { |
| | | return null; |
| | | } finally { |
| | | try { |
| | | if (null != inputStream) { |
| | | inputStream.close(); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 解析Excel数据 |
| | |
| | | |
| | | /** |
| | | * 根据文件后缀名类型获取对应的工作簿对象 |
| | | * |
| | | * @param inputStream 读取文件的输入流 |
| | | * @param fileType 文件后缀名类型(xls或xlsx) |
| | | * @return 包含文件数据的工作簿对象 |
| | |
| | | cell.setCellType(CellType.STRING); |
| | | String time = cell.getStringCellValue().trim().replaceAll(" ",""); |
| | | if(StringUtils.isNotEmpty(time)){ |
| | | resultData.setRegisterTime(DateUtils.parseDate(time,"yyyy-MM-dd")); |
| | | |
| | | resultData.setRegisterTime(parseFlexibleDate(time)); |
| | | } |
| | | } |
| | | |
| | |
| | | return resultData; |
| | | } |
| | | |
| | | /** |
| | | * 多种格式解析字符串日期 |
| | | * |
| | | * @param dateStr 日期字符串 |
| | | * @author sgj |
| | | * @since 2026/03/10 |
| | | */ |
| | | private static Date parseFlexibleDate(String dateStr) { |
| | | if (StringUtils.isEmpty(dateStr)) { |
| | | return null; |
| | | } |
| | | |
| | | // 定义支持的日期格式 |
| | | String[] parsePatterns = new String[]{ |
| | | "yyyy-MM-dd", // 2026-01-06 |
| | | "yyyy/MM/dd", // 2026/01/06 |
| | | // "yyyy年MM月dd日", // 2026 年 01 月 06 日 |
| | | "yyyy-MM-dd HH:mm:ss", // 2026-01-06 12:00:00 |
| | | "yyyy/MM/dd HH:mm:ss", // 2026/01/06 12:00:00 |
| | | "yyyy-MM-dd HH:mm", // 2026-01-06 12:00 |
| | | "yyyy/MM/dd HH:mm" // 2026/01/06 12:00 |
| | | }; |
| | | |
| | | try { |
| | | return DateUtils.parseDateStrictly(dateStr, parsePatterns); |
| | | } catch (Exception e) { |
| | | // 如果所有格式都解析失败,返回 null |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读取Excel文件内容 |
| | | * |
| | | * @param |
| | | * @return |
| | | */ |
| | | public List<InoutRecord> readExcel(String fileName) { |
| | | |
| | | //获取文件后缀名并判断 |
| | | String fileType = fileName.substring(fileName.lastIndexOf(".") + 1); |
| | | |
| | | //获取保存路径 |
| | | String path = fileService.getFileSavePath("TEMP"); |
| | | path = path + fileName; |
| | | |
| | | Workbook workbook = null; |
| | | FileInputStream inputStream = null; |
| | | |
| | | try { |
| | | // 获取Excel文件 |
| | | File excelFile = new File(path); |
| | | if (!excelFile.exists()) { |
| | | return null; |
| | | } |
| | | // 获取Excel工作簿 |
| | | inputStream = new FileInputStream(excelFile); |
| | | workbook = getWorkbook(inputStream, fileType); |
| | | |
| | | // 读取excel中的数据 |
| | | return parseExcel(workbook); |
| | | } catch (Exception e) { |
| | | return null; |
| | | } finally { |
| | | try { |
| | | if (null != inputStream) { |
| | | inputStream.close(); |
| | | } |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } |