Question

Given a simple CSV file that consists of a string of digit characters and a date in UK format: "00000000","01/01/2014"

and code to get the used cells:

IWorkbookSet workbookSet = SpreadsheetGear.Factory.GetWorkbookSet();
IWorkbook workbook = workbookSet.Workbooks.Open(@"C:\file.csv");
IRange cells = workbook.Worksheets[0].UsedRange;

when I access cells[0,0].Text it gives it as 0, because it's treating it as numeric and therefore the leading 0s are meaningless. It will do the same for the date. I'm trying to manually construct a DataTable from the cells, but I need the original values in the file.

I tried:

SpreadsheetGear.Advanced.Cells.IValues cells = (SpreadsheetGear.Advanced.Cells.IValues)workbook.Worksheets[0];
var sb = new StringBuilder();
cells[0,0].GetText(sb);

but nothing is appended to the string builder.

How can I get access to the original file values?

Was it helpful?

Solution

SpreadsheetGear does not make available the original values as found in in the CSV file (such as "00000000" in your case). You would only be able to access cell data after it has been parsed and processed by SpreadsheetGear (i.e., converting the above to a double value of 0). If you need the CSV's original values, then you'll need to open up file yourself and manually process and parse it.

It sounds like you ultimately want a DataTable, but if you still require to create a workbook file from your CSV data, once you've created a routine to manually open and parse each "cell" in your CSV file, you could enter each value into a spreadsheet as Text, so that it is preserved as it is found in the CSV file. You can go about this in two ways:

1) Set IRange.NumberFormat to "@", which will treat any future input into that IRange as Text. Example:

worksheet.Cells["A1"].NumberFormat = "@";
worksheet.Cells["A1"].Value = "00000000";

2) Prepend your inputted value with a single apostrophe, which indicates that you want the input to be treated as text. Example:

worksheet.Cells["A1"].Value = "'00000000";

If you still need a DataTable at this point, you could use the IRange.GetDataTable(...) method to accomplish this. Because the cell data is stored as Text, your DataTable values should also reflect these same values Example:

DataTable dt = worksheet.Cells["A1"].GetDataTable(GetDataFlags.None);

(There is a GetDataFlags.FormattedText option, but this isn't really relevant for your case since the cell data is stored as text anyway and so won't be formatted)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top