CSV and leading zeros

Hi,


I want to import a set of questions from a CSV file, and I need to specify the correct answer with 1 and all the others with 0 (see the attachment). For example 010 stands for ‘the 2° answer is correct’.
But when I try to read 010 from the CSV with the following command:

string cellValue = ws.Cells[row, col].StringValue;

… I get cellValue = “10” instead of “010”.
Is there a way to get the real content of the cell?

Thanks.
Max

Hi,

Thanks for using Aspose.Cells.

For your leading zero problem while loading the workbook, you will have to set LoadOptions.ConvertNumericData=false.

Please
see the code below. I have also attached the sample.csv file used in
this code. Please also see the output.

C#


string filePath = @“F:\sample.csv”;


LoadOptions opts = new LoadOptions();

opts.ConvertNumericData = false;


Workbook workbook = new Workbook(filePath, opts);


Worksheet worksheet = workbook.Worksheets[0];


Cell cellA1 = worksheet.Cells[“A1”];

Cell cellB1 = worksheet.Cells[“B1”];


Console.WriteLine(cellA1.StringValue);

Console.WriteLine(cellB1.StringValue);

Output:
000012345
0001234777

Hi,

Thanks for using Aspose.Cells.

We have tested the following code with our latest version: Aspose.Cells for .NET (Latest Version) , the formatted value of leading zero is fine:

C#


Workbook wb = new Workbook(pathCase, new TxtLoadOptions() { Separator = ‘;’ });

cell = wb.Worksheets[0].Cells[1, 4];

Console.WriteLine(cell.Value + “:” + cell.StringValue); //the output is 10:010



Of course, if user has no requirement of parsing numeric values automatically when reading the csv file, using “LoadOptions.ConvertNumericData=false” is better for performance consideration.