码迷,mamicode.com
首页 > Windows程序 > 详细

C# 关于导出EXCEL功能

时间:2015-06-23 19:30:49      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

 /// <summary>
        /// 导出Excel数据
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public ActionResult ExportExcel(SearchEntity search)//查询条件
        {

            List<Models.PerLoan.PersonalLoanExcel> perLoanList = new List<Models.PerLoan.PersonalLoanExcel>();

  形成的excel对象[Description("贷款期限")] public int? LoanLimit { get; set; }  形成表标题


            perLoanList = PersonalLoanDAL.GetProcessLoanExcelNew(city, search, status, CurrentUser.RoleId, CurrentUser.UserId, CurrentUser);//查询到的列表

            string fileName = DateTime.Now.ToString("yyyyMMddHHmm") + "_个人业务查询";
            DAL.Common.ExportExcel<Models.PerLoan.PersonalLoanExcel> exportExcel = new DAL.Common.ExportExcel<Models.PerLoan.PersonalLoanExcel>(fileName, perLoanList);
            exportExcel.ResponseExcel(1);
            return View();
        }

 

 

 

public class ExportExcel<T> where T:class
    {
        public ExportExcel(string fileName, List<T> list)
        {
            _fileName = fileName;
            _listT = list;
        }

        private HSSFWorkbook workbook;

        private string _fileName;
        public string FileName
        {
            get { return _fileName; }
            set { _fileName = value; }
        }

        private List<T> _listT;
        public List<T> ListT
        {
            get { return _listT; }
            set { _listT = value; }
        }

        /// <summary>
        /// 向客户端输出Excel
        /// </summary>
        /// <param name="ordertype">1 个人  2 经纪人</param>
        public void ResponseExcel(int ordertype)
        {
            HttpResponse response = HttpContext.Current.Response;
            response.ContentType = "application/vnd.ms-excel";
            response.HeaderEncoding = System.Text.Encoding.GetEncoding("gb2312");
            response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", _fileName + ".xls"));
            response.Clear();

            InitialiseWorkBook();
            CreateExcelData(ordertype);

            using (MemoryStream file = new MemoryStream())
            {
                workbook.Write(file);
                file.WriteTo(response.OutputStream);
            }
            response.End();
        }

        private void InitialiseWorkBook()
        {
            workbook = new HSSFWorkbook();
            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "SouFun";
            workbook.DocumentSummaryInformation = dsi;

            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "Export data";
            workbook.SummaryInformation = si;
        }

        /// <summary>
        /// 根据List中的内容生成excel数据
        /// </summary>
        /// <param name="ordertype">订单类型  1个人  2经纪人</param>
        private void CreateExcelData(int ordertype)
        {
            ISheet sheet = workbook.CreateSheet(_fileName);

            IRow rowHead = sheet.CreateRow(0);

            Type t = typeof(T);
            int column=0;
            int headNum = 0;
            foreach (PropertyInfo pi in t.GetProperties())
            {
                string headValue = string.Empty;
                object[] des = pi.GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (des.Length > 0)
                {
                    DescriptionAttribute da = (DescriptionAttribute)des[0];
                    headValue = da.Description;
                    rowHead.CreateCell(headNum, CellType.STRING).SetCellValue(headValue);
                    headNum++;
                }
                
                column++;
            }

            for (int r = 0; r < _listT.Count; r++)
            {
                IRow row = sheet.CreateRow(r + 1);

                int c = 0;
                int c1 = 0;
                decimal money=0;
                decimal interviermoney = 0;
                decimal pidaimoney = 0;
                decimal fangkuanmoney = 0;
                decimal bankreturncharge = 0;
                decimal commissionrate = 0;
                int gender = -1;

                foreach (PropertyInfo pi in t.GetProperties())
                {
                    string v = string.Empty;
                    object o = pi.GetValue(_listT[r], null);
                    object[] des = pi.GetCustomAttributes(typeof(DescriptionAttribute), true);
                    if (des.Length > 0)
                    {
                        if (o != null)
                        {
                            if (pi.Name.ToLower() == "status" || pi.Name.ToLower() == "prevstatus")
                            {
                                v = DAL.Common.Order.GetCurrentStatus(int.Parse(o.ToString()));
                            }
                            else if (pi.Name.ToLower() == "gender") //case when Gender = 0 then ‘女‘ when Gender = 1 then ‘男‘ else ‘‘ end as
                            {
                                gender = Convert.ToInt32(o);
                                if (o == null)
                                {
                                    v = "";
                                }
                                else
                                {
                                    if (gender==1)
                                    {
                                        v = "男";  
                                    }
                                    else if (gender == 0)
                                    {
                                        v = "女";
                                    }
                                }
                            }
                            else if (pi.Name.ToLower() == "datafrom")
                            {
                                v = DAL.Common.Order.GetOrderFrom(int.Parse(o.ToString()), ordertype);
                            }
                            else if (pi.Name.ToLower() == "interviewmoney")
                            {
                                interviermoney = Convert.ToDecimal(o);
                                v = interviermoney.ToString() + "万元";
                            }
                            else if(pi.Name.ToLower()=="allowappmoney")
                            {
                                pidaimoney = Convert.ToDecimal(o);
                                v = pidaimoney.ToString() + "万元";
                            }
                            else if (pi.Name.ToLower() == "fangkuanmoney")
                            {
                                fangkuanmoney = Convert.ToDecimal(o);
                                v = fangkuanmoney.ToString();
                            }
                            else if (pi.Name.ToLower() == "bankreturncharge")
                            {
                                bankreturncharge = Convert.ToDecimal(o);
                                v = bankreturncharge.ToString() + "%";
                            }
                            else if (pi.Name.ToLower() == "commissionrate")
                            {
                                commissionrate = Convert.ToDecimal(o);
                                v = commissionrate.ToString() + "%";
                            }
                            //else if (pi.Name.ToLower() == "nicknamefrom")
                            //{
                            //    if (o.ToString() == "0") { v = "金融"; }
                            //    else if (o.ToString() == "1") { v = "二手房"; }
                            //}
                            else if (pi.Name.ToLower() == "bankreturnmoney")
                            {
                                if (Convert.ToDecimal(o) <= 0M)
                                {
                                    if (interviermoney > 0) { money = interviermoney; }
                                    else if (pidaimoney > 0) { money = pidaimoney; }
                                    else if (fangkuanmoney > 0) { money = fangkuanmoney; }
                                    if (money > 0 && bankreturncharge > 0)
                                    {
                                        v = (money * 10000 * bankreturncharge / 100).ToString() + "元";
                                    }
                                }
                                else
                                {
                                    v = o.ToString() + "元";
                                }
                            }
                            else if (pi.Name.ToLower() == "commissionmoney")
                            {
                                if (interviermoney > 0) { money = interviermoney; }
                                else if (pidaimoney > 0) { money = pidaimoney; }
                                else if (fangkuanmoney > 0) { money = fangkuanmoney; }
                                if (money > 0 && commissionrate > 0)
                                {
                                    v = (money * commissionrate / 100).ToString() + "万元";
                                }
                            }
                            else if (pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                            {
                                Type ptype = pi.PropertyType.GetGenericArguments()[0];
                                if (ptype == typeof(System.DateTime))
                                {
                                    v = Convert.ToDateTime(o).ToString("yyyy-MM-dd");
                                }
                                else
                                {
                                    v = o.ToString();
                                }
                            }
                            else
                            {
                                v = o.ToString();
                            }
                            
                        }
                        row.CreateCell(c1, CellType.STRING).SetCellValue(v);
                        c1++;
                    }
                    c++;
                }

            }
        }
    }

C# 关于导出EXCEL功能

标签:

原文地址:http://www.cnblogs.com/eric-gms/p/4595818.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!