我正在使用OleDb
库更新现有的Excel Sheet
我的代码,如下所示
string conStr = "";
//getting the path of the file
string path = "file path";
//connection string for that file which extantion is .xlsx
conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;ReadOnly=False;HDR=Yes;\"";
//Process myProcess = Process.Start(path);
//Providing connection
OleDbConnection conn = new OleDbConnection(conStr);
//checking that connection state is closed or not if closed the
//open the connection
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
for (int i = 0; i < 1000; i++)
{
//making query
string query = "Update [Sheet1$] set name = 'Mohamed', Email = 'dev.msalah' , [Basic Salary] = 10 ,[Variable Salary] = 20 where ID = " + i;
//create command object
OleDbCommand cmd = new OleDbCommand(query, conn);
int result = cmd.ExecuteNonQuery();
}
conn.Close();
在Excel sheet
中的我使用了一个表来创建Calculated Field
如果在没有Process.Start
的情况下使用上述代码,除了计算字段外,一切都运行良好且快速。即使我打开文件并强制计算它也无法正常工作
如果我使用Process.Start
打开了Excel工作表并计算了成功计算的字段,但存在以下问题:
任何避免和解决此问题的建议?