aspose.cell csv读取时使用单引号作为文本限定符,将逗号作为列分隔符

时间:2014-02-25 20:54:45

标签: c# .net csv aspose-cells

我使用aspose.cell以“,”作为列分隔符&读取我的csv文件。单引号(')作为文本限定符& newline as row delimiter。看起来如下: 例如:

'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());

1 个答案:

答案 0 :(得分: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());

希望这有帮助。