EXCEL导出

方法一:到处字符串拼接的Table
1、
ChuZhuCheQiYe_Bll bll = new ChuZhuCheQiYe_Bll();
string dd = bll.ChuZuQiYeXiangQing(388);
string css = "td{color:blue;width:100px;} td{color:blue;width:100px;}";
string filename = "Test.xls";
ExportToExcel(filename, dd, css);


2、方法实现:
public void ExportToExcel(string filename, string content, string cssText)
{
 var res = HttpContext.Current.Response;
 content = String.Format("<style type='text/css'>{0}</style>{1}", cssText, content);
res.Clear();
 res.Buffer = true;
 res.Charset = "UTF-8";
res.AddHeader("Content-Disposition", "attachment; filename=" + filename);
 res.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
res.ContentType = "application/ms-excel;charset=UTF-8";
 res.Write(content);
 res.Flush();
 res.End();
}

 

 

 

 

方法二:GridView 导出


1、调用:
QuanXian_Bll bll=new QuanXian_Bll ();
List <JueSe_Ent> orders=bll.juesecombobox_Bll();
ExportToExcel(orders,new string[] { "id", "mingcheng", "muluIDS", "guanliyuanZENG", "guanliyuanSHAN", "guanliyuanGAI"},
new string[] { "订单","客户代码","用户代码","型号","数量","价格"});


2、实现:
public static void ExportToExcel(IList 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;
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 = 11;
gvw.HeaderStyle.Height=40;
gvw.RowStyle.Font.Size = 11;
}
/// <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();
fileName = string.Format("Export-File.xls");
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
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());//弹出下载
File.WriteAllText(System.Web.HttpContext.Current.Server.MapPath("~/font/"+fileName), 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;

}

posted @ 2016-09-29 15:51  搁浅+  阅读(147)  评论(0)    收藏  举报