Incorrect Format Error

Hi,

I’m trying to import some data from excel into a datatable.

One of columns must contain numeric values.

If the second row of this specific column incorrectly has text in it the resulting datatable column datatype will be set to string. The program will still run like this.

However if the numeric value is displayed in a row further down I get an ‘incorrect format error’ because the system is trying to copy a string into a datatype of double.

Any suggestions as to how I can better handle this?

Hi,


Please download and try this latest fix of Aspose.Cells for .NET v7.2.1.6, and let us know your feedback.

If the problem still persists, please provide us with your sample excel file, along with your source code in the form of a console application (zip it and attach with your message), so that we can look into it and assist you further.

You may also have a look at our Documentation Page that contains a detailed programming guide along with illustrated examples.

Hi,


Well, I think you are using ExportDataTable() method of Aspose.Cells.Cells class. For your information, the datatype for a column is determined by the very first value in that column, if it is numeric then the data type would be numeric for the field, if it is string, then it will be string data type.

The Cells.ExportDataTable() method also decides which data type would be setup for particular column in Excel sheet. By default, it is based on the first value (normally) in the column too, if it is string value, then the DataColumn would have string data type for the data table, if it has numeric value, the numeric data type would be taken for the column. I think for your case (as you are getting some error which is logical too), you may try any one option:

1) You may define your desired DataTable based on your desired columns and specify your desired data types accordingly. Then use the relevant ExportDatatTable() overloaded version to store the data into it.

See the sample code segment below:
e.g

DataTable dt = new DataTable();
dt.Columns.Add(“column1”, typeof(string));
dt.Columns.Add(“column2”, typeof(string));
dt.Columns.Add(“column3”, typeof(string));
dt.Columns.Add(“column4”, typeof(double));
dt.Columns.Add(“column5”, typeof(double));
dt.Columns.Add(“column6”, typeof(double));
dt.Columns.Add(“column7”, typeof(double));
//…
worksheet.Cells.ExportDataTable(dt, 0, 0, 1000, true);


2) Alternatively, you may use ExportDataTableAsString() method, but in this case all the data would be exported as string/text.

Thank you.

Hi,


This issue is a challenge in postal columns which, (assuming US), can be zip 5 digit numeric or zip + 4 in the form 12345-6789, the latter fails in any import using ExportDataTable.

I have tried

  1. Finding the zip column and applying a range column format of text to the zip column
  2. Explicitly adding a “Zip” column to the datatable prior to calling ExportDataTable and setting the data type of the column to string.

If you tell me that upgrading (we are at 7.0.1.0) aspose cells corrects this issue, then great, I will upgrade. Or alternately if there is an overload method OTHER than ExportDataTableAsString that can be used to achieve this, that would be great too. Thanks.

Hi,


Well, as I said earlier for ExportDataTable() method, the datatype for a column is determined by the very first value in that column, if it is numeric then the data type would be numeric for the field, if it is string, then it will be string data type. If your first value is 123456 and very next value is 12345-6789, then the problem will occur as the latter will be a string. Either you may define your desired DataTable based on your desired columns and specify your desired data types accordingly. For zip code column, it should be string data type. Then use the relevant ExportDatatTable() overloaded version to store the data into it. Alternatively, you may always use ExportDataTableAsString() method.

Thank you.