3

I want to import some data from an Excel file ( xls ) by using TADOConnection and TADOTable.

I connect to file with no problem, but when i open TADOTable some fields have ftFloat datatype because their values in excel file are numeric, but their values are not a number!

I want all fields of TADOTable (columns of Excel file) to have ftString datatype.

I set the types of columms in Excel file to Text but no changes affected!

How can I do this?

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
Mahmoud_Mehri
  • 1,643
  • 2
  • 20
  • 38

1 Answers1

10

I am guessing you have a majority of columns that may appear to be numeric, but also some that are pure text.

ADO effectively ignores the column type when importing an Excel. Instead, it guesses the column types, as stated in this MSDN link:

A Caution about Mixed Data Types

As stated previously, ADO must guess at the data type for each column in your Excel worksheet or range. (This is not affected by Excel cell formatting settings.) A serious problem can arise if you have numeric values mixed with text values in the same column. Both the Jet and the ODBC Provider return the data of the majority type, but return NULL (empty) values for the minority data type. If the two types are equally mixed in the column, the provider chooses numeric over text.

One way to load all fields as strings is to use the IMEX extended property in your connection string like following:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;
Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";

Setting IMEX to 1 makes ADO treat all columns as text, as stated in following ConnectionStrings page:

Use this one [IMEX=1] when you want to treat all data in the file as text, overriding Excels column type "General" to guess what type of data is in the column.

You can find more information about the IMEX property in this SO question.

Update: The field data type retrieved using this would be ftWideString.

Community
  • 1
  • 1
Guillem Vicens
  • 3,936
  • 30
  • 44
  • thanks, if i use IMEX then i can`t write data in excel columns !; – Mahmoud_Mehri Jan 16 '14 at 08:19
  • "but return NULL (empty) values for the minority data type" Who designed this sh*t??? Just throwing away user data because it won't make the smart decision of taking the lowest common denominator (text) about data type. – Jan Doggen Jan 16 '14 at 09:00
  • @Mahmood_M, can you please elaborate on not being able to write data in excel columns? By the way, I forgot to mention the field data type would be `ftWideString`. Adding it now. – Guillem Vicens Jan 16 '14 at 09:15