Maciej
Maciej

Reputation: 10805

OLEDB, Writing Excel cell without leading apostrophe

I'm writing to Excel file using OLEDB (C#). What I need is just RAW data format.

I've noticed all cells (headers and values) are prefixed by apostrophe (')

Is it a way to avoid adding them in all text cells?

Here is my connection string:

string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +  
 filePath + ";Extended Properties='Excel 8.0;HDR=Yes'";

I've tried use IMEX=1 like this:

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

But after that I'm receiving below error:

The Microsoft Jet database engine could not find the object 'C:\Temp\New Folder\MF_2009_04_19_2008-11-182009_DMBHCSAM1118.xls'.
Make sure the object exists and that you spell its name and the path name correctly.

Finally I've tried use IMEX=0 like this:

string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +  
   filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=0\"";

This time no exeptions raised.

Unfortunately there is still problem with apostrophes (so each my values looks as: '123, 'abc etc...)

Any idea?

Upvotes: 6

Views: 13059

Answers (8)

ramchandar
ramchandar

Reputation: 11

Insert some dummy values for the columns which has apostrophe attached in the template file. Say for example for Name column put something like this dummyname, and age column put 99. Instead of inserting a new row in the template just update the row (Update..... where Name = 'dummyname' and age =99).

This has worked for me..

Hope it works for you also!

Upvotes: 1

Venkat Kolla
Venkat Kolla

Reputation:

Try the following hack to resolve the issue. Modify the template as per the instructions

  1. In the first data row just below the header row. Format the columns in the required format.
  2. Enter some dummy values like space for characters, 0 for numeric values etc.
  3. Hide the first data row that has the dummy values and save the template

Now run your insert script using ADO.net

-Venkat Kolla

Upvotes: -1

Lunatik
Lunatik

Reputation: 3948

http://support.microsoft.com/kb/257819 has a statement to the effect that this behaviour might be unavoidable when inserting text into Excel via ADO:

A caution about editing Excel data with ADO: When you insert text data into Excel with ADO, the text value is preceded with a single quote. This may cause problems later in working with the new data.

Is the data explicitly text, might it be coerced into a numeric format? (clutching at straws...)

Upvotes: 7

TimS
TimS

Reputation: 247

Could you just use the Excel DSN? It seems to be pretty ubiquitous. I don't know .NET, so take this with a grain of salt, but here's my connection string for an OLEDB Query straight from a stock table:

"Provider=MSDASQL.1;Persist Security Info=True;Extended Properties
=""DSN=Excel Files;DBQ=" & filePath & "\" & fileName &
";DriverId=1046;MaxBufferSize=2048;PageTimeout=5;"""

And I used this basic INSERT statement:

INSERT INTO rngOutput VALUES (1, 'ABC', '$1.00', 1300)

When I did this, I didn't have any apostrophes in my data range. I'm also using Excel 2007, and I see you're using Excel 8.0 as your driver?

Hopefully that nudges you toward a solution!

Upvotes: 2

Ed Harper
Ed Harper

Reputation: 21505

It may not suit your requirements, particularly if you need to set up formatting in the Excel sheet, but have you considered writing the data out to a CSV file and saving it with an .XLS extension?

The other thing you could try would be to explicitly set format data types of your target cells in the Excel sheet to type "Text" (as through Format > Cells within Excel) before you attempt to load data. By default, the cells will be of type "General", and the driver may be adding the apostrophe to force your data to be displayed at text.

Upvotes: 0

Adarsha
Adarsha

Reputation: 2377

Check the resistry Hkey_Local_Machine/Software/Microsoft/Jet/4.0/Engines/Excel/TypeGuessRows

This decides how many rows should be scanned before deciding the format for the column. Default is 8, and 0 will force ADO to scan all column values before choosing the appropriate data type.

Upvotes: 0

Gineer
Gineer

Reputation: 2408

I know that when entering data into Excel, prefixing it with an apostrophe is an easy way to make it into a text field. Are you sure the data does not actually contain the apostrophe? If it's added to the data at entry time, your only option would be to catch them at import time and dealing with them in some custom code.

Upvotes: 0

jwendl
jwendl

Reputation: 952

Remove IMEX=1 from your connection string.

http://www.connectionstrings.com/excel

Upvotes: 0

Related Questions