public class ExportHelper
{
public static void ExportToExcel(object dataList, string[] fields, string[] headTexts, string title)
{
GridView gvw = new GridView();
int ColCount, i;
//如果筛选的字段和对应的列头名称个数相对的情况下只导出指定的字段
if (fields.Length != 0 && fields.Length == headTexts.Length)
{
ColCount = fields.Length;
gvw.AutoGenerateColumns = false;
for (i = 0; i < ColCount; i++)
{
BoundField bf = new BoundField();
bf.DataField = fields[i];
bf.HeaderText = headTexts[i];
gvw.Columns.Add(bf);
}
}
else
{
gvw.AutoGenerateColumns = true;
}
SetStype(gvw);
gvw.DataSource = dataList;
gvw.DataBind();
ExportToExcel(gvw, title);
}
/// <summary>
/// 导出数据到Excel
/// </summary>
/// <param name="DataList">IList Data</param>
/// <param name="Fields">要导出的字段</param>
/// <param name="HeadName">字段对应显示的名称</param>
public static void ExportToExcel(IList dataList, string[] fields, string[] headTexts)
{
ExportToExcel(dataList, fields, headTexts, string.Empty);
}
/// <summary>
/// 设置样式
/// </summary>
/// <param name="gvw"></param>
private static void SetStype(GridView gvw)
{
gvw.Font.Name = "Verdana";
gvw.BorderStyle = System.Web.UI.WebControls.BorderStyle.Solid;
gvw.HeaderStyle.BackColor = System.Drawing.Color.LightCyan; //一般这里要添加引用System.Drawing
gvw.HeaderStyle.ForeColor = System.Drawing.Color.Black;
gvw.HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
gvw.HeaderStyle.Wrap = false;
gvw.HeaderStyle.Font.Bold = true;
gvw.HeaderStyle.Font.Size = 10;
gvw.RowStyle.Font.Size = 10;
}
/// <summary>
/// 导出GridView中的数据到Excel
/// </summary>
/// <param name="gvw"></param>
/// <param name="DataList"></param>
private static void ExportToExcel(GridView gvw, string title)
{
string fileName;
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
//HttpContext.Current.Response.Charset = "utf-8";
//HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
HttpContext.Current.Response.Charset = "GB2312"; //设置了类型为中文防止乱码的出现
fileName = string.Format("Export-File {0:yyyy-MM-dd_HH_mm}.xls", DateTime.Now);
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
//HttpContext.Current.Response.AppendHeader("Content-Type", "text/html; charset=gb2312");
HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
//HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
gvw.RenderControl(hw);
if (!string.IsNullOrEmpty(title))
{
HttpContext.Current.Response.Write("<b><center><font size=3 face=Verdana color=#0000FF>" + title + "</font></center></b>");
}
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
HttpContext.Current.Response.End();
gvw.Dispose();
tw.Dispose();
hw.Dispose();
gvw = null;
tw = null;
hw = null;
}
}
{
public static void ExportToExcel(object dataList, string[] fields, string[] headTexts, string title)
{
GridView gvw = new GridView();
int ColCount, i;
//如果筛选的字段和对应的列头名称个数相对的情况下只导出指定的字段
if (fields.Length != 0 && fields.Length == headTexts.Length)
{
ColCount = fields.Length;
gvw.AutoGenerateColumns = false;
for (i = 0; i < ColCount; i++)
{
BoundField bf = new BoundField();
bf.DataField = fields[i];
bf.HeaderText = headTexts[i];
gvw.Columns.Add(bf);
}
}
else
{
gvw.AutoGenerateColumns = true;
}
SetStype(gvw);
gvw.DataSource = dataList;
gvw.DataBind();
ExportToExcel(gvw, title);
}
/// <summary>
/// 导出数据到Excel
/// </summary>
/// <param name="DataList">IList Data</param>
/// <param name="Fields">要导出的字段</param>
/// <param name="HeadName">字段对应显示的名称</param>
public static void ExportToExcel(IList dataList, string[] fields, string[] headTexts)
{
ExportToExcel(dataList, fields, headTexts, string.Empty);
}
/// <summary>
/// 设置样式
/// </summary>
/// <param name="gvw"></param>
private static void SetStype(GridView gvw)
{
gvw.Font.Name = "Verdana";
gvw.BorderStyle = System.Web.UI.WebControls.BorderStyle.Solid;
gvw.HeaderStyle.BackColor = System.Drawing.Color.LightCyan; //一般这里要添加引用System.Drawing
gvw.HeaderStyle.ForeColor = System.Drawing.Color.Black;
gvw.HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
gvw.HeaderStyle.Wrap = false;
gvw.HeaderStyle.Font.Bold = true;
gvw.HeaderStyle.Font.Size = 10;
gvw.RowStyle.Font.Size = 10;
}
/// <summary>
/// 导出GridView中的数据到Excel
/// </summary>
/// <param name="gvw"></param>
/// <param name="DataList"></param>
private static void ExportToExcel(GridView gvw, string title)
{
string fileName;
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
//HttpContext.Current.Response.Charset = "utf-8";
//HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
HttpContext.Current.Response.Charset = "GB2312"; //设置了类型为中文防止乱码的出现
fileName = string.Format("Export-File {0:yyyy-MM-dd_HH_mm}.xls", DateTime.Now);
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
//HttpContext.Current.Response.AppendHeader("Content-Type", "text/html; charset=gb2312");
HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
//HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
gvw.RenderControl(hw);
if (!string.IsNullOrEmpty(title))
{
HttpContext.Current.Response.Write("<b><center><font size=3 face=Verdana color=#0000FF>" + title + "</font></center></b>");
}
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
HttpContext.Current.Response.End();
gvw.Dispose();
tw.Dispose();
hw.Dispose();
gvw = null;
tw = null;
hw = null;
}
}
前台调用:
object o=myRules.Getusers();
ExportHelper.ExportToExcel(o,
new string[] { "id", "uname"},
new string[] { "id", "姓名"},
"这里是EXCEL的标题名"
);
---------------------
public object Getusers()
{
var q=from x in DataContext.users
select new {
id=x.userid,
uname=x.name
};
return q;
}
ExportHelper.ExportToExcel(o,
new string[] { "id", "uname"},
new string[] { "id", "姓名"},
"这里是EXCEL的标题名"
);
---------------------
public object Getusers()
{
var q=from x in DataContext.users
select new {
id=x.userid,
uname=x.name
};
return q;
}
直接复制代码就可以使用,如果不能使用,请添加引用(新手特别注意)
Ryan
2010年8月13日9:45:40