import xlrd
import xlwt

class ExcelHelper:
    #读取excel,返回数组
    @classmethod
    def readExcel(self,filename):
        workbook = xlrd.open_workbook(filename)
        sheet = workbook.sheets()[0]
        rowcount = sheet.nrows
        data = [sheet.row_values(i) for i in range(rowcount)]
        for i in range(1,len(data)):
            data[i][0] = DateHelper.getdate(data[i][0])
        return data

    #写入excel
    @classmethod
    def writeXls(self,filePath,data):
        filename = xlwt.Workbook(encoding='utf-8')
        sheet = filename.add_sheet('sheet1',cell_overwrite_ok=True)
        rowcnt = len(data[1])
        colcnt = len(data[0])
        map(lambda cols:sheet.write(0,cols,data[0][cols]),range(colcnt))
        for rowid in range(rowcnt):
            for cols in range(colcnt):
                if data[1][rowid][cols] is None:
                    sheet.write(rowid+1,cols,'')
                else:
                    sheet.write(rowid+1,cols,str(data[1][rowid][cols]))
            # map(lambda cols : sheet.write(rowid+1,cols,str(data[1][rowid][cols])),range(colcnt))
        filename.save(filePath)

 

相关文章:

  • 2022-03-04
  • 2021-12-10
  • 2022-12-23
  • 2021-06-01
  • 2021-12-22
  • 2022-01-21
  • 2021-12-22
猜你喜欢
  • 2022-01-25
  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2021-05-20
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案