From 6dd660c9fcc78050c9b36e88e901c6b3f4a5e31c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Mon, 14 Aug 2023 15:29:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E8=AF=BB=E5=8F=96excel=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 + .../kwan/springbootkwan/entity/ExcelData.java | 53 +++++++++ .../springbootkwan/utils/ExcelReader.java | 110 ++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 src/main/java/com/kwan/springbootkwan/entity/ExcelData.java create mode 100644 src/main/java/com/kwan/springbootkwan/utils/ExcelReader.java diff --git a/pom.xml b/pom.xml index d018fac..aea2df3 100644 --- a/pom.xml +++ b/pom.xml @@ -181,6 +181,12 @@ mapstruct ${mapstruct.version} + + + org.apache.poi + poi-ooxml + 4.1.2 + diff --git a/src/main/java/com/kwan/springbootkwan/entity/ExcelData.java b/src/main/java/com/kwan/springbootkwan/entity/ExcelData.java new file mode 100644 index 0000000..d6d4b3c --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/entity/ExcelData.java @@ -0,0 +1,53 @@ +package com.kwan.springbootkwan.entity; + +import lombok.Data; + +/** + * excel实体类 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/8/14 14:59 + */ +@Data +public class ExcelData { + private String product_id; + private String productName; + private String categoryName2; + private String genderName; + private double productYearName; + private String seasonName; + private String colorName; + private String styleName; + private double avg_sal_price; + private double gross_margin; + private double total7_size_store_day; + private double discount; + private double sal_store_count; + private double day_7_sal_qty_store_rate_rank; + private double inv_store_qty; + private String ondesk_date; + private double inv_store_ratio; + private double avg_now_price; + private double all_store_num; + private double day_7_size_store_day_rank; + private double total_ondesk_day; + private double total_replenish_qty; + private double initial_order_qty; + private double arrive_qty; + private double sal_out_rate; + private double total_arrive_qty; + private double inv_out_qty; + private double total_sal_rank; + private double total_sal_qty; + private double inv_qty; + private double replenish_not_arrive_qty; + private String counter_date; + private double total_sal_amt; + private double total_discount; + private double sal_qty; + private double inv_store_count; + private double total_purchase_qty; + private double day_30_size_store_day_rank; + private double inv_sal_ratio; +} diff --git a/src/main/java/com/kwan/springbootkwan/utils/ExcelReader.java b/src/main/java/com/kwan/springbootkwan/utils/ExcelReader.java new file mode 100644 index 0000000..98a7acc --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/utils/ExcelReader.java @@ -0,0 +1,110 @@ +package com.kwan.springbootkwan.utils; + +import com.kwan.springbootkwan.entity.ExcelData; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +import java.io.FileInputStream; +import java.io.IOException; +import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Objects; + + +/** + * 读取excel工具类 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/8/14 15:28 + */ +public class ExcelReader { + public static List readExcel(String filePath) { + List dataList = new ArrayList<>(); + try (FileInputStream fis = new FileInputStream(filePath); + Workbook workbook = new XSSFWorkbook(fis)) { + Sheet sheet = workbook.getSheetAt(0); + Iterator rowIterator = sheet.iterator(); + // Skip the header row + if (rowIterator.hasNext()) { + rowIterator.next(); + } + //忽略第二行 + if (rowIterator.hasNext()) { + rowIterator.next(); + } + while (rowIterator.hasNext()) { + Row row = rowIterator.next(); + final ExcelData excelDataFromRow = createExcelDataFromRow(row); + if (Objects.nonNull(excelDataFromRow)) { + dataList.add(excelDataFromRow); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + return dataList; + } + + private static ExcelData createExcelDataFromRow(Row row) { + ExcelData data = new ExcelData(); + final Cell cell = row.getCell(0); + if (Objects.isNull(cell)) { + return null; + } + DecimalFormat decimalFormat = new DecimalFormat("0"); + data.setProduct_id(decimalFormat.format(cell.getNumericCellValue())); + data.setProductName(row.getCell(1).getStringCellValue()); + data.setCategoryName2(row.getCell(2).getStringCellValue()); + data.setGenderName(row.getCell(3).getStringCellValue()); + data.setProductYearName(row.getCell(4).getNumericCellValue()); + data.setSeasonName(row.getCell(5).getStringCellValue()); + data.setColorName(row.getCell(6).getStringCellValue()); + data.setStyleName(row.getCell(7).getStringCellValue()); + data.setAvg_sal_price(row.getCell(8).getNumericCellValue()); + data.setGross_margin(row.getCell(9).getNumericCellValue()); + data.setTotal7_size_store_day(row.getCell(10).getNumericCellValue()); + data.setDiscount(row.getCell(11).getNumericCellValue()); + data.setSal_store_count(row.getCell(12).getNumericCellValue()); + data.setDay_7_sal_qty_store_rate_rank(row.getCell(13).getNumericCellValue()); + data.setInv_store_qty(row.getCell(14).getNumericCellValue()); + data.setOndesk_date(row.getCell(15).getStringCellValue()); + data.setInv_store_ratio(row.getCell(16).getNumericCellValue()); + data.setAvg_now_price(row.getCell(17).getNumericCellValue()); + data.setAll_store_num(row.getCell(18).getNumericCellValue()); + data.setDay_7_size_store_day_rank(row.getCell(19).getNumericCellValue()); + data.setTotal_ondesk_day(row.getCell(20).getNumericCellValue()); + data.setTotal_replenish_qty(row.getCell(21).getNumericCellValue()); + data.setInitial_order_qty(row.getCell(22).getNumericCellValue()); + data.setArrive_qty(row.getCell(23).getNumericCellValue()); + data.setSal_out_rate(row.getCell(24).getNumericCellValue()); + data.setTotal_arrive_qty(row.getCell(25).getNumericCellValue()); + data.setInv_out_qty(row.getCell(26).getNumericCellValue()); + data.setTotal_sal_rank(row.getCell(27).getNumericCellValue()); + data.setTotal_sal_qty(row.getCell(28).getNumericCellValue()); + data.setInv_qty(row.getCell(29).getNumericCellValue()); + data.setReplenish_not_arrive_qty(row.getCell(30).getNumericCellValue()); + data.setCounter_date(row.getCell(31).getStringCellValue()); + data.setTotal_sal_amt(row.getCell(32).getNumericCellValue()); + data.setTotal_discount(row.getCell(33).getNumericCellValue()); + data.setSal_qty(row.getCell(34).getNumericCellValue()); + data.setInv_store_count(row.getCell(35).getNumericCellValue()); + data.setTotal_purchase_qty(row.getCell(36).getNumericCellValue()); + data.setDay_30_size_store_day_rank(row.getCell(37).getNumericCellValue()); + data.setInv_sal_ratio(row.getCell(38).getNumericCellValue()); + return data; + } + + public static void main(String[] args) { + String filePath = "/Users/qinyingjie/Downloads/商品运营助手_滴小R_样例数据_edited.xlsx"; + List dataList = readExcel(filePath); + for (ExcelData data : dataList) { + System.out.println(data); + } + } +} \ No newline at end of file -- GitLab