{
"file": "OrderDetail-2020-06-03.xls",
"startRow": 1,
"table": "tk_bill",
"comitcount":50,
"map": {
"trade_id": "o",
"tk_status": "p",
"item_title": "h",
"alipay_total_price": "r",
"tk_paid_time": "c",
"pub_share_pre_fee": "ad",
"subsidy_fee": "w",
"tk_settletime": "d"
}
}
  1. file:要处理的表格文件
  2. startRow:从第几行开始导入
  3. table:导入到哪个表
  4. comitcount:多少笔一提交( 由于数据库对sql长度是有限制的,不建议设置太大)
  5. map:表字段与excel列的映射关系
import math
import xlrd
import json
from Db import Db db = Db() def loadConfig():
'''
加载配置
:return:
'''
with open("map.conf") as f:
return json.load(f) def chang26to10(value):
'''
26进制转10进制
'''
transfArray = {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8, 'I': 9, 'G': 10, 'K': 11, 'L': 12,
'M': 13,
'N': 14, 'O': 15, 'P': 16, 'Q': 17, 'R': 18, 'S': 19, 'T': 20, 'U': 21, 'V': 22, 'W': 23, 'X': 24,
'Y': 25, 'Z': 26};
sum = 0
length = len(value)
for idx in range(length):
char = value[idx]
if char.upper() not in transfArray:
raise Exception("无效字符:<%s>" % char)
v = transfArray[char.upper()]
tem = v * math.pow(26, length - idx - 1)
sum = tem + sum
return int(sum) if __name__ == '__main__':
importConfig = loadConfig()
# 获取要导入的表名
tableName = importConfig["table"]
comitcount = importConfig["comitcount"]
startRow = importConfig["startRow"]
cols = []
colsidx = []
for k, v in importConfig["map"].items():
cols.append(k)
colsidx.append(v)
sqlpre = "insert into %s(%s) values " % (tableName, ",".join(cols))
xls = xlrd.open_workbook(importConfig["file"])
sheet = xls.sheets()[0]
startIdx = 0
startIdx = startRow
vlist = []
for i in range(startIdx, sheet.nrows):
print("处理第%s行" % i)
temlist = [];
for cidx in colsidx:
s = "'%s'" % sheet.cell_value(i, chang26to10(cidx) - 1).replace("'", "''")
temlist.append(s)
vlist.append("(%s)" % ",".join(temlist))
# 每50行提交一次
if len(vlist) == comitcount:
sql = sqlpre + ",".join(vlist)
db.execSql(sql)
vlist.clear()
continue
if len(vlist) > 0:
sql = sqlpre + ",".join(vlist)
db.execSql(sql)
vlist.clear()
print("处理完成")

为了保证数据导入成功,建表时字段建议用varchar。(不然会有各种类型、非空之类的错误)

最新文章

  1. 开发 ASP.NET vNext 初步总结(使用Visual Studio 14 CTP1)
  2. nginx secure_link下载防盗链
  3. UVA 1324 The Largest Clique 最大团(强连通分量,变形)
  4. UITableView 滚动时隐藏键盘
  5. .NET Framework和C#版本历史概览
  6. 万恶的tileMap
  7. QQ_SingleTalkClient
  8. 【SignalR学习系列】6. SignalR Hubs Api 详解(C# Server 端)
  9. 将搜狗词库(.scel格式)转化为txt格式
  10. redis 高可用
  11. 【转载】 Java中String类型的两种创建方式
  12. testNG-失败用例重跑方法探究
  13. Lab 1-1
  14. 异步编程之asyncio简单介绍
  15. day3——关于&lt;s:if/&gt;和文件上传
  16. [Android Security] 静态分析Android程序——smali文件解析
  17. mac 地址
  18. ny49 开心的小明
  19. 《Linux内核与分析》第五周
  20. MySQL: tinyint(1) 和 tinyint(4), char 和varchar

热门文章

  1. 又拍云之 Keepalived 高可用部署
  2. 5种GaussDB ETCD服务异常实例分析处理
  3. DP?
  4. python进阶(29)单例模式
  5. 第2-4-6章 springboot整合规则引擎Drools-业务规则管理系统-组件化-中台
  6. &lt;一&gt;对象使用过程中背后调用了哪些方法
  7. 【PPT】NET Conf China 2022,主题:C#在iNeuOS工业互联网操作系统的开发及应用
  8. 【Java SE】Day04 IDEA、方法*
  9. TypeError: Object(…) is not a function
  10. linux基础第二部分