1

我能够使用 python pandas 生成一个 excel:

    df = pd.DataFrame(columns =["test"])
    writer = pd.ExcelWriter(f'{basepath}/Summary.xlsx', engine='xlsxwriter')
    df.to_excel(writer,engine='openpyxl',sheet_name= 'Sample')
    writer.save()

但是,当我尝试使用带有 HTTP 触发器的 azure 函数来触发它时,下载的 excel 会显示: 在此处输入图像描述

以下是我一直在使用的代码:

    df = pd.DataFrame(columns =["test"])
    writer = pd.ExcelWriter(f'{basepath}/Summary.xlsx', engine='xlsxwriter')
    df.to_excel(writer,engine='openpyxl',sheet_name= 'Sample')
    writer.save()
    response = func.HttpResponse(body=None,headers={'Content-Disposition':'attachment; filename='f'{basepath}/Summary.xlsx'}, mimetype='application/vnd.ms-excel')
    return response
4

1 回答 1

2

请尝试使用以下代码:

    df = pd.DataFrame(columns =["test"])
    writer = pd.ExcelWriter(f'{basepath}/Summary.xlsx', engine='xlsxwriter')
    df.to_excel(writer,engine='openpyxl',sheet_name= 'Sample')
    writer.save()

    with open(f'{basepath}/Summary.xlsx', "rb") as file:
        return func.HttpResponse(body=file.read(),headers={'Content-Disposition':'attachment; filename='f'{basepath}/Summary.xlsx'}, mimetype='application/vnd.ms-excel')
于 2021-05-24T01:55:25.813 回答