C# Excel模板导出数据,基于NPOI

  1. Excel模板
    C# Excel模板导出数据,基于NPOI_第1张图片
  2. Helper类
	public class Helper<T> where T : class, new()
    {
     
        /// 
        /// 使用模板导出Excel
        /// 
        /// 数据
        /// 模板路径
        /// 从第几行插入
        /// 
        public static byte[] TemplateOutput(List<T> list, string path, int index)
        {
     
            if (list.Count() == 0)
            {
     
                return new byte[] {
      };
            }
            using (MemoryStream ms = new MemoryStream())
            {
     
                var entityProperties = list.FirstOrDefault().GetType().GetProperties();
                string extension = Path.GetExtension(path);
                FileStream fs = File.OpenRead(path);
                IWorkbook workbook = null;
                if (extension.Equals(".xls"))
                {
     
                    workbook = new HSSFWorkbook(fs);
                }
                else
                {
     
                    workbook = new XSSFWorkbook(fs);
                }
                fs.Close();
                ISheet sheet = workbook.GetSheetAt(0);
                IRow rows = null;
                int i = 0;
                foreach (var dbitem in list)
                {
     
                    rows = sheet.CreateRow(i++ + index);
                    for (int j = 0; j < entityProperties.Length; j++)
                    {
     
                        var prop = entityProperties[j];
                        var value = prop.GetValue(dbitem)?.ToString();
                        rows.CreateCell(j).SetCellValue(value ?? "");
                    }
                }
                workbook.Write(ms);
                return ms.GetBuffer();
            }
        }
    }
  1. 导出方法以及涉及对象
//引入的命名空间
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;

		/// 
        /// Excel模板导出
        /// 
        public void ExcelExport()
        {
     
            string templetPath = Context.Server.MapPath("/content/test.xls");
            List<TableStructure> tableStructure = new List<TableStructure>() {
      
                new TableStructure()
                {
     
                    Field = "Id",
                    Type = "int",
                    Desdescribe = "Desdescribe",
                    Remark = "Remark"
                }
            };
            byte[] file = Helper<TableStructure>.TemplateOutput(tableStructure, templetPath, 2);

            Context.Response.ContentType = "application/vnd.ms-excel";
            // 添加头信息,指定文件名格式   
            Context.Response.AddHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmmsss") + ".xls");
            Context.Response.AddHeader("Content-Transfer-Encoding", "binary");
            Context.Response.ContentType = "application/octet-stream";
            Context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            Context.Response.BinaryWrite(file);
        }

        public class TableStructure
        {
     
            public string Field {
      get; set; }
            public string Type {
      get; set; }
            public string Desdescribe {
      get; set; }
            public string Remark {
      get; set; }
        }

你可能感兴趣的:(.net,C#,excel,C#,NPOI,Excel模板导出,.Net,NPOI,.NET,Excel)