/// <summary> /// 导出Excel 方法1 /// </summary> /// <returns></returns> public ActionResult ExportExcel() { List<MyEntity> listdata=new List<MyEntity>(); //listdata = GetListData(); DataSet ds = GetDataSetFormList(listdata); CreateExcelFromDatatable(ds.Tables[0], "outPutExcel.xls"); return null; } /// <summary> /// 导出Excel 方法2 /// </summary> /// <returns></returns> public ActionResult ExportExcel2() { List<MyEntity> listdata = new List<MyEntity>(); //listdata = GetListData(); Print(listdata,"outPutExcel2.xls"); return null; } /// <summary> /// 生成DataSet /// </summary> /// <param name="listdata"></param> /// <returns></returns> private DataSet GetDataSetFormList(List<MyEntity> listdata) { DataSet ds = new DataSet(); DataTable tmpdt = new DataTable(); tmpdt.Columns.Add("产品编号"); tmpdt.Columns.Add("产品售价"); tmpdt.Columns.Add("产品名称"); tmpdt.Columns.Add("会员价"); tmpdt.Columns.Add("说明"); DataRow dr; foreach (var item in listdata) { dr = tmpdt.NewRow(); dr["产品编号"] = item.Id; dr["产品售价"] = item.SalePrice; dr["产品名称"] = item.Title; dr["会员价"] = item.VipPrice; dr["说明"] = item.Summary; tmpdt.Rows.Add(dr); } tmpdt.AcceptChanges(); ds.Tables.Add(tmpdt); return ds; } /// <summary> /// 从数据表中导出数据到Excel /// </summary> /// <param name="table"></param> /// <param name="filename"></param> private void CreateExcelFromDatatable(DataTable table, string filename) { HttpResponse response = System.Web.HttpContext.Current.Response; response.Charset = "UTF-8"; response.ContentEncoding = System.Text.Encoding.Default; response.ContentType = "application/vnd.ms-excel"; response.AppendHeader("Content-Disposition", "attachment;filename=" + filename); int index = 0; string headers = ""; for (index = 0; index < table.Columns.Count; index++) { headers += table.Columns[index].ColumnName + " "; } headers += " "; response.Write(headers); response.Flush(); foreach (DataRow row in table.Rows) { string rowcontent = ""; foreach (DataColumn column in table.Columns) { rowcontent += row[column.ColumnName].ToString() + " "; } rowcontent += " "; response.Write(rowcontent); response.Flush(); } response.End(); } /// <summary> /// 打印 /// </summary> public void Print(List<MyEntity> list,string fileName) { //命名导出表格的StringBuilder变量 StringBuilder sHtml = new StringBuilder(string.Empty); //打印表头 sHtml.Append("<table border="1" width="100%">"); sHtml.Append("<tr height="40"><td colspan="5" align="center" style='font-size:24px'><b>XXXXXXX报价表" + "</b></td></tr>"); //打印列名 sHtml.Append("<tr height="20" align="center" style='background-color:#CD0000'><td>编号</td><td>商品名称</td><td>市场价</td><td>VIP价格</td><td>说明</td></tr>"); //循环读取List集合数据 for (int i = 0; i < list.Count; i++) { sHtml.Append("<tr height="20" align="left"><td style='background-color:#8DEEEE'>" + list[i].Id + "</td><td>" + list[i].Title + "</td><td style='background-color:#8DEEEE'>¥" + list[i].SalePrice + "</td><td style='color:#F00;background-color:#8DEEEE;'>¥" + list[i].VipPrice + "</td><td>" + list[i].Summary + "</td></tr>"); } sHtml.Append("</table>"); //调用输出Excel表的方法 ExportToExcel("application/ms-excel", fileName, sHtml.ToString()); } /// <summary> /// 输入HTTP头,然后把指定的流输出到指定的文件名,然后指定文件类型 /// </summary> /// <param name="FileType"></param> /// <param name="FileName"></param> /// <param name="ExcelContent"></param> public void ExportToExcel(string FileType, string FileName, string ExcelContent) { System.Web.HttpContext.Current.Response.Charset = "UTF-8"; System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString()); System.Web.HttpContext.Current.Response.ContentType = FileType; System.Web.HttpContext.Current.Response.Output.Write(ExcelContent); System.Web.HttpContext.Current.Response.Flush(); System.Web.HttpContext.Current.Response.End(); } /// <summary> /// 数据实体 /// </summary> public class MyEntity { /// <summary> /// 产品ID /// </summary> public string Id { get; set; } /// <summary> /// 产品名称/标题 /// </summary> public string Title { get; set; } /// <summary> /// 价格 /// </summary> public string SalePrice { get; set; } /// <summary> /// 会员价 /// </summary> public string VipPrice { get; set; } /// <summary> /// 说明 /// </summary> public string Summary { get; set; } }