zoukankan      html  css  js  c++  java
  • 泛型的导入导出的公用方法

    using NPOI.HSSF.UserModel;
    using NPOI.SS.Formula.Functions;
    using NPOI.SS.UserModel;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    namespace Farinfo.Ethics.Common
    {
        public class IntroductionAndExport<T> where T : class,new()
        {
            //导出
            public static void RenderToExcel(IEnumerable<T> list,string title)
            {
                IWorkbook workbook = new HSSFWorkbook();
                ISheet sheet = workbook.CreateSheet();
                DirectoryInfo imagesfile = new DirectoryInfo(("d:/"));
                string strFileName = imagesfile.FullName.ToString();
                int index = 0;
                //填充表头
                IRow dataRow = sheet.CreateRow(0);
                foreach (PropertyInfo pi in list.FirstOrDefault().GetType().GetProperties())
                {
                    dataRow.CreateCell(index).SetCellValue(pi.Name);
                    index++;
                }
                int id = 0;
                foreach (T model in list)
                {
                    id = id + 1;
                    IRow headerRow = sheet.CreateRow(id);
                    PropertyInfo[] proList = model.GetType().GetProperties();
    
                    for (int i = 0; i < proList.Count(); i++)
                    {
                        var name = proList[i].GetValue(model, null);
                        if (name == null)
                            headerRow.CreateCell(i).SetCellValue("");
                        else
                            headerRow.CreateCell(i).SetCellValue(name.ToString());
                    }
                }
                //保存
                using (MemoryStream ms = new MemoryStream())
                {
                    workbook.Write(ms);
                    File.Create(strFileName + title + ".xls").Close();
                    using (FileStream fs = new FileStream(strFileName + DateTime.Now.Second + ".xls", FileMode.Create, FileAccess.Write))
                    {
                        byte[] data = ms.ToArray();
                        fs.Write(data, 0, data.Length);
                        fs.Flush();
                        fs.Close();
                        fs.Dispose();
                    }
                }
            }
            //导入
            public static void WriterToExcel(out List<T> list, string fileName)
            {
                list = new List<T>();
                using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
                {
    
                    IWorkbook workbook = new HSSFWorkbook(stream);
                    ISheet sheet = workbook.GetSheetAt(0);
                    //Execel第一是标题,不是要导入数据库的数据
                    for (int i = 1; i <= sheet.LastRowNum; i++)
                    {
                        T model = new T();
                        IRow dataRow = sheet.GetRow(i);
                        for (int j = 0; j < dataRow.Cells.Count; j++)
                        {
                            ICell cell = dataRow.Cells[j];
                            int num;
                            Guid id;
                            DateTime time;
                            if (model.GetType().GetProperties()[j].Name != "Mark")
                            {
                                if (int.TryParse(cell.StringCellValue, out num))
                                {
                                    model.GetType().GetProperties()[j].SetValue(model, num, null);
    
                                }
                                else if (Guid.TryParse(cell.StringCellValue, out id))
                                {
                                    model.GetType().GetProperties()[j].SetValue(model, id, null);
                                }
                                else if (DateTime.TryParse(cell.StringCellValue, out time))
                                {
                                    model.GetType().GetProperties()[j].SetValue(model, time, null);
                                }
                                else if (cell.StringCellValue == "")
                                {
                                    model.GetType().GetProperties()[j].SetValue(model, null, null);
    
                                }
                                else
                                {
                                    model.GetType().GetProperties()[j].SetValue(model, cell.StringCellValue, null);
                                }
                            }
                        }
                        list.Add(model);
                    }
                }
            }
        }
    }
    

      

  • 相关阅读:
    zbb20181207 springboot @ConfigurationProperties使用
    zbb20181206 logback,lombok 默认日志logback配置解析
    Spring Boot (8) 全局异常处理
    Spring Boot (7) JdbcTemplate访问数据库
    Spring Boot (6) Spring Data JPA
    Spring Boot (4) 静态页面和Thymeleaf模板
    Spring Boot (3) 热部署devtools
    Spring Boot (2) Restful风格接口
    Spring Boot (1) 构建第一个Spring Boot工程
    idea使用maven搭建ssm框架实现登陆商品增删改查
  • 原文地址:https://www.cnblogs.com/liuchang/p/4220560.html
Copyright © 2011-2022 走看看