C#將DataTable匯出Execl、Word、Xml

來源:互聯網
上載者:User

標籤:style   blog   http   color   檔案   os   

    /// <summary>    /// 將DT轉換為Execl的方法    /// </summary>    /// <param name="dt">需要匯出的DT    /// <param name="page">頁面    /// <param name="fileName">檔案名稱    public void ToExecl(DataTable dt, Page page, string fileName)    {        HttpResponse response = page.Response;        response.Clear();        response.ContentType = "application/x-excel";        response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");        response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");        StringBuilder sB = new StringBuilder();        for (int j = 0; j < dt.Columns.Count; j++)        {            sB.Append(dt.Columns[j].Caption + "\t");        }        sB.Append("\n");        for (int i = 0; i < dt.Rows.Count; i++)        {            for (int k = 0; k < dt.Columns.Count; k++)            {                sB.Append("=\"" + dt.Rows[i][k].ToString() + "\"\t"); //解決匯出的儲存格以科學計數法顯示的問題            }            sB.Append("\n");        }        response.Write(sB.ToString());        response.End();    }public void ToWord(DataTable dt, Page page, string filName)     {         HttpResponse response = page.Response;         response.Clear();         response.ContentType = "application/msword";         response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");         response.AddHeader("Content-Disposition","attachment:filename="+System.Web.HttpUtility.UrlEncode(filName,System.Text.Encoding.UTF8)+".doc");         StringBuilder sBuilder = new StringBuilder();         for (int i = 0; i < dt.Rows.Count; i++)         {             sBuilder.Append(dt.Rows[i][1].ToString()+"\n");         }         response.Write(sBuilder.ToString());         response.End();     }  public void ToXML(DataTable dt, Page page, string filename)  {      HttpResponse response = page.Response;      //DataSet ds = new DataSet();      response.Clear();      response.ContentType = "application/x-excel";      response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");      response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename,System.Text.Encoding.UTF8) + ".xls");      System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding();      System.Xml.XmlTextWriter xw = new XmlTextWriter(response.OutputStream, utf8);      xw.Formatting = Formatting.Indented;      xw.Indentation = 4;      xw.IndentChar = ‘ ‘;      dt.TableName = "dd";      dt.WriteXml(xw);      dt = null;      GC.Collect();      xw.Flush();      xw.Close();      response.End();  }  


來源:http://blog.csdn.net/smartsmile2012/article/details/8182862

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.