public ActionResult ExportExcel(string gridUrl, string referUrl, string pageNumber, string pageSize, string queryParams, string title, string sortName, string sortOrder, string columns) { var postData = new Dictionary <string, object>(); if (!string.IsNullOrEmpty(queryParams)) { postData = queryParams.JsonToDictionary(); } var serverUrl = string.Format("{0}://{1}", Request.Url.Scheme, Request.Headers["Host"]); var requestUrl = gridUrl; var dicList = GetDicList(serverUrl, requestUrl, referUrl, postData); DataTable dt = null; if (!string.IsNullOrEmpty(columns)) { dt = new DataTable(); var columnDic = columns.JsonToDictionaryList(); foreach (var dic in columnDic) { if (!dt.Columns.Contains(dic.GetValue("title"))) { dt.Columns.Add(dic.GetValue("title")); } } foreach (var dic in dicList) { DataRow dr = dt.NewRow(); foreach (var item in dic) { var column = columnDic.FirstOrDefault(a => a.GetValue("field") == item.Key); if (column != null) { dr[column.GetValue("title")] = item.Value; } } dt.Rows.Add(dr); } } else { dt = DictionaryExtend.ConvertDicToTable(dicList.ToList()); } var buffer = NPOIExcelHelper.Export(dt, "").GetBuffer(); buffer.CheckNotNull("导出excel出现异常"); return(File(buffer, "application/vnd.ms-excel", Url.Encode(title) + ".xls")); }
public JsonResult ExportExcel() { string code = QueryString("UICode"); code.CheckNotNullOrEmpty("UICode"); var listConfig = UnitOfWork.GetSingle <ListConfig>(a => a.UICode == code); listConfig.CheckNotNull("listConfig"); var colList = listConfig.GetColumnList();//列设置 #region 数据库数据 string sql = listConfig.SQL; if (!string.IsNullOrEmpty(listConfig.OrderBy)) { if (!listConfig.OrderBy.ToLower().Contains("order") && !listConfig.OrderBy.ToLower().Contains("by")) { sql += (" order by " + listConfig.OrderBy); } else { sql += (" " + listConfig.OrderBy); } } var res = UnitOfWork.DynamicListFromSql(listConfig.DBName, sql); #endregion //fieldName替换为标题 var dt = DictionaryExtend.ConvertDicToTable(res.ToList()); foreach (DataColumn dataCol in dt.Columns) { var colItem = colList.FirstOrDefault(a => a.GetValue("field") == dataCol.ColumnName); if (colItem != null) { dataCol.ColumnName = colItem.GetValue("title"); } } NPOIExcelHelper.ExportByWeb(dt, listConfig.Name, listConfig.Name); return(Json("")); }