繁体   English   中英

使用C#将超过100k的数据SQL Server导出到Excel

[英]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);

        }

以我的经验,使用oledb处理excel文件有点麻烦。 我切换到使用OpenXml( http://www.microsoft.com/zh-cn/download/details.aspx?id=30425 )。 也许也是您的解决方案....

更新:您也可以使用excel interop处理excel文件(xls),请参阅https://msdn.microsoft.com/zh-cn/library/ms173186(v=vs.80).aspx

为此,必须在系统上安装Excel。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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