通过C#和OleDb将一行插入Excel电子表格 [英] Inserting a row into Excel Spreadsheet via C# and OleDb
问题描述
我需要以编程方式多次将一行插入到Excel电子表格中。我需要实际插入一个新行,而不是插入数据,也就是说,我需要把所有其他行下移一个。
I need to programmatically insert a row into an Excel Spreadsheet multiple times. I need to actually insert a new row and not insert data, that is, I need to actually shift all other rows down by one.
我目前正在使用OleDB来插入数据,如下所示:
I am currently using OleDB to insert the data itself like so:
//Note I have missed some code out for simplicities sake, this all works fine however
OleDbConnection oledbConn = null;
OleDbCommand cmd = null;
OleDbConnection = new OleDbConnection(connString);
OleDbConnection.Open();
string connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0; \"", TargetFile);
sting InsertCommand = string.Format("INSERT INTO [{0}${1}:{1}] Values({2})", WorksheetName, Coord, valuestring);
cmd = new OleDbCommand(InsertCommand, oledbConn);
cmd.ExecuteNonQuery();
//close etc
我想要插入一行以类似的方式。这可能吗?
I want to be able to insert a row in a similar fashion. Is this possible?
推荐答案
一目了然,您需要指定读写,默认为只读。也许:
Provider = Microsoft.Jet.OLEDB.4.0; &安培; _
Data Source = C:\Docs\Test.xls; &安培; _
Mode = ReadWrite;扩展属性=Excel 8.0; HDR =否
一瞥并重新评论,我认为 Interop 可能是最好的选择。
At a second glance and re comments, I think Interop might be the best bet.
这篇关于通过C#和OleDb将一行插入Excel电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!