简单实用的.net DataTable导出Execl

WBOY
リリース: 2016-06-13 09:31:22
オリジナル
1109 人が閲覧しました

DataTable导出Execl
代码太简单,我们直接看代码。

复制代码 代码如下:


    protected void btnPrint_Click(object sender, EventArgs e)
    {
        string strPath = "MFOut" + DateTime.Now.ToString("yyyymmddhhmmssfff") + ".xls";
        DataGrid dg = new DataGrid();
        dg.DataSource = dtMain;
        dg.DataBind();
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=" + strPath + "");
        Response.Charset = "gb2312";
        Response.ContentEncoding = System.Text.Encoding.UTF8;//不设置会有乱码
        Response.ContentType = "application/vnd.xls";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        dg.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //这个方法不能删除掉 导出时要用到,不然会报错:
        //类型“GridView”的控件“ctl00_ContentPlaceHolder1_GridView1”必须放在具有 runat=server 的窗体标记内
    }

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のおすすめ
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!