【问题标题】:Unable to write data to excel using oledb无法使用 oledb 将数据写入 excel
【发布时间】:2014-03-27 04:44:05
【问题描述】:

我想将某些数据写入我的 excel 文件,为此我正在使用 OLEDB。

我有大量数据,大概有 50000 到 60000 条记录。

目前我的代码如下所示:

string connectionString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0; Data Source ={0};Extended Properties ='Excel 12.0 Xml;HDR=YES;Mode=ReadWrite';", filePath);

using (OleDbConnection cn = new OleDbConnection(connectionString))

{

String statement = "UPDATE [Sheet1$] SET [Status] = @Status,  [Count] = @count WHERE [ID] = @Id";

 cn.Open();


for (int index = 0; index < _listResult.Count; index++)

{                                                      

      OleDbCommand cmd = new OleDbCommand(statement, cn);

      cmd.Parameters.AddWithValue("@Status", _listResult[index].Status);

      cmd.Parameters.AddWithValue("@count", _listResult[index].Count);

      cmd.Parameters.AddWithValue("@Id", _listResult[index].Id);

      cmd.ExecuteNonQuery();

}


 cn.Close();

但如果我这样做,则数据不会更新到 excel 表中。

相反,我在内外移动 For 循环,打开连接并执行查询,然后它工作正常。但这需要很多时间。

我在这里做错了吗?

请给我适当的方法来实现这一点。

任何帮助将不胜感激。

注意:刚才我发现以下代码在我从 Visual Studio 运行应用程序时正在工作,但是当我从那里进行发布构建并运行它时,它不会将数据写入excel表格。请帮忙。

谢谢

【问题讨论】:

  • 嗨,您还需要这方面的帮助吗?

标签: excel c#-4.0 oledb oledbconnection


【解决方案1】:

您需要将代码拆分如下​​:

  1. 在循环之外 - 声明 cmd。向其中添加 3 个参数及其类型。在这个阶段不要赋值。
  2. 在循环内部 - 为参数分配适当的值,然后执行语句。

你的代码应该是这样的(我假设所有参数都是整数,所以你需要根据你的实际类型进行修改):

OleDbCommand cmd = new OleDbCommand(statement, cn);
cmd.Parameters.Add("@Status", OleDbType.Integer);
cmd.Parameters.Add("@count", OleDbType.Integer);
cmd.Parameters.Add("@id", OleDbType.Integer);

for (int index = 0; index < _listResult.Count; index++)
 {
  cmd.Parameters["@Status"].Value = _listResult[index].Status;
  cmd.Parameters["@count"].Value = _listResult[index].Count;
  cmd.Parameters["@Id"].Value = _listResult[index].Id;
  cmd.ExecuteNonQuery();
 }

【讨论】:

  • 嗨 Shree,非常感谢您的帮助。我会试试这个并回复您。
  • 它在 Visual Studio 中工作,但在我进行发布构建和运行时无法工作
  • 没有显示错误,说明excel写入成功,但是excel表格没有更新。
  • 我可以使用在发布模式下生成的 .exe 进行更新。您是否检查过它是否可能是文件权限问题或其他什么?
  • 感谢您提供宝贵的 cmets。我会详细了解它。
【解决方案2】:

我正在使用这种方法使用数据表在excel中写入我想要的字段,你也可以尝试使用这个

public void CerateExcel()
    {
        Response.ClearContent();
        Response.ClearHeaders();

        DataTable dt = new DataTable();
        dt = Session["table"] as DataTable;

        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=ABC1.xls");
        Response.ContentType = "application/ms-excel";
        Response.ContentEncoding = System.Text.Encoding.Unicode;
        Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());


        System.IO.StringWriter sw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);

        form1.RenderControl(hw);

        Response.Write(sw.ToString());
        Response.Flush();
        Response.End();
}

在新页面中将您的数据添加到网格(html 页面表单名称:form1),这将自动为您创建 excel 文件。

【讨论】:

  • 我有一个excel,我需要更新那个excel上的cols,而不是创建一个新的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多