asp.net 导出数据到Excle
2011-04-02 10:01
435 查看
public class PrinExcel { public PrinExcel() { // //TODO: 在此处添加构造函数逻辑 // } public static void ToExcel(System.Web.UI.Control ctl, string strFileName) { HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName + ".xls"); HttpContext.Current.Response.Charset = "utf-8"; string style = @"<style> .text { } </script> "; //Excel中的文本格式 HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); //System.Text.Encoding.Default; HttpContext.Current.Response.ContentType = "application/ms-excel"; //设置输出流为简体中文 ctl.Page.EnableViewState = false; System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true); System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); ctl.RenderControl(hw); HttpContext.Current.Response.Write(style); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End(); hw.Flush(); hw.Close(); tw.Flush(); tw.Close(); } //导出到Excel public static void ToExcel2(System.Web.UI.Control ctl, string strFileName) { HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName + ".xls"); HttpContext.Current.Response.Charset = "GB2312"; //"utf-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); //System.Text.Encoding.Default; HttpContext.Current.Response.ContentType = "application/ms-excel"; //设置输出流为简体中文 ctl.Page.EnableViewState = false; System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true); System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); ctl.RenderControl(hw); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End(); hw.Flush(); hw.Close(); tw.Flush(); tw.Close(); } //导出到Excel public static void ToExcel(DataTable dt) { string sb = ""; foreach (DataRow dr in dt.Rows) { for (int i = 0; i < dt.Columns.Count; i++) { sb = sb + dr[i].ToString() + "\t"; } sb = sb + "\n"; } HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=myexcel.xls"); HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default; HttpContext.Current.Response.ContentType = "application/ms-excel"; System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); hw.WriteLine(sb.ToString()); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End(); hw.Flush(); hw.Close(); tw.Flush(); tw.Close(); } //导出到Excel public static void ToExcel(string sb) { HttpContext.Current.Response.AppendHeader("Content-Disposition", "inline;filename=myexcel.xls"); HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default; HttpContext.Current.Response.ContentType = "application/ms-excel"; System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); hw.WriteLine(sb.ToString()); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End(); hw.Flush(); hw.Close(); tw.Flush(); tw.Close(); }
相关文章推荐
- ASP.Net MVC中数据库数据导出Excel,供HTTP下载
- asp.net导出Excel/Csv格式数据最优方案(C#)
- 将Dataset数据导出到Excel中(ASP.NET/C#)
- C# ,ASP.NET,Winform将数据导出到Execl汇总
- Asp.Net 导出数据格式为Excel 数据的9种方案
- asp.net中导出excel数据的方法汇总
- asp.net导出EXCEL的好方法!(好用,导出全部数据)
- ASP.NET导出数据到Excel
- ASP.NET页面数据导出excel表方法
- asp.net导出Excel格式数据方案
- ASP.Net MVC中数据库数据导出Excel,供HTTP下载(转)
- ASP.NET数据列表导出为Excel
- ASP.NET导出数据到Excel的实现方法
- 在ASP.NET中将数据库中数据导出为Excel表格
- asp.net导出数据到Excel的方法(完全无组件,不用调用Excel.exe)
- [转]asp.net导出Excel/Csv格式数据最优方案(C#)
- asp.net Linq把数据导出到Excel的代码
- asp.net中如何导出数据
- asp.net 导出数据到excel报“段太小而不能接受所要添加的数据的数量。试着插入或粘贴较少的数据”错误的解决方法
- ASP.net 导出Excel数据攻略