0

我正在使用 aspose.cell 来读取我的 csv 文件,其中 "," 作为列分隔符,单引号 (') 作为文本限定符,换行符作为行分隔符。如下图所示:例如:

'1'.'2','3','4'

'5','6','7','8'

从 aspose.cell 读取时,值(考虑到所有文本值都没有数字)呈现为

1', 2', 3', 4'

5', 6', 7', 8'

文本限定符为双引号 (") 时没有问题。

有人可以帮忙吗,如何将我的带有单引号 (') 的 csv 文件作为 aspose.cell 的文本限定符

谢谢

TxtLoadOptions options = new TxtLoadOptions( LoadFormat.CSV)
options.ConvertNumericData = false;
options.ConvertDateTimeData = false;
options.SeparatorString=",";
//  reading aspose license reading, code removed
//stream    is ----stream reader
Workbook workBook = new Workbook(stream, options);
workBook.Settings.CheckExcelRestriction = false;
var workSheet = workBook.Worksheets[workBook.Worksheets.ActiveSheetIndex];

Console.WriteLine( workSheet.Cells[0, 0].Value.ToString());  
Console.WriteLine( workSheet.Cells[0, 1].Value.ToString());
Console.WriteLine( workSheet.Cells[0, 2].Value.ToString());
Console.WriteLine( workSheet.Cells[0, 3].Value.ToString());

Console.WriteLine( workSheet.Cells[1, 0].Value.ToString());  
Console.WriteLine( workSheet.Cells[1, 1].Value.ToString());
Console.WriteLine( workSheet.Cells[1, 2].Value.ToString());
Console.WriteLine( workSheet.Cells[1, 3].Value.ToString());
4

1 回答 1

0

将 TxtLoadOptions 与 CSV 文件一起使用时似乎是一个问题。您可以尝试使用 LoadOptions 而不是 TxtLoadOptions 打开 csv 文件而不会出现任何问题。你可以参考下面的代码:

Aspose.Cells.LoadOptions options = new Aspose.Cells.LoadOptions(Aspose.Cells.LoadFormat.CSV);

options.ConvertNumericData = false;

Workbook workBook = new Workbook("C:\\Data\\open.csv", options);
workBook.Settings.CheckExcelRestriction = false;
var workSheet = workBook.Worksheets[workBook.Worksheets.ActiveSheetIndex];

Console.WriteLine( workSheet.Cells[0, 0].Value.ToString());  
Console.WriteLine( workSheet.Cells[0, 1].Value.ToString());
Console.WriteLine( workSheet.Cells[0, 2].Value.ToString());
Console.WriteLine( workSheet.Cells[0, 3].Value.ToString());

Console.WriteLine( workSheet.Cells[1, 0].Value.ToString());  
Console.WriteLine( workSheet.Cells[1, 1].Value.ToString());
Console.WriteLine( workSheet.Cells[1, 2].Value.ToString());
Console.WriteLine( workSheet.Cells[1, 3].Value.ToString());

希望这可以帮助。

于 2014-03-18T12:51:33.160 回答