NPIO操作Excel入门

2019-12-03 15:33:44 浏览数 (1)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/CJB_King/article/details/53117942

代码语言:javascript复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using System.IO;

namespace _02使用NOPI操作excel
{
    class Program
    {
        static void Main(string[] args)
        {
            //1、读取EXCEL到Filestream
            using (FileStream fs = File.OpenRead("1.xls"))
            {
                //2、根据文件流fs创建一个Workbook
                using (Workbook wk = new HSSFWorkbook(fs))
                {
                    //3、循环获取工作表中的个数 wk.NumberOfSheets
                    for (int i = 0; i < wk.NumberOfSheets; i  )
                    {
                        //4、循环获取每个工作表
                        using (Sheet sheet = wk.GetSheetAt(i))
                        {
                           Console.WriteLine("===================={0}===============", sheet.SheetName);
                            //5、循环获取每一行sheet.LastRowNum获取最后一行的索引
                            for (int r = 0; r <= sheet.LastRowNum; r  )
                            {
                               
                                Row row = sheet.GetRow(r);
                                //6、获取行中的单元格
                                for (int c = 0; c < row.LastCellNum; c  )
                                {
                                     Cell cell = row.GetCell(c);
                                     Console.Write(cell.ToString() "t");
                                 }
                                 Console.WriteLine();
                            }
                        }
                    }
                }
            }
            Console.ReadKey();
        }
    }
}

以上是读取Excel表格中数据的代码,仅供参考;

代码语言:javascript复制
 XWPFDocument doc = new XWPFDocument();
            XWPFParagraph p1 = doc.CreateParagraph();
            XWPFRun x = p1.CreateRun();
            using (FileStream fsImg = new FileStream("E:/1.jpg", FileMode.Open, FileAccess.Read))
            {
                x.AddPicture(fsImg, (int)PictureType.PNG, "asdsad", (int)(400.0 * 9525), (int)(300.0 * 9525));
            }
 
            using (FileStream fs = new FileStream("1.docx", FileMode.Create))
            {
                doc.Write(fs);
            }

向word中插入图片;

0 人点赞