在网上找了一堆类似的导出方法,因为涉及到前后台的问题,前端页面显示的数据是由后台提供的接口,通过获取到json数据,因为我只用写后台方法提供给前端用导出功能,目前导出的功能能实现了,代码如下,帮忙改进一下也可以namespace exportexcel.Common
{
    public class ExcelClass
    {
        /// <summary>
        /// 实体类集合导出到Excle2003
        /// </summary>
        /// <param name="cellHeard">单元头的Key和Value</param>
        /// <param name="enList">数据源</param>
        /// <param name="sheetName">工作表名称</param>
        /// <returns>文件的下载地址</returns>
        public static string EntityListToExcel2003(Dictionary<string, string> cellHeard, IList enList, string sheetName)
        {
            try
            {
                string fileName = sheetName + "-" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls"; // 文件名称
                string urlPath = "UpFiles/ExcelFiles/" + fileName; // 文件下载的URL地址,供给前台下载
                string filePath = HttpContext.Current.Server.MapPath("\\" + urlPath); // 文件路径                //检测是否存在文件夹,若不存在就建立个文件夹
                string directoryName = Path.GetDirectoryName(filePath);
                if (!Directory.Exists(directoryName))
                {
                    Directory.CreateDirectory(directoryName);
                }                //解析单元格头部,设置单元头的中文名称
                HSSFWorkbook workbook = new HSSFWorkbook();
                ISheet sheet = workbook.CreateSheet(sheetName);
                IRow row = sheet.CreateRow(0);
                List<string> keys = cellHeard.Keys.ToList();
                for (int i = 0; i < keys.Count; i++)
                {
                    row.CreateCell(i).SetCellValue(cellHeard[keys[i]]); 
                }                // List对象的值赋值到Excel的单元格里
                int rowIndex = 1;
                foreach (var en in enList)
                {
                    IRow rowTmp = sheet.CreateRow(rowIndex);
                    for (int i = 0; i < keys.Count; i++)
                    {
                        string cellValue = ""; 
                        object properotyValue = null; 
                        System.Reflection.PropertyInfo properotyInfo = null;                        //若属性头的名称包含'.',就表示是子类里的属性,那么就要遍历子类
                        if (keys[i].IndexOf(".") >= 0)
                        {
                            string[] properotyArray = keys[i].Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
                            string subClassName = properotyArray[0]; 
                            string subClassProperotyName = properotyArray[1];
                            System.Reflection.PropertyInfo subClassInfo = en.GetType().GetProperty(subClassName);
                            if (subClassInfo != null)
                            {
                                var subClassEn = en.GetType().GetProperty(subClassName).GetValue(en, null);
                                properotyInfo = subClassInfo.PropertyType.GetProperty(subClassProperotyName);
                                if (properotyInfo != null)
                                {
                                    properotyValue = properotyInfo.GetValue(subClassEn, null); 
                                }
                            }
                        }
                        else
                        {
                            //若不是子类的属性,直接根据属性名称获取对象对应的属性
                            properotyInfo = en.GetType().GetProperty(keys[i]);
                            if (properotyInfo != null)
                            {
                                properotyValue = properotyInfo.GetValue(en, null);
                            }
                        }                        // 属性值经过转换赋值给单元格值
                        if (properotyValue != null)
                        {
                            cellValue = properotyValue.ToString();
                            if (cellValue.Trim() == "0001/1/1 0:00:00" || cellValue.Trim() == "0001/1/1 23:59:59")
                            {
                                cellValue = "";
                            }
                        }
                        rowTmp.CreateCell(i).SetCellValue(cellValue);
                    }
                    rowIndex++;
                }
                FileStream file = new FileStream(filePath, FileMode.Create);
                workbook.Write(file);
                file.Close();                return urlPath;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
但是测试时的数据是写死的,而需要完成的功能是导出页面显示数据,页面数据前面说过了是由接口提供的json数据拼接的一个表格,怎么直接得到这个页面显示数据,因为有三四十个页面需要有导出功能,所以需要同时适用。测试功能代码如下,数据是直接我给出写死的
namespace exportexcel.Ajax
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class Handler1 : IHttpHandler
    {
        public bool IsReusable
        {
            get
            {
                throw new NotImplementedException();
            }
        }               public void ProcessRequest(HttpContext context)
        {
            try
            {
                List<UserEntity> enlist = new List<UserEntity>() {
                      new UserEntity{Name="刘一",Age=22,Gender="Male",ChineseScores=80,MathScores=90},
                      new UserEntity{Name="陈二",Age=23,Gender="Male",ChineseScores=81,MathScores=91},
                      new UserEntity{Name="张三",Age=24,Gender="Male",ChineseScores=82,MathScores=92},
                      new UserEntity{Name="李四",Age=25,Gender="Male",ChineseScores=83,MathScores=93},
                      new UserEntity{Name="王五",Age=26,Gender="Male",ChineseScores=84,MathScores=94},
                    };
                Dictionary<string, string> cellheader = new Dictionary<string, string> {
                      { "Name", "姓名" },
                      { "Age", "年龄" },
                      { "Gender", "性别" },
                      { "ChineseScores", "语文成绩" },
                      { "MathScores", "数学成绩" },
                    };                string urlPath = ExcelClass.EntityListToExcel2003(cellheader, enlist, "学生成绩");
                System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
                context.Response.ContentType = "text/plain";
                context.Response.Write(js.Serialize(urlPath));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}测试导出的效果就是这样,还算满意,但是想达到项目想要的结果,就要获取页面动态数据之前有个思路,就是通过条件,直接后台数据库中去查数据,得到这个数据后调用写好的方法拼接,但是遇到一个问题,因为数据库有的表中带有经纬度字段,想要获取到像页面显示的具体地址,需要用到反地理编码,这个没玩过不会啊,所以还是想请教一下大神们,如何获取到当前页面所显示的数据