在Excel上使用更新命令时计算字段不起作用

时间:2016-04-14 01:15:28

标签: c# excel oledb

我正在使用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工作表并计算了成功计算的字段,但存在以下问题:

  1. 处理时间很长
  2. 处理完毕后未保存工作表
  3. 任何避免和解决此问题的建议?

0 个答案:

没有答案