首页 > 代码库 > 从 EXCEL 导入数据

从 EXCEL 导入数据

 1 import xlrd
 2 from xlrd.xldate import XLDateAmbiguous
 3 
 4 file = ch02-xlsxdata.xlsx
 5 
 6 wb = xlrd.open_workbook(filename=file)
 7 
 8 ws = wb.sheet_by_name(Sheet1)
 9 
10 dataset = []
11 
12 for r in range(ws.nrows):
13     col = []
14     for c in range(ws.ncols):
15         col.append(ws.cell(r, c).value)
16         if ws.cell_type(r, c) == xlrd.XL_CELL_DATE:
17             try:
18                 print ws.cell_type(r, c)
19                 from datetime import datetime
20                 date_value =http://www.mamicode.com/ xlrd.xldate_as_tuple(ws.cell(r, c).value, wb.datemode)
21                 print datetime(*date_value)
22             except XLDateAmbiguous as e:
23                 print e
24     dataset.append(col)
25 
26 from pprint import pprint
27 
28 pprint(dataset)

 

从 EXCEL 导入数据