Background export data, summarize their export experience for MVC project:
Problems encountered, Firefox and non-Firefox browsers, we have to deal with it:
The figure is Firefox garbage problem:
Dt = DataTable new new DataTable ( " user login statistical management " ); DataColumn dc1 = new DataColumn("登录用户", Type.GetType("System.String")); DataColumn dc2 = new DataColumn("真实姓名", Type.GetType("System.String")); DC3 DataColumn = new new DataColumn ( " partner name " , Type.GetType ( " System.String " )); DataColumn dc4 = new DataColumn("所在区域", Type.GetType("System.String")); DataColumn dc5 = new DataColumn("所属区域", Type.GetType("System.String")); DataColumn dc6 = new DataColumn("联系人", Type.GetType("System.String")); DataColumn dc7 = new DataColumn("家庭地址", Type.GetType("System.String")); DataColumn dc8 = new DataColumn("详细地址", Type.GetType("System.String")); DataColumn dc9 = new DataColumn("用户角色", Type.GetType("System.String")); DataColumn dc10 = new DataColumn("登录时间", Type.GetType("System.String")); DataColumn dc11 = new DataColumn("登录次数", Type.GetType("System.String")); dt.Columns.Add(dc1); dt.Columns.Add(dc2); dt.Columns.Add(dc3); dt.Columns.Add(dc4); dt.Columns.Add(dc5); dt.Columns.Add(dc6); dt.Columns.Add(dc7); dt.Columns.Add(dc8); dt.Columns.Add(dc9); dt.Columns.Add(dc10); dt.Columns.Add(dc11); foreach (User_Statistics model in list) { string[] arr = model.LoginDate.Split("".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); string strArea = model.AgentProvince + ">" + model.AgentCity + ">" + model.AgentArea; string strArea1 = model.Province + "-" + model.City + "-" + model.Area; string[] strArr = strArea.Split('>'); string[] strArr1 = strArea1.Split('-'); string strAreaJoin = strArea.Length < 3 ? "未知" : strArea; string strAreaJoin1 = strArea1.Length < 3 ? "未知" : strArea1; string resultArea = String.Empty; string strProvince = model.AgentProvince; dict.TryGetValue(strProvince, out resultArea); DataRow dr = dt.NewRow(); DR [ " Login User " ] = model.UserName; dr [ " real name " ] = String ? .IsNullOrEmpty (model.RealName) " Unknown " : model.RealName; dr [ " partner name " ] = model.AgentName; DR [ " Area " ] = strAreaJoin; DR [ " ordinary area " ] = String ? .IsNullOrEmpty (resultArea) " Unknown " : resultArea; DR [ " Contacts " ] = model.AgentContact; dr [ " home address " ] = strAreaJoin1; dr [ " Address " ] = String ? .IsNullOrEmpty (model.Address) " Unknown " : model.Address; dr["用户角色"] = (model.UserRole == null) || (model.UserRole == "0") || (model.UserRole == "") ? "未知" : Web.Helper.ViewHelper.GetUserRoleName(model.UserRole); DR [ " login time " ] = ARR [ 2 ] + " - " + ARR [ 0 ] + " - " + ARR [ . 1 ]; DR [ " log number " ] model.LoginCount ?? = "" ; dt.Rows.Add(dr); } string selRoleName = userRole == "" ? "全部用户" : Web.Helper.ViewHelper.GetUserRoleName(userRole); string ExcelFileName = selRoleName + "-日志" + "-" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".xls"; // Firefox IF (the BrowserType == " Firefox " ) { // attachment download operation String contentType = " file application / X-XLS " ; byte [] = byteArr new new Web.Helper.ExportExcelOprate () ExportExcelDT (dt).; FileContentResult fcResult = new FileContentResult(byteArr, contentType); fcResult.FileDownloadName = ExcelFileName; fcResult.ExecuteResult(this.ControllerContext); } // non-Firefox browsers the else {
// attachment downloads Response.ContentType = "application/x-xls"; Response.ContentEncoding = System.Text.Encoding.GetEncoding ( " UTF-8 " ); String encodFileName = HttpUtility.UrlEncode (System.Text.Encoding.UTF8.GetBytes (ExcelFileName)); // solve Chinese name, the garbage problem Response.AppendHeader("Content-Disposition", "attachment;filename=" + encodFileName); Response.Write(System.Text.Encoding.UTF8.GetString(new Web.Helper.ExportExcelOprate().ExportExcelDT(dt))); }
ExportExcelOprate General Method class:
// Created: Liwei // Created: 2012/10/25 public class ExportExcelOprate { /// <the Summary> /// Export Excel Datatable version /// </ the Summary> /// <param name = "dt"> Export of Datatable </ param> /// <param name = "ExcelName"> Export EXCEL the name does not need to have an extension _xls </ param> public byte [] ExportExcelDT (the DataTable dt) { System.Text.StringBuilder sbStr = new System.Text.StringBuilder(); string colHeaders = "", ls_item = ""; The DataRow [] myRow = dt.Select (); // can similarly dt.Select ( "id> 10") in the form of data to achieve the purpose of screening int I = 0 ; int Cl = dt.Columns.Count; // obtain the data table each column headings, t is divided between the headline, a column header added after the last carriage sbStr.Append ( " <HTML> <head> <Meta HTTP-equiv = \" the Content-the Type \ "Content = \" text / html; charset = utf-8 \ "/> </ head> <body> <table border = 1> <tr style = \" color: Black; border: Gray 1px solid; text-align: center; \ "> " ); for (I = 0 ; I <Cl; I ++ ) { colHeaders += "<th>" + dt.Columns[i].Caption.ToString() + "</th>"; } sbStr.Append (colheaders + " </ TR> " ); // write data acquired information to the HTTP output stream // processed line data the foreach (the DataRow Row in myRow) { // current line data is written to the HTTP output stream, and for downlink data blanking ls_item ls_item = " <TR style = 'text-align = left: Center; Color: Black; font-size: 15px;'> " ; for (I = 0 ; I <Cl; I ++ ) { IF (I == (Cl - . 1 )) // the last column, add n- { ls_item += "<td>" + row[i].ToString() + "</td></tr>"; } else { ls_item += "<td>" + row[i].ToString() + "</td>"; } } sbStr.Append(ls_item); } sbStr.Append("</table></body></html>"); return System.Text.Encoding.UTF8.GetBytes(sbStr.ToString()); } }
Reproduced in: https: //www.cnblogs.com/Kummy/archive/2013/04/03/2957243.html