NPOI导出Excel表格

1.首先,在项目中引用npoi

 

2.后台代码:

        public void Export()
        {
            using (var conn = new PersonDBContext())
            {
                //获取数据
                List<Person> personList = conn.Persons.ToList();
                var num = personList.Count();

                //获取类型的属性数量
                var typeNum = typeof(Person).GetProperties().Count();

                HSSFWorkbook workbook = new HSSFWorkbook();
                //创建一个页签
                var sheet1 = workbook.CreateSheet("Sheet1");

                //设置表头
                IRow row = sheet1.CreateRow(0);

                row.CreateCell(0).SetCellValue("编号");
                row.CreateCell(1).SetCellValue("姓名");
                row.CreateCell(2).SetCellValue("年龄");
                row.CreateCell(3).SetCellValue("性别");
                row.CreateCell(4).SetCellValue("地址");
                row.CreateCell(5).SetCellValue("电话");

                //创建行
                for (var i = 0; i < num; i++)
                {
                    IRow row1 = sheet1.CreateRow(i+1);

                    //创建单元格,填充数据
                    row1.CreateCell(0).SetCellValue(personList[i].Id.ToString());
                    row1.CreateCell(1).SetCellValue(personList[i].Name.ToString());
                    row1.CreateCell(2).SetCellValue(personList[i].Age.ToString());
                    row1.CreateCell(3).SetCellValue(personList[i].Sex.ToString());
                    row1.CreateCell(4).SetCellValue(personList[i].Address.ToString());
                    row1.CreateCell(5).SetCellValue(personList[i].Phone.ToString());

                }

                //设置行宽度

                sheet1.SetColumnWidth(2, 10 * 256);

                //获取单元格  并设置样式

                ICellStyle styleCell = workbook.CreateCellStyle();

                //居中
                styleCell.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;

                //垂直居中
                styleCell.VerticalAlignment = VerticalAlignment.Top;
                ICellStyle cellstyle = workbook.CreateCellStyle();

                //设置字体

                IFont fontColorRed = workbook.CreateFont();
                fontColorRed.Color = HSSFColor.OliveGreen.Red.Index;

                styleCell.SetFont(fontColorRed);
                
                


                //输出Execl

                string fileName = string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now).Trim();
                fileName = fileName + ".xls";
                var context = HttpContext;
                context.Response.ContentType = "application/vnd.ms-excel";
                context.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", context.Server.UrlEncode(fileName)));
                context.Response.Clear();

                MemoryStream file = new MemoryStream();
                workbook.Write(file);
                context.Response.BinaryWrite(file.GetBuffer());
                context.Response.End();
            }

        }

3.前台调用

<a href="/Home/Export">666</a>

4.结果

 

 

注:好像需要  a标签才能导出,AJAX的异步调取无效,或者 采用  window.open  或者 window.location.href

 

参考:http://www.cnblogs.com/rhythmK/p/3833393.html

 

转载于:https://www.cnblogs.com/dzw159/p/6728545.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值