Skip to content

Commit 9aeb68c

Browse files
committed
fix: 修复段落过长导出知识库报错
1 parent 26a551b commit 9aeb68c

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

apps/dataset/serializers/dataset_serializers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ def export_excel(self, with_valid=True):
681681
document_list)
682682
workbook = DocumentSerializers.Operate.get_workbook(data_dict, document_dict)
683683
response = HttpResponse(content_type='application/vnd.ms-excel')
684-
response['Content-Disposition'] = 'attachment; filename="dataset.xls"'
684+
response['Content-Disposition'] = 'attachment; filename="dataset.xlsx"'
685685
workbook.save(response)
686686
return response
687687

apps/dataset/serializers/document_serializers.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from functools import reduce
1515
from typing import List, Dict
1616

17-
import xlwt
17+
import openpyxl
1818
from celery_once import AlreadyQueued
1919
from django.core import validators
2020
from django.db import transaction
@@ -34,8 +34,8 @@
3434
from common.handle.impl.qa.xls_parse_qa_handle import XlsParseQAHandle
3535
from common.handle.impl.qa.xlsx_parse_qa_handle import XlsxParseQAHandle
3636
from common.handle.impl.table.csv_parse_table_handle import CsvSplitHandle
37-
from common.handle.impl.table.xlsx_parse_table_handle import XlsxSplitHandle
3837
from common.handle.impl.table.xls_parse_table_handle import XlsSplitHandle
38+
from common.handle.impl.table.xlsx_parse_table_handle import XlsxSplitHandle
3939
from common.handle.impl.text_split_handle import TextSplitHandle
4040
from common.mixins.api_mixin import ApiMixin
4141
from common.util.common import post, flat_map
@@ -490,25 +490,27 @@ def export(self, with_valid=True):
490490
data_dict, document_dict = self.merge_problem(paragraph_list, problem_mapping_list, [document])
491491
workbook = self.get_workbook(data_dict, document_dict)
492492
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"'
494494
workbook.save(response)
495495
return response
496496

497497
@staticmethod
498498
def get_workbook(data_dict, document_dict):
499499
# 创建工作簿对象
500-
workbook = xlwt.Workbook(encoding='utf-8')
500+
workbook = openpyxl.Workbook()
501+
workbook.remove_sheet(workbook.active)
501502
for sheet_id in data_dict:
502503
# 添加工作表
503-
worksheet = workbook.add_sheet(document_dict.get(sheet_id))
504+
worksheet = workbook.create_sheet(document_dict.get(sheet_id))
504505
data = [
505506
['分段标题(选填)', '分段内容(必填,问题答案,最长不超过4096个字符)', '问题(选填,单元格内一行一个)'],
506-
*data_dict.get(sheet_id)
507+
*data_dict.get(sheet_id, [])
507508
]
508509
# 写入数据到工作表
509510
for row_idx, row in enumerate(data):
510511
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
512514
# 创建HttpResponse对象返回Excel文件
513515
return workbook
514516

ui/src/api/dataset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ const exportDataset: (
199199
dataset_id: string,
200200
loading?: Ref<boolean>
201201
) => Promise<any> = (dataset_name, dataset_id, loading) => {
202-
return exportExcel(dataset_name + '.xls', `dataset/${dataset_id}/export`, undefined, loading)
202+
return exportExcel(dataset_name + '.xlsx', `dataset/${dataset_id}/export`, undefined, loading)
203203
}
204204

205205
export default {

ui/src/api/document.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ const exportDocument: (
310310
loading?: Ref<boolean>
311311
) => Promise<any> = (document_name, dataset_id, document_id, loading) => {
312312
return exportExcel(
313-
document_name + '.xls',
313+
document_name + '.xlsx',
314314
`${prefix}/${dataset_id}/document/${document_id}/export`,
315315
{},
316316
loading

0 commit comments

Comments
 (0)