views:

611

answers:

3

Hi,

I'm needing to import an Excel spreadsheet into my program and have the following code:

string connectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;IMEX=1;HDR=NO;""", MyExcelFile.xls);

command.CommandText = "SELECT * FROM [Sheet1$]";

(Note, code above isn't real code but should let you see what I'm doing)

I'm getting the file imported, only problem is any columns in the Excel sheet which are over 255 characters are being truncated.

Is there any way around this happening?

I read somewhere that if you make sure there is a long line of text in the column within the first 8 rows, then it will be treated as a memo field and therefore not truncated but that didn't seem to work.

Any ideas?

Graeme

+1  A: 

Probably your problem has an easier solution, but as a last resort, try to save your Excel file as a CSV text file, then process it using the regular file and string manipulation classes instead of the JET engine.

Konamiman
Ended up doing it this way - now I open the Excel file through C#, save it to CSV and then parse that. Thanks all
Graeme
This is the solution? Seriously? What a crock; worthless, worthless excel spreadsheets. For such a popular format, it's borderline impossible to read data from it into an application. There are no well-maintained, open-source, fully-functional APIs for reading xlsx spreadsheets as of today, and these JET and ACE engines are such a hack (guessing field types by looking at first X rows, truncating fields to 255 characters, registry hacks necessary to change options, excel required to be installed for certain features, file being open in excel changes behavior, etc.). What a joke!
Triynko
@Triynko: I completely agree this is a joke, and sadly it's a joke on us developers :( These kinds of things make me start thinking about moving from MS to something else cause after all this proves that MS doesn't care :(
Piotr Owsiak
+2  A: 

Bumped into this one a few times. Fortunatly there's a registry hack to fix, described on MSDN here: http://support.microsoft.com/kb/189897

Effectively, Excel only looks at the first eight rows of data to determine how long columns should be. 255 is the default if the length is 255 chars or less. The MSDN article I've referenced above explains how to add a registry key "TypeGuessRows" that tells Excel how many rows to scan to determine column lengths.

Chris J
Yeah, this is the thing I was meaning in my original post - checked my registry and the TypeGuessRows value is 8. I intentionally put in a value 500 chars long in row 2 of each column which might have large data but this didn't seem to make any difference...
Graeme
A: 

It is usually best to import into an existing table. It is not too difficult to create a suitable table via code.

Remou
And to those problems of 255 length and type guessing go away? Can you provide some working sample?
Piotr Owsiak