写一个xls文件的瓶框架

时间:2014-04-04 21:29:21

标签: python bottle xlsxwriter

我需要使用瓶子框架创建一个显示xls文件。

这是我当前的代码,它打开一个写内容“Hello World”..我的真正的word文件将包含很多行..

如何有效地写出所有内容?

我当前的视图在xls工作表中打印单个单元格。

@app.route('/SimpleCSV',method='GET')
def createxls():
 xls = ""
    import StringIO
    import xlsxwriter
    output = StringIO.StringIO()

    workbook = xlsxwriter.Workbook(output, {'in_memory':True})
    worksheet = workbook.add_worksheet()
    worksheet.write(0,0,'Hello World')
    worksheet.write(0,1,'This is prem')
    workbook.close()

    output.seek(0)

    print 'Excel file created and now will be returned'
    response.headers['Content-Type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8'
    response.headers['Content-Disposition'] = 'attachment; filename=text.xls'

    return output.read()

WOrking Code:

@bottle.route('/xl')
def create_xls():
    import StringIO
    import xlsxwriter
    output = StringIO.StringIO()

    workbook = xlsxwriter.Workbook(output, {'in_memory':True})
    worksheet = workbook.add_worksheet()
    for each in range(100):
        for each1 in range(100):
                worksheet.write(each,each1,"Test")
    workbook.close()

    output.seek(0)

    response.headers['Content-Type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8'
    response.headers['Content-Disposition'] = 'attachment; filename=text1.xlsx'
    return output.read()

0 个答案:

没有答案