如何在Excel工作表内的表格对象中插入行? [英] How to Insert Rows to Table Object Inside an Excel Sheet?

查看:220
本文介绍了如何在Excel工作表内的表格对象中插入行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难将行插入到现有的表对象中。这是我的代码段:

I have difficulties trying to insert rows into an existing table object. Here is my code snippet:

string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + @"C:\myExcelFile.xlsx" + ";Extended Properties=\"Excel 12.0;ReadOnly=False;HDR=Yes;\"";
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
    conn.Open();
    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = conn;

    string insertQuery = String.Format("Insert into [{0}$] (ID, Title,NTV_DB, Type ) values(7959, 8,'e','Type1')", TabDisplayName);
    cmd.CommandText = insertQuery;
    cmd.ExecuteNonQuery();
    cmd = null;
    conn.Close();
}

结果是将行插入到现成的表对象下面:

As a result I get my rows inserted below a ready-made table object:

我也尝试过在表对象中插入数据,如下所示:

I've also tried inserting data inside a table object like so:

 string insertQuery = String.Format("Insert into [{0}$].[MyTable] (ID, Title,NTV_DB, Type ) values(7959, 8,'e','Type1')", TabDisplayName);

但是我得到一个错误:


Microsoft Access数据库引擎找不到对象 MyTable。确保对象存在,并且正确拼写了它的名称和路径名。如果'MyTable'不是本地对象,请检查您的网络连接或联系服务器管理员。

The Microsoft Access database engine could not find the object 'MyTable'. Make sure the object exists and that you spell its name and the path name correctly. If 'MyTable' is not a local object, check your network connection or contact the server administrator.

名称 MyTable 确实存在。如果有人能阐明这个奥秘,我将不胜感激。

As you can see, table with a name MyTable does exist. I would be very grateful if someone can shed some light on this mystery.

推荐答案

基于此处


ADO.NET无法访问XL的Table对象(也称为列表),也不能访问直接或间接引用表名称或
结构化引用的
范围名称。 ADO.NET可以访问表
上引用表单元格范围的命名范围。

ADO.NET cannot access XL's Table objects (aka Lists) nor can it access range names that directly or indirectly reference the table's name or structured references. ADO.NET can access named ranges over tables that reference the table's cell range.

示例:名称 MyTable 是指 = $ A $ 1:$ E $ 3
因此,您可以为此使用 Range Sheet1 $ A:E )。在这种情况下,您的Insert语句可能会变成这样:

Example: Name MyTable refers to =$A$1:$E$3. So you can use Range for example (Sheet1$A:E) for this purpose. In this case your Insert statement could change to something like this:

string insertQuery = "INSERT INTO [Sheet1$A:E] (ID, Title,NTV_DB, Type) VALUES (7959, 8,'e','Type1')"; 

其他限制:


工作簿保护:ADO无法访问受密码保护的工作簿。

Workbook Protection: ADO cannot access password protected workbooks.

使用的行:ADO将插入到最后使用的行下方。注意!最后使用的行可以为空。

Used Rows: ADO will insert below the last used row. NOTE! The last used row can be empty.

Tables 如前所述。

这篇关于如何在Excel工作表内的表格对象中插入行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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