Question
What column names cannot be used when creating an Excel spreadsheet with ADO.
I have a statement that creates a page in a spreadsheet:
CREATE TABLE [TableName] (Column string, Column2 string);
I have found that using a column name of Date
or Container
will generate an error when the statement is executed.
Does anyone have a complete (or partial) list of words that cannot be used as column names? This is for use in a user-driven environment and it would be better to "fix" the columns than to crash.
My work-around for these is to replace any occurences of Date
or Container
with Date_
and Container_
respectively.
Solution
You can use brackets for any fieldname, e.g.:
CREATE TABLE [TableName] ([Date] string, [Container] string)
Full example:
using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\temp\\test.xls;Extended Properties='Excel 8.0;HDR=Yes'"))
{
conn.Open();
OleDbCommand cmd = new OleDbCommand("CREATE TABLE [TableName] ([Date] string, [Container] string)", conn);
cmd.ExecuteNonQuery();
}
OTHER TIPS
Here are the reserved words for MS Query:
http://support.microsoft.com/kb/125948
Cell naming rules:
http://ezinearticles.com/?Rules-For-Naming-Cells-in-Microsoft-Excel&id=218607
It seems more like an issue with SQL reserved words. This is a good list