泛型的导入导出的公用方法

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

  

posted @ 2015-01-13 10:15  流浪的狸猫  阅读(608)  评论(0编辑  收藏  举报