excel寫入操作
阿新 • • 發佈:2018-09-01
not exc 保存 nco odi spa book write pat
字典列表類型數據寫入excel。
1 #導入xlwt庫 2 import xlwt 3 import os 4 # 步驟1:獲取excel文件的絕對路徑 5 dirPath = os.path.join(os.getcwd(),"data") 6 if not os.path.exists(dirPath): 7 os.mkdir(dirPath) 8 9 excelPath = os.path.join(dirPath,"data.xls") 10 # 步驟2:創建工作簿 workbook 11 12 workbook = xlwt.Workbook(encoding="utf-8") 13 # 步驟3:在工作簿中創建sheet單頁 14 worksheet = workbook.add_sheet("數據測試單頁") 15 # 步驟4:添加數據 16 #步驟4-1:設置表頭數據 17 headers=["字段1","字段2","字段3"] 18 #步驟4-2:循環輸入表頭數據 19 for colsIndex in range(len(headers)): 20 worksheet.write(0,colsIndex,headers[colsIndex]) 21 22 #步驟4-3:循環寫入其他表格內容 23 for rowIndex in range(1,4):24 for colsIndex in range(len(headers)): 25 worksheet.write(rowIndex,colsIndex,(rowIndex+colsIndex)) 26 # 步驟5:保存工作簿workbook 27 workbook.save(excelPath) 28 print("excel文件寫入完畢")
excel寫入操作