Jump to content
Xtreme .Net Talk

INSERT into Excel File - !!Number Stores as Text?? [C#]


Recommended Posts

Posted

Really odd problem - I am trying to add an integer value to a cell in Excel using the following statement:

"INSERT INTO [Task$] ([CLIENTS], [ASSIGNMENTS], [sTATUS], [ELAPSED TIME]) VALUES( '" + cbClient.Text + "', '" + cbAssignment.Text + "', 'IDLE', '0')"

 

The problem is the [ELASPED TIME] value added... all I need to do is initialize the cell to 0 (integer or double)

But this doesn't work correctly - if I open the file (.xls) in EXCEL afterwards I get a green ! on the cell that says "Number Stored as Text"

 

Anyone have a clue? Any help would be greatly appreciated

Thanks,

Posted
A little trick that I learned to work with excel is to create a macro that does what you want your code to do and then observe the code that was created in the macro. In this case you want to insert a number as text, then convert it to a number. The thought I have is that maybe you can explicitly set the format of the cell to be a number and you can figure out the command for doing that via the VBA macros.
Posted

Since this isn't about automating Excel but inserting data into an existing Excel spreadsheet using ADO.NET (I presume), the macro recorder won't help you - you simply cannot record a macro that performs this action...

 

Shaitan00, does the table already have data, or is this the first row?

The reason I'm asking is that the Jet driver for Excel uses existing records to determine the data type for each column. When the table has no data, all columns are assumed to be text. This MSDN KB article has more information about this. (The article is about ADO and not ADO.NET, but the driver is the same, and none of the "ADO.NET + Excel" articles I found covers all the nitty-gritty as well as this one.)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...