• NPOI读取Excel,导入数据到Excel练习01


    NPOI 2.2.0.0,初级读取导入Excel

    1.读取Excel,将数据绑定到dgv上

     1         private void button1_Click(object sender, EventArgs e)
     2         {
     3             List<Book> books = new List<Book>();
     4             //1.读取Excel文件
     5             using (FileStream fsReder = File.OpenRead("练习.xlsx"))
     6             {
     7                 //2.创建工作簿
     8                 IWorkbook wk = new XSSFWorkbook("练习.xlsx");
     9                 //IWorkbook wk=new XSSFWorkbook(fsReder);
    10                 //获取第一个工作表
    11                 ISheet sheet = wk.GetSheetAt(0);
    12                 //3.遍历每一行,行和列的索引都是从0开始
    13                 for (int i = 3; i < sheet.LastRowNum; i++)
    14                 {
    15                     IRow row = sheet.GetRow(i);
    16                     //MessageBox.Show(row.GetCell(0).ToString());
    17                     //4.读取每一行的数据,加到对象中
    18                     books.Add(new Book()
    19                     {
    20                         Name = row.GetCell(0).ToString(),
    21                         One = row.GetCell(1).ToString(),
    22                         Two = row.GetCell(2).ToString(),
    23                         Three = row.GetCell(3).ToString(),
    24                         Four = row.GetCell(4).ToString(),
    25                         Five = row.GetCell(5).ToString(),
    26                         Six = row.GetCell(6).ToString(),
    27 
    28                     });
    29                 }
    30             }
    31             //5.绑定数据
    32             dgv.DataSource = books;
    33             dgv.AutoGenerateColumns = false;
    34             dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    35             MessageBox.Show("操作完成");
    36         }
    View Code

      2.将数据导入到Excel中

     1         private void button2_Click(object sender, EventArgs e)
     2         {
     3             List<Book> books = new List<Book>()
     4             {
     5                 new Book() {Name = "火影忍者",One  ="34",Two = "56",Three = "67",Four = "78",Five = "99",Six = "54"},
     6                 new Book() {Name = "海贼王",  One ="64",Two = "79",Three = "97",Four = "74",Five = "29",Six = "56"},
     7                 new Book() {Name = "死神",One  ="94",Two = "51",Three = "57",Four = "64",Five = "90",Six = "74"},
     8                 new Book() {Name = "游戏王",One  ="94",Two = "30",Three = "56",Four = "45",Five = "58",Six = "46"},
     9             };
    10             //1.创建工作簿
    11             using (FileStream fsWrite = File.OpenWrite("1.xlsx"))
    12             {
    13                 IWorkbook wk = new XSSFWorkbook();
    14                 //2.创建第一个工作表
    15                 ISheet sheet = wk.CreateSheet("第一张表");
    16                 //第一行设置标题
    17                 IRow rowHead = sheet.CreateRow(0);
    18 
    19                 rowHead.CreateCell(0).SetCellValue("图书名");                
    20                 rowHead.CreateCell(1).SetCellValue("一月");              
    21                 rowHead.CreateCell(2).SetCellValue("二月");           
    22                 rowHead.CreateCell(3).SetCellValue("三月");
    23                 rowHead.CreateCell(4).SetCellValue("四月");
    24                 rowHead.CreateCell(5).SetCellValue("五月");
    25                 rowHead.CreateCell(6).SetCellValue("六月");
    26 
    27                 //3.在工作表的最后一行下,循环创建工作表的每一行,添加数据
    28                 for (int i = 1; i < books.Count; i++)
    29                 {
    30                     IRow row = sheet.CreateRow(i);
    31                     row.CreateCell(0).SetCellValue(books[i].Name);
    32                     row.CreateCell(1).SetCellValue(books[i].One);
    33                     row.CreateCell(2).SetCellValue(books[i].Two);
    34                     row.CreateCell(3).SetCellValue(books[i].Three);
    35                     row.CreateCell(4).SetCellValue(books[i].Four);
    36                     row.CreateCell(5).SetCellValue(books[i].Five);
    37                     row.CreateCell(6).SetCellValue(books[i].Six);
    38                 }
    39                 wk.Write(fsWrite);
    40             }
    41 
    42             MessageBox.Show("操作完成");
    43         }
    View Code

  • 相关阅读:
    问题
    cas restful接口实现SSO
    变量,数据类型
    注释
    下载,配置环境变量,第一个demo
    数据挖掘概念与技术(韩家伟)阅读笔记1
    pattern与matcherr
    Array.sort排序
    linux下C语言中的flock函数用法 【转】
    我为什么要进国企----HP大中华区总裁孙振耀退休感言
  • 原文地址:https://www.cnblogs.com/CSharpLover/p/5193655.html
Copyright © 2020-2023  润新知