如何使用Python导出Excel文件?

时间:2016-07-22 17:27:06

标签: javascript python django excel python-2.7

我想使用Python导出Excel文件。我正在将csv数据发送到我的代码并制作Excel文件。文件被保存到数据成功的特定位置。但它没有显示为下载,因此用户知道文件已下载。

有一些额外的代码可以在Excel工作表的某些列上进行验证:

def downloadUploadExclusionDetailReport(request):
    csv_data=request.GET.get('csv_data')
    createExcel(csv_data)
    return HttpResponse(csv_data, content_type="application/text")      

def createExcel(data):
    wb=openpyxl.Workbook()
    WorkSheet=wb.create_sheet(title="report")
    data=data.split('|')
    row_count=1
    col_count=1
    for row in data:
        row=row.split(',')
        col_count=1
        for cell in row:
            WorkSheet.cell(row=row_count, column=col_count).value=cell
            col_count=col_count+1
        row_count=row_count+1
    ########Add Checked / Unchecked 
    dv1 = DataValidation(type="list", formula1='"checked,unchecked"', allow_blank=False)
    # Optionally set a custom error message
    dv1.error ='Your entry is not in the list'
    dv1.errorTitle = 'Invalid Entry'
    # Optionally set a custom prompt message
    dv1.prompt = 'Please select from the list'
    dv1.promptTitle = 'List Selection'
    # Add the data-validation object to the worksheet
    WorkSheet.add_data_validation(dv1)
    dv1.ranges.append('A2:A1048576')

    ########Add Exclusion Reason List
    dv2 = DataValidation(type="list", formula1='"limited_by_machine_size,fixed_cycle_time,moq_cannot_be_renegotiated,lt_cannot_be_renegotitaed,vendor_refused_to_be_vendor_consignment,quality_issue_dont_have_short_term_solution,fix_import_export_time,special_requirement_from_customer,new_product,others"', allow_blank=True)
    # Optionally set a custom error message
    dv2.error ='Your entry is not in the list'
    dv2.errorTitle = 'Invalid Entry'
    # Optionally set a custom prompt message
    dv2.prompt = 'Please select from the list'
    dv2.promptTitle = 'List Selection'
    # Add the data-validation object to the worksheet
    WorkSheet.add_data_validation(dv2)
    dv2.ranges.append('B2:B1048576')

    ########Add Action Category List
    dv3 = DataValidation(type="list", formula1='"Max_Benefit,MTO-MTS,Vendor_Consignment,Lead_Time,Prod_Cycle_Time,MOQ,Batch_Size,Quality_Issue"', allow_blank=True)
    # Optionally set a custom error message
    dv3.error ='Your entry is not in the list'
    dv3.errorTitle = 'Invalid Entry'
    # Optionally set a custom prompt message
    dv3.prompt = 'Please select from the list'
    dv3.promptTitle = 'List Selection'
    # Add the data-validation object to the worksheet
    WorkSheet.add_data_validation(dv3)
    dv3.ranges.append('C2:C1048576')

    wb.save('C:\Python27\henkel_supply_chain\\fuploads\Template Ver 2.xlsx')

0 个答案:

没有答案