|
14 | 14 | from functools import reduce |
15 | 15 | from typing import List, Dict |
16 | 16 |
|
17 | | -import xlwt |
| 17 | +import openpyxl |
18 | 18 | from celery_once import AlreadyQueued |
19 | 19 | from django.core import validators |
20 | 20 | from django.db import transaction |
|
34 | 34 | from common.handle.impl.qa.xls_parse_qa_handle import XlsParseQAHandle |
35 | 35 | from common.handle.impl.qa.xlsx_parse_qa_handle import XlsxParseQAHandle |
36 | 36 | from common.handle.impl.table.csv_parse_table_handle import CsvSplitHandle |
37 | | -from common.handle.impl.table.xlsx_parse_table_handle import XlsxSplitHandle |
38 | 37 | from common.handle.impl.table.xls_parse_table_handle import XlsSplitHandle |
| 38 | +from common.handle.impl.table.xlsx_parse_table_handle import XlsxSplitHandle |
39 | 39 | from common.handle.impl.text_split_handle import TextSplitHandle |
40 | 40 | from common.mixins.api_mixin import ApiMixin |
41 | 41 | from common.util.common import post, flat_map |
@@ -490,25 +490,27 @@ def export(self, with_valid=True): |
490 | 490 | data_dict, document_dict = self.merge_problem(paragraph_list, problem_mapping_list, [document]) |
491 | 491 | workbook = self.get_workbook(data_dict, document_dict) |
492 | 492 | response = HttpResponse(content_type='application/vnd.ms-excel') |
493 | | - response['Content-Disposition'] = f'attachment; filename="data.xls"' |
| 493 | + response['Content-Disposition'] = f'attachment; filename="data.xlsx"' |
494 | 494 | workbook.save(response) |
495 | 495 | return response |
496 | 496 |
|
497 | 497 | @staticmethod |
498 | 498 | def get_workbook(data_dict, document_dict): |
499 | 499 | # 创建工作簿对象 |
500 | | - workbook = xlwt.Workbook(encoding='utf-8') |
| 500 | + workbook = openpyxl.Workbook() |
| 501 | + workbook.remove_sheet(workbook.active) |
501 | 502 | for sheet_id in data_dict: |
502 | 503 | # 添加工作表 |
503 | | - worksheet = workbook.add_sheet(document_dict.get(sheet_id)) |
| 504 | + worksheet = workbook.create_sheet(document_dict.get(sheet_id)) |
504 | 505 | data = [ |
505 | 506 | ['分段标题(选填)', '分段内容(必填,问题答案,最长不超过4096个字符)', '问题(选填,单元格内一行一个)'], |
506 | | - *data_dict.get(sheet_id) |
| 507 | + *data_dict.get(sheet_id, []) |
507 | 508 | ] |
508 | 509 | # 写入数据到工作表 |
509 | 510 | for row_idx, row in enumerate(data): |
510 | 511 | for col_idx, col in enumerate(row): |
511 | | - worksheet.write(row_idx, col_idx, col) |
| 512 | + cell = worksheet.cell(row=row_idx + 1, column=col_idx + 1) |
| 513 | + cell.value = col |
512 | 514 | # 创建HttpResponse对象返回Excel文件 |
513 | 515 | return workbook |
514 | 516 |
|
|
0 commit comments