views:

1352

answers:

2

Hi folks,

I'm using the OleDbConnection class to retrieve data from an Excel 2000/2003 workbook:

string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                          "Data Source=" + filename + ";" +
                          "Extended Properties=\"Excel 8.0;IMEX=1\";";

OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();

// code to get table name from schema omitted

var dataAdapter = new OleDbDataAdapter(string.Format("SELECT * FROM [{0}]", name),connection);
var myDataSet = new DataSet();
dataAdapter.Fill(myDataSet, "ExcelInfo");

Now it turns out that cells in the worksheet with length greater than 255 characters are being truncated. Is this a limitation in the Microsoft.Jet.OLEDB-provider, or is there something I can do about it?

Anyone?

A: 

Looks like you need a schema.ini file to set the column type(s) to 'memo':

http://msdn.microsoft.com/en-us/library/ms709353.aspx

steamer25
Yes, I see how that can work, but it's really not practical. The user is picking the file using an OpenFileDialog, and there's no way to guarantee that a schema.ini file exists in the same directory.
Tor Haugen
+1  A: 

The OLEDB provider for excel will attempt to automatically determine the DataTypes based off of the first 8 rows of data, this can be set with the HDR=Yes/No property in the connection string. Additionally, there are multiple types that it can apply to text columns. The memo type holds over 255 characters, so if none of the first 8 rows have that then it will incorrectly set the data type.

The way to change this is by changing a registry setting called TypeGuessRows, as described here: Microsoft Support

rmoore
Thanks. It's a somewhat ugly solution, as my installer will have to modify the user's registry, but I expect it will work. So long as an administrator runs the installer..
Tor Haugen
@rmoore - i dont understand microsofts suggestion here/ if you change it to 16 doesn't it just check the first 16 rows ??
ooo
@ooo If you change it to 0, then it scans all rows. Be careful though as this can have a performance impact on larger tables.
rmoore