Exemple #1
0
 /// <summary>
 /// 导出excel
 /// </summary>
 /// <typeparam name="T">集合列表里对象的类型</typeparam>
 /// <param name="list">数据列表</param>
 /// <returns></returns>
 protected FileResult ToExcel(DataTable list)
 {
     #region 获取XML
     XMLHelp xmlhelp = new Common.XMLHelp(string.Format("/XmlConfig/excel/{0}-{1}.xml", ControllerContext.RouteData.DataTokens["area"].ToString(), ControllerContext.RouteData.Values["controller"].ToString()));
     var     fields  = xmlhelp.GetList <Xml_Field>().Select(a => new KeyValuePair <string, string>(a.code, a.name)).ToList();
     #endregion
     var dirPath = Server.MapPath("/upload/toexcel/");
     if (System.IO.Directory.Exists(dirPath) == false)
     {
         System.IO.Directory.CreateDirectory(dirPath);
     }
     var filePath = string.Format("{0}{1}.xls", dirPath, System.Guid.NewGuid().ToString());
     ExcelHelp.ToExcelByNPOI(list, filePath, fields);
     return(File(filePath, "application/vnd.ms-excel"));
 }
Exemple #2
0
 /// <summary>
 /// 导出excel
 /// </summary>
 /// <typeparam name="T">集合列表里对象的类型</typeparam>
 /// <param name="list">数据列表</param>
 /// <returns></returns>
 protected FileResult ToExcel(DataTable list, string xmlFileName)
 {
     #region 获取XML
     XMLHelp xmlhelp = new XMLHelp(string.Format("/XmlConfig/excel/{0}.xml", xmlFileName));
     var     fields  = xmlhelp.GetList <Xml_Field>().Select(a => new KeyValuePair <string, string>(a.code, a.name)).ToList();
     #endregion
     var dirPath = Server.MapPath("/upload/toexcel/");
     if (System.IO.Directory.Exists(dirPath) == false)
     {
         System.IO.Directory.CreateDirectory(dirPath);
     }
     var filePath = string.Format("{0}{1}.xls", dirPath, WebTools.getGUID());
     ExcelHelp.ToExcelByNPOI(list, filePath, fields);
     return(File(filePath, "application/vnd.ms-excel"));
 }