All, there are many question on this subject but none solve my problem. I have written a fairly involved routine to export a passed DataSet/DataTable to Excel (I have Office 2010 running under Win7) using OleDb and the Access Database Engine. The problem is no matter how I define the columns to be written to Excel all values are exported as TEXT/STRING fields.
I am using the OleDbConnection string
string fileName = @"F:\SomePath\MyExcel.xlsx";
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=
{0};Extended Properties=""Excel 12.0 Xml;HDR=YES;MaxScanRows=0;IMEX=0""", fileName);
and have tried numerous other connection string options with no luck.
I generate the Excel definitions in code, but explicitly these are produced as
CREATE TABLE [MetaTab] ([Table] TEXT,[Field] TEXT,[Seq] NUMBER,[DataLevel] NUMBER)
I then generate the code for inserts, for the example above this is
INSERT INTO [MetaTab$]([Table],[Field],[Seq],[DataLevel])VALUES('B1A','EstabID','1','9')
This works, but all values are written as TEXT. How can I force Excel to take other data formats?
Note: I have tried removing the apostrophes for non-string but this does not work either. I am genuinely stuck and any ideas would be greatly appreciated. Thanks for your time.
I don’t think you can. An excel format is not actually analagous to a database column data type – ultimately the underlying value is always either a number or a string; and then the format determines how it is displayed.
Even if I’m wrong – I personally much prefer to use http://epplus.codeplex.com/ to generate my Excel spreadsheets – you can do a lot of very advanced stuff in it as well as simple stuff like formatting.