输出excel

protected void Page_Load(object sender, EventArgs e)
    {
        //List<Web.DLL.wxItem.userlist> list = Web.DLL.wxItem.userlist.getlistbywhere(GlobalData.GetDBHelper_wxItem(), "");
        //Dictionary<string, string> d = new Dictionary<string, string>();
        //d.Add("Name", "姓名");
        //d.Add("MobilePhone", "电话");
        //d.Add("AddIpCity", "IP地址");

        //ExExcel(list, d, "test");


        IDBHelper dbhelper = GlobalData.GetDBHelper_wxItem();
        DataTable dt = dbhelper.ExecuteDataTable("select a.*,b.name as groupname from news a,news_group b  where a.groupid=b.id");
        dbhelper.CloseDBConnection();

        List<excelmodel> emlist = new List<excelmodel>();
        foreach (System.Data.DataRow dr in dt.Rows)
        {
            excelmodel em = new excelmodel();
            em.groupname = dr["groupname"].ToString();
            em.name = dr["name"].ToString();
            em.addtime = dr["addtime"].ToString();
            emlist.Add(em);
        }
        Dictionary<string, string> d = new Dictionary<string, string>();
        d.Add("groupname", "类别");
        d.Add("name", "标题");
        d.Add("addtime", "时间");
        ExExcel(emlist, d, "test");

    }

 public class excelmodel
    {
        public string groupname { get; set; }
        public string name { get; set; }
        public string addtime { get; set; }
    }

 /// <summary> 
    /// 将一组对象导出成EXCEL 
    /// </summary> 
    /// <typeparam name="T">要导出对象的类型</typeparam> 
    /// <param name="objList">一组对象</param> 
    /// <param name="FileName">导出后的文件名</param> 
    /// <param name="columnInfo">列名信息</param> 
    public void ExExcel<T>(List<T> objList, Dictionary<string, string> columnInfo, string FileName)
    {
        if (columnInfo.Count == 0) { return; }
        if (objList.Count == 0) { return; }
        //生成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] + "\t";
            }
        }
        //如果没有找到可用的属性则结束 
        if (myPro.Count == 0) { return; }
        excelStr += "\r";
        foreach (T obj in objList)
        {
            foreach (System.Reflection.PropertyInfo p in myPro)
            {
                excelStr += p.GetValue(obj, null) + "\t";
            }
            excelStr += "\r";
        }
        //输出EXCEL 
        HttpResponse rs = System.Web.HttpContext.Current.Response;
        rs.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        rs.AppendHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");
        rs.ContentType = "application/ms-excel";
        rs.Write(excelStr);
        rs.End();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哇哈哈AD钙奶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值