python导出excel优劣点

Python013

python导出excel优劣点,第1张

优点:

1. 导出excel非常方便,可以节省时间和精力,因为只需要几行代码就可以完成。

2. 可以根据用户的需求自定义excel文件,比如可以定义表头、字体、单元格样式等,以满足用户的特殊需求。

3. 可以基于python控制excel中的数据,比如可以更新某一列的数据,添加或删除表格等。

缺点:

1. 可能存在兼容性问题,不同版本的Excel之间可能存在差异。

2. 由于Excel文件格式较复杂,所以生成Excel文件会比较耗时,可能会影响应用程序的性能。

一、数据查询方法(此项可根据实际业务需求更改)

二、数据生成excel

三、发送邮件方法介绍

导出excel如遇到下图报错:

1.排查编码 #coding:utf-8 sys.setdefaultencoding('utf8') 等

2.写入第一行数据的中文字符,或者字符串需要有引号,忽略会报错。

python导出数据到excel文件的方法:

1、调用Workbook()对象中的add_sheet()方法

1

2

wb = xlwt.Workbook()

ws = wb.add_sheet('A Test Sheet')

2、通过add_sheet()方法中的write()函数将数据写入到excel中,然后使用save()函数保存excel文件

1

2

3

4

5

6

7

ws.write(0, 0, 1234.56, style0)

ws.write(1, 0, datetime.now(), style1)

ws.write(2, 0, 1)

ws.write(2, 1, 1)

ws.write(2, 2, xlwt.Formula("A3+B3"))

wb.save('example.xls')

完整代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

import xlwtfrom datetime import datetime

style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on',num_format_str='#,##0.00')

style1 = xlwt.easyxf(num_format_str='D-MMM-YY')

wb = xlwt.Workbook()

ws = wb.add_sheet('A Test Sheet')

ws.write(0, 0, 1234.56, style0)

ws.write(1, 0, datetime.now(), style1)

ws.write(2, 0, 1)

ws.write(2, 1, 1)

ws.write(2, 2, xlwt.Formula("A3+B3"))

wb.save('example.xls')

程序执行结果如下:

更多Python知识,请关注:Python自学网!!

(推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)