如何在Asp.Net中将数据保存到Excel中的数据? [英] How Can I Save Data In A Datatable To Excel In Asp.Net?

查看:93
本文介绍了如何在Asp.Net中将数据保存到Excel中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些数据的数据表。我想将这些数据保存到Excel中。请帮助

I have a Datatable which contains some data.I want to save that data in to Excel.Please help

推荐答案

使用gridview来实现excel文件



将您的数据表绑定到gridview并使用下面提到的代码导出。





use gridview to excel file

bind your datatable to gridview and export using below mention code.


Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=" + yourfilename);
            Response.ContentType = "application/excel";
            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();






试试这个。希望它能解决你的问题



Hi,

Try this one. Hope it will solve your problem

public void ExportToExcel(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                string filename = "Report.xls";
                System.IO.StringWriter tw = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
                DataGrid dgGrid = new DataGrid();
                dgGrid.DataSource = dt;
                dgGrid.DataBind();

                //Get the HTML for the control.
                dgGrid.RenderControl(hw);
                //Write the HTML back to the browser. 
                Response.ContentType = "application/vnd.ms-excel";
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
                this.EnableViewState = false;
                Response.Write(tw.ToString());
                Response.End();
            }
        }





谢谢



Thanks


protected void btnSave_Click(object sender, EventArgs e)
        {

            try
            {

             if (ViewState["Excel"] != null && ViewState["Excel"].ToString() != "")
                {
                    DataTable dt = (DataTable)ViewState["Excel"];

                    //  Create a dummy GridView
                    GridView GridView1 = new GridView();
                    GridView1.AutoGenerateColumns = false;

                    BoundField bfield = new BoundField();
                    bfield.DataField = "First_Name";
                    bfield.HeaderText = "Name";
                    GridView1.Columns.Add(bfield);


                    GridView1.AllowPaging = false;
                    GridView1.DataSource = dt;
                    GridView1.DataBind();



                    // GridView1.Page.EnableViewState = false;
                    StringWriter sWriter = new StringWriter();
                    HtmlTextWriter hWriter = new HtmlTextWriter(sWriter);
                    GridView1.RenderControl(hWriter);
                    string HtmlInfo = sWriter.ToString().Trim();
                    FileStream fStream = new FileStream("e:\\datagjobs.xls", FileMode.Create);
                    BinaryWriter BWriter = new BinaryWriter(fStream);
                    BWriter.Write(HtmlInfo);
                    BWriter.Close();
                    fStream.Close();
                    //Label1.Text = "File write to c:\\data.xls ";
                }
            }
            catch (Exception ex)
            {
                // Label1.Text = ex.ToString();
            }
        }


这篇关于如何在Asp.Net中将数据保存到Excel中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆