Skip to content

fix: 修复段落过长导出知识库报错 #1319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/dataset/serializers/dataset_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ def export_excel(self, with_valid=True):
document_list)
workbook = DocumentSerializers.Operate.get_workbook(data_dict, document_dict)
response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename="dataset.xls"'
response['Content-Disposition'] = 'attachment; filename="dataset.xlsx"'
workbook.save(response)
return response

Expand Down
16 changes: 9 additions & 7 deletions apps/dataset/serializers/document_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from functools import reduce
from typing import List, Dict

import xlwt
import openpyxl
from celery_once import AlreadyQueued
from django.core import validators
from django.db import transaction
Expand All @@ -34,8 +34,8 @@
from common.handle.impl.qa.xls_parse_qa_handle import XlsParseQAHandle
from common.handle.impl.qa.xlsx_parse_qa_handle import XlsxParseQAHandle
from common.handle.impl.table.csv_parse_table_handle import CsvSplitHandle
from common.handle.impl.table.xlsx_parse_table_handle import XlsxSplitHandle
from common.handle.impl.table.xls_parse_table_handle import XlsSplitHandle
from common.handle.impl.table.xlsx_parse_table_handle import XlsxSplitHandle
from common.handle.impl.text_split_handle import TextSplitHandle
from common.mixins.api_mixin import ApiMixin
from common.util.common import post, flat_map
Expand Down Expand Up @@ -490,25 +490,27 @@ def export(self, with_valid=True):
data_dict, document_dict = self.merge_problem(paragraph_list, problem_mapping_list, [document])
workbook = self.get_workbook(data_dict, document_dict)
response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = f'attachment; filename="data.xls"'
response['Content-Disposition'] = f'attachment; filename="data.xlsx"'
workbook.save(response)
return response

@staticmethod
def get_workbook(data_dict, document_dict):
# 创建工作簿对象
workbook = xlwt.Workbook(encoding='utf-8')
workbook = openpyxl.Workbook()
workbook.remove_sheet(workbook.active)
for sheet_id in data_dict:
# 添加工作表
worksheet = workbook.add_sheet(document_dict.get(sheet_id))
worksheet = workbook.create_sheet(document_dict.get(sheet_id))
data = [
['分段标题(选填)', '分段内容(必填,问题答案,最长不超过4096个字符)', '问题(选填,单元格内一行一个)'],
*data_dict.get(sheet_id)
*data_dict.get(sheet_id, [])
]
# 写入数据到工作表
for row_idx, row in enumerate(data):
for col_idx, col in enumerate(row):
worksheet.write(row_idx, col_idx, col)
cell = worksheet.cell(row=row_idx + 1, column=col_idx + 1)
cell.value = col
# 创建HttpResponse对象返回Excel文件
return workbook

Expand Down
2 changes: 1 addition & 1 deletion ui/src/api/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ const exportDataset: (
dataset_id: string,
loading?: Ref<boolean>
) => Promise<any> = (dataset_name, dataset_id, loading) => {
return exportExcel(dataset_name + '.xls', `dataset/${dataset_id}/export`, undefined, loading)
return exportExcel(dataset_name + '.xlsx', `dataset/${dataset_id}/export`, undefined, loading)
}

export default {
Expand Down
2 changes: 1 addition & 1 deletion ui/src/api/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ const exportDocument: (
loading?: Ref<boolean>
) => Promise<any> = (document_name, dataset_id, document_id, loading) => {
return exportExcel(
document_name + '.xls',
document_name + '.xlsx',
`${prefix}/${dataset_id}/document/${document_id}/export`,
{},
loading
Expand Down
Loading