【Python】生成Excel檔案
阿新 • • 發佈:2021-02-05
生成excel表格需要第三方庫:xlwt
安裝第三方庫:pip install xlwt
#coding:utf-8 #匯入函式庫 import xlwt #新建一個workbok workbook = xlwt.Workbook(encoding = 'utf-8') #加入一個工作表 sheet1 = workbook.add_sheet('Sheet1') #向單元格寫入資料 #sheet1.write(行號,列號,資料) #定義表頭(第一行) fields=['員工姓名','部門','性別'] idx=0 for x in fields: sheet1.write(0,idx,x) idx=idx+1 #寫入100條測試用的資料 for i in range(1,101): col=0 dt=['員工%d'%i,'工程部','男'] for x in fields: sheet1.write(i,col,dt[col]) col=col+1 #儲存Excel檔案 workbook.save("員工資訊.xls") #如果需要進行網路處理,也可以不儲存檔案,直接在內部中操作這個檔案 #output = io.BytesIO()x