Click here to Skip to main content
16,004,587 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm extending written by an contract developer (who is no longer accessible) to create and write tables of data to Excel worksheets successfully but with one hitch - when the table name has an ending digit, the worksheet names are prepended with an underscore (e.g., "_SAT1"). The code writes over 10K records with 25 columns of data to Excel accurately (and quickly!).

Most of the table names are static so the initial phrase of the create statement is specified as in:

C#
sqlCmd = "create table SVR (" + sqlColumns + ")";


When the table name has a trailing digit, an underscore appears in the worksheet name. when the table name is represented by a string variable or a constant - the reason being the table name needs to include an integer like my example above.

Happy to post more of the code or answer questions. Just be warned however, I'm a FORTRAN programmer swimming in the deep end of the C# pool so....

Ted

What I have tried:

I've tried the following permutations:
C#
sqlCmd = "create table " + sheetName.Trim() + " (" + sqlColumns + ")";

Where
C#
sheetName.Trim() == "SAT1"

Other trys:
C#
sqlCmd = "create table [" + sheetName.Trim() + "] (" + sqlColumns + ")";
sqlCmd = "create table [SAT1] (" + sqlColumns + ")";

All result in a worksheet name of "_SAT1".
Posted
Comments
PIEBALDconsult 20-Jul-24 16:54pm    
For the most part, I write CSV rather than directly to Excel.

Having said that, I have written directly to Excel, but it can be problematic.

One thing to bear in mind, though it might not matter to what you're doing, is that a worksheet may contain any number of logical tables, there is no 1:1 relationship between them.

1 solution

Unfortunately, this appears to be a well known issue with the driver you are using. There are numerous accounts, on the internet, of people facing this issue. Rather than using a number, you could use the text representation; so you would write Sheet Two, instead of Sheet 2.

If you can't do this, you are left with these choices as far as I can see
  1. Live with the fact that the sheet name starts with an underscore
  2. Use a library to write the data into the spreadsheet instead of the OLEDB driver, which could end up being quite a rewrite
  3. Use a library to open up the spreadsheet and rewrite the sheet name to remove the leading underscore.
[EDIT]I have just seen a suggestion to change the connection string to use the IMEX settings as this may control how the name is created.
C#
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=filename.xlsx;Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\"";
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900