导出导出Execl demo

using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;

using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;

namespace 导出
{
    class Program
    {
        static void Main(string[] args)
        {
            var timeold = DateTime.Now;
            Console.WriteLine("这是一个学生表:");
            List<Student> students = new List<Student>();

           
                students.Add(new Student { Name = "张三", Sex = "男", Age = 20 });
                students.Add(new Student { Name = "李四", Sex = "女", Age = 21 });
          


            //foreach (var item in students)
            //{
            //    Console.WriteLine(item.Name + "," + item.Sex + "," + item.Age);
            //}
            //调用导出方法
            ToExecl(students);

            //调用导入方法
            ReadExecl();
            var timenew = DateTime.Now;
            var time = timenew - timeold;
            Console.WriteLine("总共耗时:{0}",time);
            Console.ReadKey();
        }
        //导出方法
        static void ToExecl(List<Student> students)
        {
            //创建工作簿对象
            IWorkbook workbook = new HSSFWorkbook();
            //创建工作表
            ISheet sheet = workbook.CreateSheet("onesheet");
            IRow row0 = sheet.CreateRow(0);
            row0.CreateCell(0).SetCellValue("姓名");
            row0.CreateCell(1).SetCellValue("性别");
            row0.CreateCell(2).SetCellValue("年龄");
            for (int r = 0; r < students.Count; r++)
            {
                //创建行row
                IRow row = sheet.CreateRow(r + 1);
                row.CreateCell(0).SetCellValue(students[r].Name);
                row.CreateCell(1).SetCellValue(students[r].Sex);
                row.CreateCell(2).SetCellValue(students[r].Age);
            }

            //创建流对象并设置存储Excel文件的路径
            using (FileStream url = File.OpenWrite(@"C:\Users\FOX\Desktop\新建文件夹\写入excel.xls"))
            {

                //导出Excel文件
                workbook.Write(url);
                Console.WriteLine("写入成功!");
            };
        }

        static void ReadExecl()
        {
            //首先根据需要读取的文件创建一个文件流对象
            using (FileStream fs = File.OpenRead(@"C:\Users\FOX\Desktop\新建文件夹\写入excel.xls"))
            {
                IWorkbook workbook = null;
                //这里需要根据文件名格式判断一下
                //HSSF只能读取xls的
                //XSSF只能读取xlsx格式的
                if (Path.GetExtension(fs.Name) == ".xls")
                {
                    workbook = new HSSFWorkbook(fs);
                }
                else if (Path.GetExtension(fs.Name) == ".xlsx")
                {
                    workbook = new XSSFWorkbook(fs);
                }
                //因为Excel表中可能不止一个工作表,这里为了演示,我们遍历所有工作表
                for (int i = 0; i < workbook.NumberOfSheets; i++)
                {
                    //得到当前sheet
                    ISheet sheet = workbook.GetSheetAt(i);
                    //也可以通过GetSheet(name)得到
                    //遍历表中所有的行
                    //注意这里加1,这里得到的最后一个单元格的索引默认是从0开始的
                    for (int j = 0; j < sheet.LastRowNum + 1; j++)
                    {
                        //得到当前的行
                        IRow row = sheet.GetRow(j);
                        //遍历每行所有的单元格
                        //注意这里不用加1,这里得到的最后一个单元格的索引默认是从1开始的
                        for (int k = 0; k < row.LastCellNum; k++)
                        {
                            //得到当前单元格
                            ICell cell = row.GetCell(k, MissingCellPolicy.CREATE_NULL_AS_BLANK);
                          
                            Console.Write(cell.StringCellValue + " ");
                        }
                        Console.WriteLine();
                    }
                }
            }
        }
        
    }
    
    class Student
    {
        public string Name { get; set; }
        public string Sex { get; set; }
        public int Age { get; set; }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_39072819/article/details/100804066
今日推荐