Unable to write data to excel using oledb

2019-09-02 19:00发布

问题:

I want to write certain data to my excel file and for that purpose i am using OLEDB.

I have a huge chunk of data which can count to maybe 50000 to 60000 records.

Currently my code is as shown below:

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();

But if i am doing like this then the data is not updated to the excel sheet.

Instead i shift the For loop outside and inside that i open the connection and execute the query ,then it is working fine.But that takes a lot of time.

Am i doing anything wrong here?

Please give me appropriate ways to achieve this.

Any help will be appreciated.

NOTE: Just now i found out that the following code is working when i am running the application from visual studio but when i am taking a release build and running it from there,then it is not writing the data to the excel sheet.Please help.

Thanks

回答1:

You need to split the code as follows:

  1. Outside the loop - declare cmd. Add the 3 parameters to it, along with their types. DO NOT assign values at this stage.
  2. Inside the loop - assign the appropriate values to the parameters and then execute the statement.

Your code should look something like this (I assumed all parameters to be integers, so you need to modify based on your actual types):

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();
 }


回答2:

I am using this method to write the fields i want in excel using datatable you can also try with this

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();
}

Add Your Data to Grid in new Page (html page form name :form1) and This will automatically create the excel file for you.