使用多种编码方式读取CSV文件
Contents
[
Hide
]
有时,您的CSV文件包含多种编码(Unicode、ANSI、UTF8、UTF7等)。Aspose.Cells允许您加载此类CSV文件,并将其转换为其他格式,例如PDF或XLSX。
Aspose.Cells提供了TxtLoadOptions.is_multi_encoded属性,您需要将其设置为true以正确加载包含多种编码的CSV文件。
以下截图显示了一个包含两行的示例CSV文件。第一行是ANSI编码,第二行是Unicode编码
输入文件 |
---|
![]() |
以下截图显示了未将TxtLoadOptions.is_multi_encoded属性设置为true的情况下从上述CSV文件转换的XLSX文件。如您所见,Unicode文本未正确转换。
输出文件1:未对多种编码进行处理 |
---|
![]() |
以下截图显示了将TxtLoadOptions.is_multi_encoded属性设置为true后从上述CSV文件转换的XSLX文件。如您所见,现在Unicode文本已经正确转换。
输出文件2:IsMultiEncoded设置为true |
---|
![]() |
以下是将上述 CSV 文件正确转换为 XLSX 格式的示例代码。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |