尝试通过ADO.net在Excel中插入长字符串 [英] Trying to insert long strings in Excel via ADO.net

查看:129
本文介绍了尝试通过ADO.net在Excel中插入长字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ACE引擎和OleDb ADO.net连接器在Excel(XLSX)文件中创建工作表(表格)。 (我厌倦了编写CSV然后将CSV导入Excel。)



只要字符串值适合255个字符,它就能很好地工作。

但是比这更长的字符串值又怎么样?

我已经bin了一下,发现其他人提到了TEXT,LONGTEXT,MEMO等,但是他们没有似乎工作。我仍然得到:



System.Data.OleDb.OleDbException:该字段太小,无法接受您尝试添加的数据量。尝试插入或粘贴较少的数据。



这里有没有人克服这个限制?

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?

推荐答案

这个问题有很多可能的解决方案。



A.使用带有RequiredSheet Name的模板Excel,第一行包含与您的数据类型匹配的数据;喜欢超过500个字符长的文本等。

您将此Excel与您的解决方案保持在一起,无论何时需要写入Excel,创建此副本,插入记录并首先删除

行。



B.使用Excel对象模型



C.在Excel中创建表使用OLEDB驱动程序使用以下语句

创建表[myTableName](col1 int,col2 char(20))

然后插入myTableName。 myTableName是您创建的SheetName。



在这里你也可以删除表格...

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...
// drop the worksheet if it already exists so we can define it ourselves
if(conn.GetSchema("Tables", new String[]{null, null, "myTableName


,< span class =code-keyword> null })。Rows.Count!= 0
{
使用(OleDbCommand cmd = new OleDbCommand( DROP TABLE [myTableName
", null}).Rows.Count != 0) { using ( OleDbCommand cmd = new OleDbCommand( "DROP TABLE [myTableName


,conn))
cmd.E xecuteNonQuery();
}
", conn ) ) cmd.ExecuteNonQuery(); }







我建议从我的经验中选择第三个选项。< br $> b $ b

谢谢,



Kuthuparakkal




I recommend the third option from my experience.

Thanks,

Kuthuparakkal


这篇关于尝试通过ADO.net在Excel中插入长字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆