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);
                    }
                }
            }
        }
    }
    

      

  • 相关阅读:
    赫夫曼树相关算法
    用栈来实现 括号匹配 字符序列检验
    二叉树的建立和遍历
    数据结构-算术表达式求值
    构造一个单链表L,其头结点指针为head,编写程序实现将L逆置
    单链表的基本操作(C语言)数据结构
    java代码打印杨辉三角
    无标题
    写一个方法,判断给定的数字是偶数还是奇数
    关于生物信息学与R的相关资料和网站
  • 原文地址:https://www.cnblogs.com/liuchang/p/4220560.html
Copyright © 2011-2022 走看看