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

Contents
[ ]

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

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

输入文件
todo:image_alt_text

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

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

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string filePath = dataDir + "MultiEncoded.csv";
// Set Multi Encoded Property to True
TxtLoadOptions options = new TxtLoadOptions();
options.IsMultiEncoded = true;
// Load the CSV file into Workbook
Workbook workbook = new Workbook(filePath, options);
// Save it in XLSX format
workbook.Save( filePath + ".out.xlsx", SaveFormat.Xlsx);

相关文章