B/S匯出資料到excel

來源:互聯網
上載者:User

積累篇二:

   感覺上次winform寫的資料匯出有點繁瑣了,這次整合了一下前輩的做法,比較簡單,但是缺陷:表名稱固定的

  下面讓我們來看看是如何?的:

    /// <summary>    /// 匯出資料    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    /// *** 12.12    protected void btnExport_Click(object sender, EventArgs e)    {        List<Tb_AgreementInfo> list;        list = tam.GetModelList("1=1");        this.gvCAStatics.DataSource = list;        this.gvCAStatics.DataBind();        CreateExcel(list, "ConFulfilStatics.xls");    }    /// <summary>    /// 匯出資料    /// </summary>    /// <param name="list"></param>    /// <param name="FileName"></param>    /// *** 12.12    public void CreateExcel(List<Tb_AgreementInfo> list, string FileName)    {        HttpResponse resp;        resp = Page.Response;        resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");        resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);        string colHeaders = "", ls_item = "";        //輸出標題         colHeaders += "合約名稱" + "\t";        colHeaders += "合約類型" + "\t";        colHeaders += "負責人" + "\t";        colHeaders += "簽署日期" + "\t";        colHeaders += "審核狀態" + "\n";        resp.Write(colHeaders);        //逐行處理資料            foreach (Tb_AgreementInfo custom in list)        {            ls_item += custom.AreementName + "\t";            ls_item += custom.AreementType==0?"一般客戶":"供應商" + "\t";            ls_item += custom.YuetCommissarial + "\t";            ls_item += custom.SubsceibeDate + "\t";            ls_item += custom.IsAduiting == 0 ? "未審核" : "審核通過" + "\n";            //當前行資料寫入HTTP輸出資料流,並且置空ls_item以便下行資料                  resp.Write(ls_item);            ls_item = "";        }        //寫緩衝區中的資料到HTTP標頭檔中          resp.End();    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.