我正在使用XlsxWriter(版本1.0.2)来创建Excel文件,但是当我导出并打开它时,会显示错误:“我们发现filename.xlsx中的某些内容存在问题。你想让我们尽可能多地恢复吗?如果您信任此工作簿的来源,请单击是。“当我单击”是“按钮并在”关闭“之后,Excel打开并且一切正常,因此我无法理解为什么会显示此错误。我使用的是Python 3.6.4,Django 2.0.1,Microsoft Office 2016,Windows 10和模块IO的Python。我想知道如何解决这个问题。
评论后添加了代码:
def testError(request):
data = [["Goal 1", 0, 0], ["1", "Activity 1", "Alex, Peter"], ["1.1", "Activity 1.1", "Matthew"], ["1.1.1", "Activity 1.1.1", "Jhon"]]
output = io.BytesIO()
workbook = xlsxwriter.Workbook(output, {'in_memory': True})
worksheet = workbook.add_worksheet("Sheet1")
bold = workbook.add_format({'bold': True})
title = 'Example'
merge_format = workbook.add_format({
'bold': 1,
'align': 'left',
'valign': 'vcenter'})
worksheet.merge_range('A1:C1', title, merge_format)
a = 'B3:D'
a += str(len(data)+4)
worksheet.add_table(a, {'columns': [{'header': 'No'}, {'header': 'ACTIVITY'}, {'header': 'PARTAKERS'}], 'banded_rows': False, 'banded_columns': False, 'autofilter': 0})
border_format = workbook.add_format({
'border': 1,
'align': 'left',
'font_size': 10
})
merge_format = workbook.add_format({
'bold': 1,
'border': 1,
'align': 'left',
'valign': 'vcenter',
})
header_format = workbook.add_format({
'bold': 1,
'align': 'center',
'valign': 'vcenter',
})
tab = 'B3:D'
tab += str(len(data)+3)
worksheet.autofilter(tab)
it = 0
countElem = (len(data))
max_width_number = 0
max_width_name = 0
max_width_partaker = 0
for ite in range(3, countElem+3):
cel = 'B'
cel += str(ite+1)
if (data[it][0] != 0 and data[it][1] == 0 and data[it][2] == 0):
worksheet.write_row(cel, data[it])
iniCel = cel
finCel = 'D'
finCel += str(ite+1)
mergecel = iniCel+':'+finCel
worksheet.merge_range(mergecel, data[it][0], merge_format)
else:
i = 1
for d in data[it]:
worksheet.write_rich_string((str('B'+str(ite+1))), str(data[it][0]))
if len(str(data[it][0])) > max_width_number:
max_width_number = len(str(data[it][0]))
worksheet.write_rich_string((str('C'+str(ite+1))), str(data[it][1]))
if len(data[it][1]) > max_width_name:
max_width_name = len(data[it][1])
worksheet.write((str('D'+str(ite+1))), data[it][2])
if len(data[it][2]) > max_width_partaker:
max_width_partaker = len(data[it][2])
it = it+1
worksheet.conditional_format(a, {'type': 'no_blanks', 'format': border_format})
worksheet.conditional_format('B3:D3', {'type': 'no_blanks', 'format': header_format})
worksheet.set_column(1, 1, max(5, max_width_number))
worksheet.set_column(2, 2, max(20.57, max_width_name))
worksheet.set_column(3, 3, max(8.29, max_width_partaker))
workbook.close()
output.seek(0)
response = HttpResponse(output.read(), content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
response['Content-Disposition'] = str("attachment; filename= workbook_example.xlsx")
return response
答案 0 :(得分:1)
您的代码中存在两个问题。
首先,您无法合并表格中的单元格。 Excel不允许它。所以删除这一行:
worksheet.merge_range(mergecel, data[it][0], merge_format)
此外,您不应该像这样在表格中添加自动过滤器:
worksheet.autofilter(tab)
而是使用add_table()
的自动过滤属性:
worksheet.add_table(a, {..., 'autofilter': True})
之后,工作簿将在没有警告的情况下打开并显示如下:
您可能还需要调整表格范围。注意,您不需要像这样为表创建范围:
tab = 'B3:D'
tab += str(len(data)+3)
add_table()
方法以及XlsxWriter中的几乎所有其他方法都采用(row, col)
表示法see the docs。