fix: 修复excel导出内容有特殊字符出错

This commit is contained in:
shaohuzhang1 2024-10-15 14:01:05 +08:00 committed by shaohuzhang1
parent 54695eff98
commit 9b44c9a05a
2 changed files with 6 additions and 0 deletions

View File

@ -20,6 +20,7 @@ from django.core.cache import caches
from django.db import transaction, models
from django.db.models import QuerySet, Q
from django.http import StreamingHttpResponse
from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE
from rest_framework import serializers
from application.flow.workflow_manage import Flow
@ -205,6 +206,8 @@ class ChatSerializers(serializers.Serializer):
for row_idx, row in enumerate(batch_data, start=i + 2):
for col_idx, value in enumerate(self.to_row(row), 1):
cell = worksheet.cell(row=row_idx, column=col_idx)
if isinstance(value, str):
value = re.sub(ILLEGAL_CHARACTERS_RE, '', value)
cell.value = value
output = BytesIO()

View File

@ -21,6 +21,7 @@ from django.db import transaction
from django.db.models import QuerySet
from django.http import HttpResponse
from drf_yasg import openapi
from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE
from rest_framework import serializers
from xlwt import Utils
@ -510,6 +511,8 @@ class DocumentSerializers(ApiMixin, serializers.Serializer):
for row_idx, row in enumerate(data):
for col_idx, col in enumerate(row):
cell = worksheet.cell(row=row_idx + 1, column=col_idx + 1)
if isinstance(col, str):
col = re.sub(ILLEGAL_CHARACTERS_RE, '', col)
cell.value = col
# 创建HttpResponse对象返回Excel文件
return workbook