【问题标题】:cant update Excel file if Excel is not open?如果 Excel 未打开,则无法更新 Excel 文件?
【发布时间】:2013-04-24 07:25:22
【问题描述】:

我正在尝试在 C# 中使用 Oledb 和数据集读取和更新 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 运行,项目就会成功执行

标签: c# asp.net web-services excel ms-office


【解决方案1】:

在运行程序之前是否需要打开 Excel 文件?

【讨论】:

    猜你喜欢
    • 2021-10-18
    • 2015-01-23
    • 1970-01-01
    • 2018-01-25
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多