如果Excel未打开,则无法更新Excel文件

本文关键字:Excel 文件 更新 如果 | 更新日期: 2023-09-27 18:10:33

我正在尝试在c#中使用Oledb和Dataset读取和更新Excel。

读取操作工作良好,即使两个Excel文件是打开或不打开。

更新操作仅当文件处于打开状态时有效但更新操作给出错误,如果Excel文件未打开

Code :
string query2 = "SELECT * FROM [Fixtures Input$A:IP]";
        string qupdate;        
        string conStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("SAE.xls") + ";Mode=ReadWrite;" + "Extended 
Properties='"Excel 8.0;IMEX=0;HDR=YES;TypeGuessRows=0;ImportMixedTypes=Text'"";
        con = new OleDbConnection(conStr);
        con.Open();
        cmd2 = new OleDbCommand(query2, con);
        adap2 = new OleDbDataAdapter(cmd2);
        ds2 = new DataSet();
        adap2.Fill(ds2, "Fixtures Input");
        #region "DoingRowEmpty"
        for (int i = 1; i <= 10; i++)
        {
            DataRow aaa = ds2.Tables[0].Rows[i];
            aaa.BeginEdit();
            //aaa.ItemArray[2] = "test";
            aaa.EndEdit();
            id = Convert.ToString(i);
            qupdate = "UPDATE [Fixtures Input$] SET [Fixtures] = '', [Fixture Type] = '', [x] = '' , [(R/N)] = '' , [Fixture Quantity] = '' ,"
                      + " [Quantity of Gangs] = '' , [Sensor mount] = '' , [new lamps] = '', [High bay] = '', [burn/year] = '' ,[Ladder] = ''  where [id] = '" + id + "'";
            adap2.UpdateCommand = new OleDbCommand(qupdate, con);
            adap2.Update(ds2.Tables[0]);
}
#endregion

有什么建议吗?

如果Excel未打开,则无法更新Excel文件

有必要在运行程序之前打开Excel文件吗?