[CSharpTips]C# NPOI简单读写Excel

C# NPOI简单读写Excel

安装Nuget包

Demo

using System;
using System.IO;
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.HSSF.Util;

namespace NPOI_Test
{
    class Program
    {

        static void Main(string[] args)
        {
            /*************************************************************
            IWorkbook :
                         该接口用于操作excel工作簿的一个接口, 主要有两个实现
                             HSSFWorkbook: 用于读取excel2007版本以下的xls文件
                            XSSFWorkbook : 用于读取.xlsx 文件
                        主要方法:
                            CreateSheet()  : 创建一个 工作表
                            GetSheetAt()   : 根据索引或名称获取工作表对象
            ISheet :
                        excel工作表对象
                        主要属性:
                            LastRowNum : 最后一行的索引
                            FirstRowNum: 第一行的索引
                        主要方法:
                            GetRow(int index) : 根据索引获取一行
                            CreateRow(int index) : 创建一个数据行
            IRow :
                        数据行对象
                        主要方法
                            CreateCell(int index) : 指定索引创建一个单元格
                            GetCell(int index) : 获取单元格
            ICell:
                        单元格对象
                        主要方法
                            SetCellValue(string value) : 设置单元格的值
                            ToString()  : 获取该单元格填充的内容
            **************************************************************/

            string filePath = Environment.CurrentDirectory + @"\Test.xlsx";

            /******************** 创建、写入、保存************************/
            //创建工作簿.xlsx
            IWorkbook workbook = new XSSFWorkbook();
            //创建工作表
            ISheet sheet = workbook.CreateSheet("sheet1");
            //设置列宽
            sheet.SetColumnWidth(0, 256 * 20);
            //创建行
            IRow row = sheet.CreateRow(0);
       //设置行高
       row.Height = 256*2 ;
//在行上创建单元格 ICell cell = row.CreateCell(0); //单元格赋值 cell.SetCellValue("HELLO,NPOI!"); //设置单元格样式 ICellStyle cellstyle = workbook.CreateCellStyle(); //水平居中 cellstyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; //垂直居中 cellstyle.VerticalAlignment = VerticalAlignment.Center; //自动换行 False cellstyle.WrapText = false; //设置字体 IFont font = workbook.CreateFont(); //字体大小 font.FontHeightInPoints = 12; //字体 font.FontName = "宋体";
       //加粗
       font.IsBold = True;
       //设置字体
            cellstyle.SetFont(font);
//设置颜色 //使用NPOI已经有的颜色创建(这里跟xls一样,不知道为什么可以通用) cellstyle.FillForegroundColor = HSSFColor.BrightGreen.Index; cellstyle.FillPattern = FillPattern.SolidForeground; //设置边框样式 cellstyle.BorderBottom = BorderStyle.Thin; cellstyle.BorderRight = BorderStyle.Thin; cellstyle.BorderTop = BorderStyle.Thin; cellstyle.BorderLeft = BorderStyle.Thin; //把定义好的cellstyle赋值给单元格 row.GetCell(0).CellStyle = cellstyle; //合并单元格 row.CreateCell(1); row.CreateCell(2); sheet.AddMergedRegion(new CellRangeAddress(0, 0, 1, 2)); row.GetCell(1).SetCellValue("MERGE CELLS"); //转换为字节流 MemoryStream stream = new MemoryStream(); workbook.Write(stream); var buf = stream.ToArray(); //保存为Excel文件 using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { fs.Write(buf, 0, buf.Length); fs.Flush(); } /************************* 读取***************************/ IWorkbook workbook1 = new XSSFWorkbook(filePath); //创建工作表 ISheet sheet1 = workbook1.GetSheet("sheet1"); //读取单元格的值 ICell cell1 = sheet1.GetRow(0).GetCell(0); Console.WriteLine(cell1.Address.ToString() + " value = " + cell1.ToString()); ICell cell2 = sheet1.GetRow(0).GetCell(1); Console.WriteLine(cell2.Address.ToString() + " value = " + cell2.ToString()); Console.ReadLine(); } } }

运行结果

 

 

 

 

 

 

posted @ 2022-07-04 14:47  xiaoshuye  阅读(340)  评论(0编辑  收藏  举报