python写excel总结
2024-09-06 10:47:14
废话不说,直接上代码:
import xlrd
import xlwt
# 读excel然后写到mysql的套路
def updata_info():
book = xlrd.open_workbook("tb_yuzhao.xls")
sheet = book.sheet_by_name("sheet1") # 建立数据库连接
db = MySQLdb.connect(。。。。。。。)
# 游标对象,用于数据库逐行遍历
cursor = db.cursor() # 创建一个for循环迭代读取xls文件每行数据的, 从第二行开始是要跳过标题
for r in range(1, sheet.nrows):
code_name = sheet.cell(r, 0).value
multi_desc = sheet.cell(r, 3).value
# 执行sql语句
update_sql = 'update yuzhao set desc = "{0}" where code_name = "{1}"'.format(desc, code_name)
print update_sql
cursor.execute(update_sql)
cursor.close()
db.commit()
db.close()
print 'Done!' # 写excel的套路
def write_excel(filename, data):
book = xlwt.Workbook() # 创建excel对象
sheet = book.add_sheet('sheet1') # 添加一个表
c = 0 # 保存当前列
for d in data: # 取出data中的每一个元组存到表格的每一行
for index in range(len(d)): # 将每一个元组中的每一个单元存到每一列
sheet.write(c, index, d[index])
c += 1
book.save(filename) # 保存excel
最新文章
- es6小白学习笔记(一)
- [转]C/C++ 程序员必须收藏的资源大全
- maven web项目build失败
- hdu 1425 sort 解题报告
- jquery的监听事件和触发事件
- codeforces 682C Alyona and the Tree DFS
- 总结XX网app中webapp常见的前端错误。
- python----特性002
- 23. leetcode 169. Majority Element
- Project 2:传奇汉诺塔
- linux正确的关机方法
- centos7搭建zabbix3.0监控系统
- 正则求解@"; (?<;=^\[length=)(\d+)(?=\])";
- [ZJOI2006]超级麻将(可行性dp)
- Mac 建PHP 环境 及 配置 apache 默认目录
- C# SQLite数据库
- 1458 Common Subsequence
- ThinkPHP 3.2 用户注册邮箱验证激活帐号
- javascript随笔和常见的知识点
- C++ leetcode::two sum