6.通过xlwt写入xlsx文件

#encoding:utf-8

"""
目标:将一组数组写入倒XLS文件中
"""

import xlwt

class myWriteXls():
    def __init__(self,filename):
        self.filename = filename
        self.writexls()
    def writexls(self):
        #新建Excel
        exfile = xlwt.Workbook()
        #给第一个sheet赋予名称
        sheetname = exfile.add_sheet("BOM Mapping List")
        #给第一个sheet写入第一行头部内容
        tabtopName = ['81BOM', '70 HSG Bin1', '70 HSG Bin2', '70 HSG Bin3', '70 HSG Bin4', '70 HSG Bin5', '70 HSG Bin6', '70 MLB Bin1\n(AT&S)-MP', '70 MLB Bin2\n(UMT)-MP', '70 MLB Bin3\n(AVA1)-MP', '70 MLB Bin4\n(AVA A8)-MP']
        j=0
        for topname in tabtopName:
            sheetname.write(0,j,topname)
            j+=1
        #写入数据内容
        lie=-1
        hang=1
        '''一行数据的插入方法'''
#         rows = ['81','82','83','84','85','86','87','88','89','89-10','89-11']
#         for row in rows:
#             sheetname.write(hang,lie,row)
#             lie+=1
#         exfile.save(self.filename)   
#         print("excel创建成功!")
        '''一列数据的插入方法'''
#         rows = ['81','82','83','84','85','86','87','88','89','89-10','89-11']
#         for row in rows:
#             sheetname.write(hang,lie,row)
#             hang+=1
            
        '''多维数组的插入方法'''
        dwRows=[['81','82','83','84','85','86','87','88','89','89-10','89-11'],['71','72','73','74','75','76','77','78','79','79-10','79-11']]        
        for rows in dwRows:
            lie+=1
            for row in rows:
                print(row)
                sheetname.write(hang,lie,row)
                hang+=1
            hang=1
        print("excel创建成功!")
        
        exfile.save(self.filename)
if __name__ == '__main__':
    filepath = 'Wtest81.xlsx'#这样会直接放在src下面
    filename = '../W81text.xlsx'
    oxl = myWriteXls(filename)

 

posted @ 2022-03-18 13:17  种太阳  阅读(559)  评论(0编辑  收藏  举报