How to export DataTable to Excel

后端 未结 21 2352
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 15:36

How can I export a DataTable to Excel in C#? I am using Windows Forms. The DataTable is associated with a DataGridView control. I have

21条回答
  •  醉酒成梦
    2020-11-22 15:38

    Just Make use of the CloseMXL.Excel Library. It's easy and pretty fast too.

    Class

    private DataTable getAllList()
            {
                string constr = ConfigurationManager.ConnectionStrings["RConnection"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("SELECT EmpId, gender, EmpName, pOnHold FROM Employee  WHERE EmpId= '"+ AnyVariable + "' ORDER BY EmpName"))
                    {
                        using (SqlDataAdapter da = new SqlDataAdapter())
                        {
                            DataTable dt = new DataTable();
                            cmd.CommandType = CommandType.Text;
                            cmd.Connection = con;
                            da.SelectCommand = cmd;
                            da.Fill(dt);
                            dt.Columns[0].ColumnName = "Employee Id";
                            dt.Columns[1].ColumnName = "Gender";
                            dt.Columns[2].ColumnName = "Employee Name";
                            dt.Columns[3].ColumnName = "On Hold";
    
                            return dt;
                        }
                    }
                }
            }
    

    Then another method which get the Dataset

    public DataSet getDataSetExportToExcel()
            {
                DataSet ds = new DataSet();
                DataTable dtEmp = new DataTable("CLOT List");
                dtEmp = getAllList();
                 ds.Tables.Add(dtEmp);
                 ds.Tables[0].TableName = "Employee"; //If you which to use Mutliple Tabs
                 return ds;
              }
    

    Now you Button Click Event

    protected void btn_Export_Click(object sender, EventArgs e)
            {
                DataSet ds = getDataSetExportToExcel();
    
                using (XLWorkbook wb = new XLWorkbook())
                {
                    wb.Worksheets.Add(ds);
                    wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                    wb.Style.Font.Bold = true;
    
                    Response.Clear();
                    Response.Buffer = true;
                    Response.Charset = "";
                    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    Response.AddHeader("content-disposition", "attachment;filename=EmployeeonHoldList.xlsx");
    
                    using (MemoryStream MyMemoryStream = new MemoryStream())
                    {
                        wb.SaveAs(MyMemoryStream);
                        MyMemoryStream.WriteTo(Response.OutputStream);
    
                        Response.Flush();
                        Response.End();
                    }
                }
            }
    

提交回复
热议问题