zoukankan      html  css  js  c++  java
  • Excel导出公共函数

           /// <summary> 
            /// 将一组对象导出成EXCEL 
            /// </summary> 
            /// <typeparam name="T">要导出对象的类型</typeparam> 
            /// <param name="objList">一组对象</param> 
            /// <param name="FileName">导出后的文件名</param> 
            /// <param name="columnInfo">列名信息</param> 
            public void Excel<T>(List<T> objList, string FileName, Dictionary<string, string> columnInfo)
            {
                //生成EXCEL的HTML 
                string excelStr = "";
                Type myType = objList[0].GetType();
                //根据反射从传递进来的属性名信息得到要显示的属性 
                List<System.Reflection.PropertyInfo> myPro = new List<System.Reflection.PropertyInfo>();
                foreach (string cName in columnInfo.Keys)
                {
                    System.Reflection.PropertyInfo p = myType.GetProperty(cName);
                    if (p != null)
                    {
                        myPro.Add(p);
                        excelStr += columnInfo[cName] + "	";
                    }
                }
                //如果没有找到可用的属性则结束 
                excelStr += "
    ";
                foreach (T obj in objList)
                {
                    foreach (System.Reflection.PropertyInfo p in myPro)
                    {
                        excelStr += p.GetValue(obj, null) + "	";
                    }
                    excelStr += "
    ";
                }
                //输出EXCEL 
                HttpResponse rs = System.Web.HttpContext.Current.Response;
                rs.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                rs.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8));
                rs.ContentType = "application/ms-excel";
                rs.Write(excelStr);
                rs.End();
            }
  • 相关阅读:
    ssh 使用密钥文件
    VS2015企业版,社区版,专业版详细对比
    Redis 与 数据库处理数据的两种模式(转)
    工业级物联网项目架构设计思想(转)
    C# and Redis,安装作为服务
    C# CRC32
    c++,C# 转换
    app配置智能硬件的解决方案
    C# 与C++的数据转换
    C++ 对数组sizeof 和对数组元素sizeof
  • 原文地址:https://www.cnblogs.com/CielWater/p/3540491.html
Copyright © 2011-2022 走看看