把DataTable導出到Excel

   

public static void DownExcelData(this Page thispage,DataTable dataTable,string title)
        {

            Workbook wb = new Workbook();
            wb.Worksheets.Clear();
            wb.Worksheets.Add(SheetType.Worksheet);
            var ws = wb.Worksheets[0];
            ws.Name = title;
            ws.Cells.ImportDataTable(dataTable, true, "A1");
            var m = wb.SaveToStream();
            var buff = m.ToArray();

            var httpRenponse = System.Web.HttpContext.Current.Response;

            httpRenponse.Clear();
            httpRenponse.Charset = "GB2312";
            httpRenponse.Buffer = true;
            thispage.EnableViewState = false;
            httpRenponse.ContentEncoding = System.Text.Encoding.Default;
            httpRenponse.AppendHeader("Content-Disposition", string.Format("attachment;filename=" + System.Web.HttpUtility.UrlEncode(title + ".xls", Encoding.UTF8)));
            thispage.Response.ContentType = "application/vnd.ms-excel";
            httpRenponse.BinaryWrite(buff);

            httpRenponse.Flush();

            httpRenponse.Close();

            httpRenponse.End();
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章