利用NPOI写入Excel

引用NPOI.Dll文件

应用命名空间

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

创建基础数据

public class Student
    {
        public int Age { get; set; }
        public string Name { get; set; }
        public Student(string name,int age)
        {
            this.Name = name;
            this.Age = age;
        }
    }
            List<Student> stus = new List<Student>(){new Student("小明",10),new Student("小刚",13)};
            IWorkbook wk = new HSSFWorkbook();
            ISheet sheet = wk.CreateSheet("s1");
            for (int i = 0; i < stus.Count; i++)
            {
                IRow row = sheet.CreateRow(i);
                row.CreateCell(0).SetCellValue(stus[i].Name);
                row.CreateCell(1).SetCellValue(stus[i].Age);
            }
            using (FileStream fsWrite=File.OpenWrite(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),"1.xls")))
            { 
                wk.Write(fsWrite);
            }
            //打开文件
            Process.Start(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "1.xls"));

猜你喜欢

转载自blog.csdn.net/weixin_42479664/article/details/83056249
今日推荐