Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
4.08/5 (3 votes)
I'm experimenting with using the ACE engine with the OleDb ADO.net connector to create worksheets (tables) in an Excel (XLSX) file. (I get tired of writing CSV and then importing the CSV to Excel.)

It works fairly well as long as string values fit in 255 characters.
But what about string values that are longer than that?
I've binged around a bit and found others mentioning TEXT, LONGTEXT, MEMO, etc., but they don't seem to work. I still get:

System.Data.OleDb.OleDbException: The field is too small to accept the amount of data you attempted to add. Try inserting or pasting less data.

Has anyone here overcome this limitation?
Posted
Comments
Maciej Los 10-Oct-14 9:10am    
Did you tested it on single Excel file or several?

There are many possible solutions to this problem.

A. Use a Template Excel with the RequiredSheet Name with the first row contains data that matches your Datatype; like texts with more than 500 char long etc.
You keep this Excel with your solution and whenever you need to write to Excel, create a copy of this, insert records, and delete first
row.

B. Use Excel Object Model

C. Create Table in Excel using OLEDB driver using following statement
Create table [myTableName] (col1 int, col2 char(20))
Then insert to myTableName. myTableName is the SheetName you are creates.

Here you can also Drop Tables too...
C#
// drop the worksheet if it already exists so we can define it ourselves
if(conn.GetSchema("Tables", new String[]{null, null, "myTableName$", null}).Rows.Count != 0)
{
using ( OleDbCommand cmd = new OleDbCommand( "DROP TABLE [myTableName$]", conn ) )
cmd.ExecuteNonQuery();
}



I recommend the third option from my experience.

Thanks,

Kuthuparakkal
 
Share this answer
 
v2
Comments
PIEBALDconsult 14-Feb-13 9:03am    
I don't want to do A and B. I'm doing C already.
Kuthuparakkal 15-Feb-13 2:23am    
Okay.. Did it work for you ?
PIEBALDconsult 15-Feb-13 9:47am    
Not for these long strings. Hence the question.
arsen4ik 10-Oct-14 8:12am    
Can you show an example of С. option
As per my experience, ACE.OLEDB driver does not supports long text (over 255 characters) insertion.

I heard that it's possible to workaround it.
Option 1)
Passed strings longer than 255 characters are truncated in Excel[^].

Option 2)
Treating data as text[^]

Note: Never tested!

Few months ago i wrote few links, but i never had enough time to read it:
Data Types in OLE DB (OLE DB)[^] and this: OLE DB Data Type Mappings[^]
 
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