|
|
@@ -0,0 +1,115 @@
|
|
|
+package org.dromara.product;
|
|
|
+
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.sql.Date;
|
|
|
+
|
|
|
+public class DataToMysqlScript {
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ // 假设你的文件名为 "中车协议4.2 - 副本.xlsx"
|
|
|
+ String excelFilePath = "C:\\Users\\XiaoLu\\Desktop\\中车协议4.2 - 副本.xlsx";
|
|
|
+
|
|
|
+ try (FileInputStream fis = new FileInputStream(excelFilePath);
|
|
|
+ Workbook workbook = new XSSFWorkbook(fis)) {
|
|
|
+
|
|
|
+ Sheet sheet = workbook.getSheetAt(0); // 获取第一个Sheet
|
|
|
+
|
|
|
+ // 从第2行开始遍历(索引为1)
|
|
|
+ for (Row row : sheet) {
|
|
|
+ // 跳过标题行
|
|
|
+ if (row.getRowNum() == 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 读取单元格数据
|
|
|
+ // A列序号, B列项目编号(空), C列产品编号, D列产品名称, E列商品说明,
|
|
|
+ // M列协议价(中车), N列比价链接, G列规格型号, H列保质期
|
|
|
+ String productNo = getCellValueAsString(row.getCell(2)); // C列 (Index 2)
|
|
|
+ String externalPrice = getCellValueAsString(row.getCell(12)); // M列 (Index 12)
|
|
|
+ String productDesc = getCellValueAsString(row.getCell(4)); // E列 (Index 4)
|
|
|
+ String refLink = getCellValueAsString(row.getCell(13)); // N列 (Index 13)
|
|
|
+ String specCode = getCellValueAsString(row.getCell(6)); // G列 (Index 6)
|
|
|
+ String shelfLife = getCellValueAsString(row.getCell(7)); // H列 (Index 7)
|
|
|
+
|
|
|
+ // 如果产品编号为空,跳过该行
|
|
|
+ if (productNo.trim().isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成 SQL 语句
|
|
|
+ // 注意:SQL 中的字符串值需要单引号包裹
|
|
|
+ // 需要对内容中的单引号进行转义 (SQL 中单引号转义是 ''
|
|
|
+ String sql1 = String.format(
|
|
|
+ "UPDATE external_product SET external_price = '%s' WHERE product_no = '%s';",
|
|
|
+ escapeSql(externalPrice),
|
|
|
+ escapeSql(productNo)
|
|
|
+ );
|
|
|
+
|
|
|
+ String sql2 = String.format(
|
|
|
+ "UPDATE product_extend " +
|
|
|
+ "SET product_description = '%s', " +
|
|
|
+ "reference_link = '%s', " +
|
|
|
+ "specifications_code = '%s' " +
|
|
|
+ "WHERE product_id = (SELECT id FROM product_base WHERE product_no = '%s');",
|
|
|
+ escapeSql(productDesc),
|
|
|
+ escapeSql(refLink),
|
|
|
+ escapeSql(specCode),
|
|
|
+ escapeSql(productNo)
|
|
|
+ );
|
|
|
+
|
|
|
+ // 第三条 SQL 是插入保质期属性
|
|
|
+ // 注意:如果保质期字段为空,默认设为 '12个月',根据你的数据看大部分是12个月
|
|
|
+ String finalShelfLife = shelfLife.isEmpty() ? "12个月" : shelfLife;
|
|
|
+ String sql3 = String.format(
|
|
|
+ "INSERT INTO `yoe_product_db`.`product_classification_diy` (`product_id`, `attribute_key`, `attribute_value`) " +
|
|
|
+ "VALUES ((SELECT id FROM product_base WHERE product_no = '%s'), '保质期', '%s');",
|
|
|
+ escapeSql(productNo),
|
|
|
+ escapeSql(finalShelfLife)
|
|
|
+ );
|
|
|
+
|
|
|
+ // 输出 SQL
|
|
|
+ System.out.println("-- 处理产品: " + productNo);
|
|
|
+ System.out.println(sql1);
|
|
|
+// System.out.println(sql2);
|
|
|
+// System.out.println(sql3);
|
|
|
+ System.out.println(); // 空行分隔
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 辅助方法:将 Cell 转换为字符串
|
|
|
+ private static String getCellValueAsString(Cell cell) {
|
|
|
+ if (cell == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ // 处理数字格式(如价格、编号)
|
|
|
+ if (cell.getCellType() == CellType.NUMERIC) {
|
|
|
+ // 如果是日期格式,需要特殊处理,这里简单按数字处理
|
|
|
+ if (DateUtil.isCellDateFormatted(cell)) {
|
|
|
+ return cell.getDateCellValue().toString();
|
|
|
+ } else {
|
|
|
+ // 防止科学计数法,转换为普通字符串
|
|
|
+ return String.valueOf(cell.getNumericCellValue());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 其他类型(STRING, BOOLEAN, FORMULA等)直接取字符串
|
|
|
+ return cell.toString().trim();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 辅助方法:SQL 转义,防止 SQL 注入或语法错误
|
|
|
+ private static String escapeSql(String value) {
|
|
|
+ if (value == null || value.isEmpty()) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ // SQL 中单引号 ' 需要转义为 ''
|
|
|
+ return value.replace("'", "''");
|
|
|
+ }
|
|
|
+}
|