码迷,mamicode.com
首页 > 其他好文 > 详细

NPOI读写Excel表格、Word文档

时间:2015-12-06 01:37:48      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

NPOI是从POI移植过来的.NET版本,专门对Word、Excel进行读写操作的一个开源项目

下面就试着怎么用我们的C#来进行读写操作,我现在用的Office版本是2016

我们首先通过Nuget把NPOI引入到我们的项目中

1、读取Excel表格

using(FileStream fs=new FileStream(@"C:\Users\BIDIANQING\Desktop\1.xlsx", FileMode.Open,FileAccess.Read))
            {
                XSSFWorkbook book = new XSSFWorkbook(fs);
                ISheet sheet= book.GetSheetAt(0);
                string sheetName= sheet.SheetName;
                int lastRowNum = sheet.LastRowNum;
                
                for (int i = 0; i <= lastRowNum; i++)
                {
                    string str = string.Empty;
                    IRow row= sheet.GetRow(i);
                    int lastCellNum = row.LastCellNum;
                    for (int j = 0; j < lastCellNum; j++)
                    {
                        ICell cell= row.GetCell(j);
                        CellType cellType= cell.CellType;
                        if (cellType == CellType.Numeric)
                        {
                            str += cell.NumericCellValue.ToString()+"|";
                        }
                        else if (cellType == CellType.String)
                        {
                            str += cell.StringCellValue+"|";
                        }
                    }
                    Console.WriteLine(str);
                }
                
            }

 

2、把数据写入Excel表格

List<Person> list = new List<Person>();
            list.Add(new Person() { Id = 1, Name = "张三" });
            list.Add(new Person() { Id = 2, Name = "李四" });
            list.Add(new Person() { Id = 3, Name = "小王" });
            list.Add(new Person() { Id = 4, Name = "小鸣" });

            using (FileStream fs = new FileStream(@"C:\Users\BIDIANQING\Desktop\2.xlsx", FileMode.Create, FileAccess.Write))
            {
                XSSFWorkbook book = new XSSFWorkbook();
                ISheet sheet= book.CreateSheet("person");
                int n=0;
                foreach (var item in list)
                {
                    IRow row= sheet.CreateRow(n);
                    row.CreateCell(0, CellType.Numeric).SetCellValue(item.Id);
                    row.CreateCell(1, CellType.String).SetCellValue(item.Name);
                    n++;
                }
                book.Write(fs);
            }
            Console.WriteLine("OK");
            Console.ReadKey();

 

3、读取Word文档

 using(FileStream fs=new FileStream(@"C:\Users\BIDIANQING\Desktop\1.docx", FileMode.Open,FileAccess.Read))
            {
                XWPFDocument doc = new XWPFDocument(fs);
                //var package= doc.Package;
                var paragraphs= doc.Paragraphs;
                foreach (var item in paragraphs)
                {
                    Console.WriteLine(item.ParagraphText);
                }
            }
            Console.ReadKey();

 

4、把数据写入Word文档

NPOI读写Excel表格、Word文档

标签:

原文地址:http://www.cnblogs.com/bidianqing/p/5022650.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!