不用與Excel控制項互動直接把資料匯出到Excel

來源:互聯網
上載者:User
 
private void ExportToExcel(DataTable dt, string fileName, string worksheetName)        {            Response.Clear();            Response.AddHeader("content-disposition", "attachment;filename="+ fileName + "");            Response.ContentType = "application/vnd.ms-excel";     
    Response.Charset = "gb2312";  //編碼設定解決亂碼問題,當然也可以在html頭部設定            Response.ContentEncoding = Encoding.GetEncoding("GB2312");
            StringWriter stringWriter = new StringWriter();            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);            DataGrid dataExportExcel = new DataGrid();            dataExportExcel.ItemDataBound += new DataGridItemEventHandler(dataExportExcel_ItemDataBound);            dataExportExcel.DataSource = dt;            dataExportExcel.DataBind();            dataExportExcel.RenderControl(htmlWrite);            StringBuilder sbResponseString = new StringBuilder();            sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html;\"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>"+ worksheetName +"</x:Name><x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head> <body>");            sbResponseString.Append(stringWriter + "</body></html>");            Response.Write(sbResponseString.ToString());            Response.End();        }        void dataExportExcel_ItemDataBound(object sender, DataGridItemEventArgs e)        {            if (e.Item.ItemType == ListItemType.Header)            {                //Header Text Format can be done as follows                e.Item.Font.Bold = true;                //Adding Filter/Sorting functionality for the Excel                int cellIndex = 0;                while (cellIndex < e.Item.Cells.Count)                {                    e.Item.Cells[cellIndex].Attributes.Add("x:autofilter", "all");                    e.Item.Cells[cellIndex].Width = 200;                    e.Item.Cells[cellIndex].HorizontalAlign = HorizontalAlign.Center;                    cellIndex++;                }            }            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)            {                int cellIndex = 0;                while (cellIndex < e.Item.Cells.Count)                {                    //Any Cell specific formatting should be done here                    e.Item.Cells[cellIndex].HorizontalAlign = HorizontalAlign.Left;                    cellIndex++;                }            }        }

文章轉自:http://blog.weareon.net/export-datatable-to-excel-in-asp-net-without-using-excel-interop-part-ii/

聯繫我們

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