# coding: UTF-8 from pyExcelerator.ImportXLS import parse_xls from pyExcelerator import Workbook import string class ExcelHandler(): #读取excel文件的内容,返回一个包含行列的集合 #@param file_full_name: #@return: sheet_list[[0,[a,a,a]],[1,[b,b,b]],[2,[c,c,c]],[3,[d,d,d]]] def read_xls(self, file_full_name): results = [] #获得excel的所有"谢特" sheets = parse_xls(file_full_name) #迭代"谢特"-----------"谢特"[0]是"谢特"的名字."谢特"[1]是内容 for t in range(len(sheets)): sheet = sheets[t] result = [] #获得谢特[1]的长度,即,列数乘以行数的值 #已知有13个列,根据这个谢特[1]的长度得到一共有多少行 length = len(sheet[1]) / 13 #先创建一个满足excel格数的空数组 for ti in range(length + 1): #@UnusedVariable result.append([" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "]) #迭代该谢特中的单元格,按照坐标插入到结果集中 for point in sheet[1]: str_point = str(point) #从谢特中获得行和列 _x = string.atoi(str_point.replace(" ", "").split(',')[0].replace("(", "")) _y = string.atoi(str_point.replace(" ", "").split(',')[1].replace(")", "")) try: if (str(_y) == '12') | (str(_y) == '1') | (str(_y) == '2') | (str(_y) == '4') | (str(_y) == '7'): if unicode(sheet[1][point]).find(".0") >= 0: result[_x][_y] = unicode(sheet[1][point]).replace(".0", "") continue; result[_x][_y] = unicode(sheet[1][point]) except: print str(_x)+","+str(_y)+"---"+str(len(result))+"---"+str(t) results.append([sheet[0], result]) return results #根据一个包含所有谢特的集合生成Excel文件 #@param sheet_list: sheet_list[[0,[a,a,a]],[1,[b,b,b]],[2,[c,c,c]],[3,[d,d,d]]] #@param save_file_name: def create_sheet_list(self, sheet_list, save_file_name): wb = Workbook() for sheet in sheet_list: ws = wb.add_sheet(sheet[0]) # 写内容 for i in range(len(sheet[1])): line = sheet[1][i] for j in range(len(line)): item = line[j] ws.write(i , j, item) wb.save(save_file_name) ################# ## 测试 读取--导出 ################# #eh = ExcelHandler() #results = eh.read_xls('./datafiles/finallyLIYIN.xls') #eh.create_sheet_list(results, "./hehehe.xls")