使用多种编码方式读取CSV文件

Contents
[ ]

Aspose.Cells提供了TxtLoadOptions.is_multi_encoded属性,您需要将其设置为true以正确加载包含多种编码的CSV文件。

以下截图显示了一个包含两行的示例CSV文件。第一行是ANSI编码,第二行是Unicode编码

输入文件
todo:image_alt_text

以下截图显示了未将TxtLoadOptions.is_multi_encoded属性设置为true的情况下从上述CSV文件转换的XLSX文件。如您所见,Unicode文本未正确转换。

输出文件1:未对多种编码进行处理
todo:image_alt_text

以下截图显示了将TxtLoadOptions.is_multi_encoded属性设置为true后从上述CSV文件转换的XSLX文件。如您所见,现在Unicode文本已经正确转换。

输出文件2:IsMultiEncoded设置为true
todo:image_alt_text

以下是将上述 CSV 文件正确转换为 XLSX 格式的示例代码。

from aspose.cells import SaveFormat, TxtLoadOptions, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
filePath = dataDir + "MultiEncoded.csv"
# Set Multi Encoded Property to True
options = TxtLoadOptions()
options.is_multi_encoded = True
# Load the CSV file into Workbook
workbook = Workbook(filePath, options)
# Save it in XLSX format
workbook.save(filePath + ".out.xlsx", SaveFormat.XLSX)