excel导入数据到mysql数据库

比如需要把exel数据导入数据库,有很多种方法,比如如果能登录mysql服务器,可以使用load data,也可以借助比如etl工具kettle,也可以专门处理csv工具的python脚本等方法。

我常用的是我认为是比较高效的一种,直接写excel公式。

比如我需要向表 inventory_alert 插入数据,表结构如下:

CREATE TABLE inventory_alert (
  ID int(11) NOT NULL AUTO_INCREMENT COMMENT 主键ID,
  INVENTORY_CODE varchar(40) COLLATE utf8mb4_0900_as_cs NOT NULL COMMENT 存货编码,
  INVENTORY_NAME varchar(64) COLLATE utf8mb4_0900_as_cs NOT NULL COMMENT 存货名称,
  MAT_AVG decimal(10,0) DEFAULT NULL COMMENT 月均发货,
  MAT_MSD decimal(10,0) DEFAULT NULL COMMENT 月标准差,
  MAT_WSD decimal(10,0) DEFAULT NULL COMMENT 周标准差,
  TSM_AMOUNT decimal(10,0) DEFAULT NULL COMMENT 发货金额合计(万元)(MAT-12个月),
  TSM_PENCT decimal(10,0) DEFAULT NULL COMMENT 发货金额占比,
  TSM_APENCT decimal(10,0) DEFAULT NULL COMMENT 发货金额累计占比,
  VAR_COA decimal(10,0) DEFAULT NULL COMMENT 变异系数,
  THE_COA varchar(64) COLLATE utf8mb4_0900_as_cs DEFAULT NULL COMMENT 理论象限,
  QA_SET varchar(64) COLLATE utf8mb4_0900_as_cs DEFAULT NULL COMMENT 象限设置,
  WSLT decimal(10,0) DEFAULT NULL COMMENT 发运提前期(周),
  CSLF decimal(10,0) DEFAULT NULL COMMENT 客户服务水平系数,
  REMARK varchar(400) COLLATE utf8mb4_0900_as_cs DEFAULT NULL COMMENT 备注,
  SEC_INVENTORY decimal(10,0) DEFAULT NULL COMMENT 安全库存数量,
  AVA_INVENTORY decimal(10,0) DEFAULT NULL COMMENT 可用库存数量,
  SEC_AVA_INVENTORY decimal(10,0) DEFAULT NULL COMMENT 可用库存/安全库存,
  FA_REQ1 decimal(10,0) DEFAULT NULL COMMENT 未来需求1,
  FA_REQ2 decimal(10,0) DEFAULT NULL COMMENT 未来需求2,
  FA_REQ3 decimal(10,0) DEFAULT NULL COMMENT 未来需求3,
  MON_AVG decimal(10,0) DEFAULT NULL COMMENT 月均,
  SALES_SCORE decimal(10,0) DEFAULT NULL COMMENT 纯销指标合计,
  LTPA decimal(10,0) DEFAULT NULL COMMENT 库销比,
  RISK varchar(40) COLLATE utf8mb4_0900_as_cs DEFAULT NULL COMMENT 风险提示,
  PRIMARY KEY (INVENTORY_CODE,INVENTORY_NAME),
  KEY ID (ID)
) ENGINE=InnoDB AUTO_INCREMENT=62 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_as_cs

先把excel第一行字段与列明对其,我的比如有20+个字段,最开始公式拼接为:

="insert into inventory_alert(INVENTORY_CODE,INVENTORY_NAME,MAT_AVG,MAT_MSD,MAT_WSD,TSM_AMOUNT,TSM_PENCT,TSM_APENCT,VAR_COA,THE_COA,QA_SET,WSLT,CSLF,REMARK,SEC_INVENTORY,
AVA_INVENTORY,SEC_AVA_INVENTORY,F_REQ1,F_REQ2,F_REQ3,MON_AVG,SALES_SCORE,LTPA,RISK)values("&A2&","&B2&","&C2&","&D2&","&E2&","&F2&","&G2&","&H2&","&I2&","&J2&",
"&K2&","&L2&","&M2&","&N2&","&O2&","&P2&","&Q2&","&R2&","&S2&","&T2&","&U2&","&V2&","&W2&","&X2&");"

excel提示:

excel导入数据到mysql数据库

然后使用这个函数,找了一些用法,修改后:

=CONCATENATE("insert into inventory_alert(INVENTORY_CODE,INVENTORY_NAME,MAT_AVG,MAT_MSD,MAT_WSD,TSM_AMOUNT,TSM_PENCT,TSM_APENCT,VAR_COA,THE_COA,QA_SET,WSLT,CSLF,REMARK,SEC_INVENTORY,
AVA_INVENTORY,SEC_AVA_INVENTORY,F_REQ1,F_REQ2,F_REQ3,MON_AVG,SALES_SCORE,LTPA,RISK)values("&A2&","&B2&","&C2&","&D2&","&E2&","&F2&","&G2&","&H2&","&I2&","&J2&",
"&K2&","&L2&","&M2&","&N2&","&O2&","&P2&","&Q2&","&R2&","&S2&","&T2&","&U2&","&V2&","&W2&","&X2&");")

任然提示超出255个字符,想了一下,是excel这个格子里面我的这串字符串太多了,然后拆成2段拼接起来就好。

="insert into inventory_alert(INVENTORY_CODE,INVENTORY_NAME,MAT_AVG,MAT_MSD,MAT_WSD,TSM_AMOUNT,TSM_PENCT,TSM_APENCT,VAR_COA,THE_COA,QA_SET,WSLT,CSLF,REMARK,SEC_INVENTORY,
AVA_INVENTORY,SEC_AVA_INVENTORY,F_REQ1,F_REQ2,F_REQ3,MON_AVG,SALES_SCORE,LTPA,RISK)"

="values(‘"&A2&"‘,‘"&B2&"‘,‘"&C2&"‘,‘"&D2&"‘,‘"&E2&"‘,‘"&F2&"‘,‘"&G2&"‘,‘"&H2&"‘,‘"&I2&"‘,‘"&J2&"‘,"&K2&","&L2&","&M2&","&N2&","&O2&","&P2&","&Q2&","&R2&","&S2&","&T2&","&U2&","&V2&","&W2&","&X2&");"

最后再在后面一列,把这2列拼起来,比如我的这2列是Y与Z:

=""&Y2&"&"&Z2&""

然后在把拼接后的insert语句复制到gui执行就可以了。

select concat(update alertdb.inventory_alert ma set ma.,column_name,= null where ma.,column_name,=,‘‘‘‘‘;) from information_schema.COLUMNS where table_name=inventory_alert

这个是把excel里面的空字符串update为null,要不然看起来不好看。

excel导入数据到mysql数据库

上一篇:Oracle Database CHECK NUMBER For tables/views/triggers/function/procedure


下一篇:6. Connection has already been closed 数据库连接被关闭