简体   繁体   中英

Export more than 100k data sql server to excel using C#

private void button1_Click(object sender, EventArgs e)
        {
            string constr = ConfigurationManager.ConnectionStrings["db"].ConnectionString;

            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("SELECT * FROM Sheet1$", con);

            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            DataTable dt = new DataTable();
            sda.Fill(dt);
            //BindingSource bsource = new BindingSource();
            //bsource.DataSource = dt;
            //dataGridView1.DataSource = bsource;
            //sda.Update(dt);

            DataSet ds = new DataSet();
            ds.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
            sda.Fill(dt);
            ds.Tables.Add(dt);
            ExcelLibrary.DataSetHelper.CreateWorkbook("D:\\ChallanBulkUpload\\ChallanBulkUpload\\ChallanBulkUpload\\UploadFile\\challan.xls", ds);

        }

In my experience, using oledb to process excel files is a bit buggy. I switched over to using OpenXml ( http://www.microsoft.com/en-us/download/details.aspx?id=30425 ). Maybe also a solution for you ....

Update: You can also use excel interop to process excel files (xls), see https://msdn.microsoft.com/en-us/library/ms173186(v=vs.80).aspx

For this to work, Excel has to be installed on the system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM