C# Excel

1.C#通过NPOI对Excel操作:

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 通过NPOI进行对Excel操作
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //
        }
        // 写Excel
        private void button1_Click(object sender, EventArgs e)
        {
            // 创建list集合
            List<Person> list1 = new List<Person>()
            {
                new Person() {name="n",age=12,email="[email protected]" },
                new Person() {name="a",age=11,email="[email protected]" },
                new Person() {name="m",age=10,email="[email protected]" },
                new Person() {name="e",age=9,email="[email protected]" },
                new Person() {name="j",age=8,email="[email protected]" },
                new Person() {name="r",age=7,email="[email protected]" }
            };
            // 创建工作薄对象
            IWorkbook wkbook = new HSSFWorkbook();
            // 在该工作薄中创建表对象
            ISheet sheet = wkbook.CreateSheet("List for Person");
            // 想对象中插入行和列的信息
            for(int i = 0; i < list1.Count; i++)
            {
                // 创建行信息
                IRow row = sheet.CreateRow(i);  // 创建第几行
                // 创建列(填写列信息)
                row.CreateCell(0).SetCellValue(list1[i].name);  // 给第0列设置信息
                row.CreateCell(1).SetCellValue(list1[i].age);  // 给第1列设置信息
                row.CreateCell(2).SetCellValue(list1[i].email);  // 给第2列设置信息
                // 上面这三行等价于下面这种写法
                /*
                ICell cell = row.CreateCell(0);
                cell.SetCellValue(list1[i].name);
                */
            }
            // 写到Excel
            using(FileStream fswrite = File.OpenWrite("list.xls"))
            {
                wkbook.Write(fswrite);  //工作薄写入的对象是文件流
            }
            MessageBox.Show("写入成功!");
        }
        // 读Excel
        private void button2_Click(object sender, EventArgs e)
        {
            // 获取Excel中的内容
            using(FileStream fsread = File.OpenRead("list.xls"))
            {
                // 将读取到的文件流的内容放到工作薄中
                IWorkbook wkbook = new HSSFWorkbook(fsread);
                // 获取每个工作表Sheet
                for(int i = 0; i < wkbook.NumberOfSheets; i++)
                {
                    // 获取每个工作表sheet
                    ISheet sheet = wkbook.GetSheetAt(i);
                    Console.WriteLine("============================================={0}=============================================", sheet.SheetName);  // 打印书工作表的名称
                    // 遍历每个工作表中的行(LastRowNum最后一行的数值)
                    for (int j = 0; j <= sheet.LastRowNum; j++)
                    {
                        // 获取对应行
                        IRow row = sheet.GetRow(j);
                        // 这个LastCellNum不需要等于
                        for (int k = 0; k < row.LastCellNum; k++)
                        {
                            // 获取每个单元格的内容
                            ICell cell = row.GetCell(k);
                            string value = cell.ToString(); // 不区分类型,都以字符串进行输出
                            /*
                            string valueS = cell.StringCellValue;  // 获取string类型
                            bool valueB = cell.BooleanCellValue;  // 获取bool类型
                            // 等等还有其他类型
                            */
                            Console.Write("{0}   |     ", value);
                        }
                        Console.WriteLine();
                    }
                }
            }
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/namejr/p/11093633.html
今日推荐