# -*- coding: utf-8 -*-
import xlwt


def write_excel():
    f = xlwt.Workbook()

    fenlei = ['一类','二类','三类','四类']
    data_list = [[1,2,3],[11,22,33,44],[111,222,333,444,555],[1111,2222,3333,4444,5555,6666]]


    # cell_overwrite_ok=True是为了可以让用户可以重复写内容用的
    sheet1 = f.add_sheet('Sheet1',cell_overwrite_ok=True)
    row0 = ["第一列","第二列","第三列","第四列"]
    # 写第一行
    for i in range(0,len(row0)):
        sheet1.write(0,i,row0[i])


    # 升级写法,加上字体风格。
    style = xlwt.XFStyle()
    font = xlwt.Font()
    font.name = 'Tahoma'
    font.bold = True
    font.italic = True
    font.underline = True


    # 合并单元格
    # write_merge的函数定义:
    # def write_merge(self, r1, r2, c1, c2, label="", style=Style.default_style):
    # 0 <= c1 <= c2 <= 255
    # 0 <= r1 <= r2 <= 65535
    index = 1
    for i in range(0,len(fenlei)):
        sheet1.write_merge(index, index + len(data_list[i]) - 1, 1, 1, fenlei[i], style)
        index = index + len(data_list[i])

    sheet1.col(1).width = 5000


    f.save('test1.xls')

if __name__ == '__main__':
    write_excel()

12-25 02:36