asp.net 导出数据到Excle

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)