NPOI的基礎操作

1、創建Excel

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (FileStream fs = new FileStream(@"C:\Users\dongshenfeng\Desktop\test.xlsx", FileMode.Create))
            {
                NPOI.SS.UserModel.IWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
                NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet("測試表");
                NPOI.SS.UserModel.IRow row = null;

                // 寫入字段名
                row = sheet.CreateRow(0);
                row.CreateCell(0).SetCellValue("姓名");
                row.CreateCell(1).SetCellValue("性別");
                row.CreateCell(2).SetCellValue("年齡");

                // 寫入字段值
                row = sheet.CreateRow(1);
                row.CreateCell(0).SetCellValue("張三");
                row.CreateCell(1).SetCellValue("男");
                row.CreateCell(2).SetCellValue("22");

                row = sheet.CreateRow(2);
                row.CreateCell(0).SetCellValue("李四");
                row.CreateCell(1).SetCellValue("女");
                row.CreateCell(2).SetCellValue("23");

                row = sheet.CreateRow(3);
                row.CreateCell(0).SetCellValue("王五");
                row.CreateCell(1).SetCellValue("男");
                row.CreateCell(2).SetCellValue("26");

                // 寫入文件流
                workbook.Write(fs);
                workbook.Close();
            }
        }
    }
}

在這裏插入圖片描述

2、設置列寬和行高

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (FileStream fs = new FileStream(@"C:\Users\dongshenfeng\Desktop\test.xlsx", FileMode.Create))
            {
                NPOI.SS.UserModel.IWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
                NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet("測試表");
                NPOI.SS.UserModel.IRow row = null;
                NPOI.SS.UserModel.ICell cell = null;

                // 設置列寬
                sheet.SetColumnWidth(0, 20 * 256);
                sheet.SetColumnWidth(1, 30 * 256);
                sheet.SetColumnWidth(2, 40 * 256);

                // 設置行高
                row = sheet.CreateRow(0);
                row.Height = 800;

                // 寫入字段名
                cell = row.CreateCell(0);
                cell.SetCellValue("姓名");
                cell = row.CreateCell(1);
                cell.SetCellValue("性別");
                cell = row.CreateCell(2);
                cell.SetCellValue("年齡");

                // 設置行高
                row = sheet.CreateRow(1);
                row.Height = 1600;

                // 寫入字段值
                cell = row.CreateCell(0);
                cell.SetCellValue("張三");
                cell = row.CreateCell(1);
                cell.SetCellValue("男");
                cell = row.CreateCell(2);
                cell.SetCellValue("22");

                // 寫入文件流
                workbook.Write(fs);
                workbook.Close();
            }
        }
    }
}

在這裏插入圖片描述

3、單元格居中

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (FileStream fs = new FileStream(@"C:\Users\dongshenfeng\Desktop\test.xlsx", FileMode.Create))
            {
                NPOI.SS.UserModel.IWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
                NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet("測試表");
                NPOI.SS.UserModel.IRow row = null;
                NPOI.SS.UserModel.ICell cell = null;

                // 居中樣式
                NPOI.SS.UserModel.ICellStyle cellStyle = workbook.CreateCellStyle();
                cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
                cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;

                // 寫入字段名
                row = sheet.CreateRow(0);
                cell = row.CreateCell(0);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("姓名");

                cell = row.CreateCell(1);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("性別");

                cell = row.CreateCell(2);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("年齡");

                // 寫入字段值
                row = sheet.CreateRow(1);
                cell = row.CreateCell(0);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("張三");

                cell = row.CreateCell(1);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("男");

                cell = row.CreateCell(2);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("22");

                // 寫入文件流
                workbook.Write(fs);
                workbook.Close();
            }
        }
    }
}

在這裏插入圖片描述

4、設置字體

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (FileStream fs = new FileStream(@"C:\Users\dongshenfeng\Desktop\test.xlsx", FileMode.Create))
            {
                NPOI.SS.UserModel.IWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
                NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet("測試表");
                NPOI.SS.UserModel.IRow row = null;
                NPOI.SS.UserModel.ICell cell = null;

                // 居中樣式
                NPOI.SS.UserModel.ICellStyle cellStyle = workbook.CreateCellStyle();
                cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
                cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;

                // 設置字體
                NPOI.SS.UserModel.IFont font = workbook.CreateFont();
                font.FontHeightInPoints = 20;
                font.FontName = "楷體";
                font.Underline = NPOI.SS.UserModel.FontUnderlineType.DoubleAccounting;
                font.IsBold = true;
                font.IsItalic = true;
                cellStyle.SetFont(font);

                // 寫入字段名
                row = sheet.CreateRow(0);
                cell = row.CreateCell(0);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("姓名");

                cell = row.CreateCell(1);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("性別");

                cell = row.CreateCell(2);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("年齡");

                // 寫入字段值
                row = sheet.CreateRow(1);
                cell = row.CreateCell(0);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("張三");

                cell = row.CreateCell(1);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("男");

                cell = row.CreateCell(2);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("22");

                // 寫入文件流
                workbook.Write(fs);
                workbook.Close();
            }
        }
    }
}

在這裏插入圖片描述

5、設置表格邊框

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (FileStream fs = new FileStream(@"C:\Users\dongshenfeng\Desktop\test.xlsx", FileMode.Create))
            {
                NPOI.SS.UserModel.IWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
                NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet("測試表");
                NPOI.SS.UserModel.IRow row = null;
                NPOI.SS.UserModel.ICell cell = null;

                // 邊框樣式
                NPOI.SS.UserModel.ICellStyle cellStyle = workbook.CreateCellStyle();
                cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
                cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
                cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;

                // 寫入字段名
                row = sheet.CreateRow(0);
                cell = row.CreateCell(0);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("姓名");

                cell = row.CreateCell(1);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("性別");

                cell = row.CreateCell(2);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("年齡");

                // 寫入字段值
                row = sheet.CreateRow(1);
                cell = row.CreateCell(0);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("張三");

                cell = row.CreateCell(1);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("男");

                cell = row.CreateCell(2);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("22");

                // 寫入文件流
                workbook.Write(fs);
                workbook.Close();
            }
        }
    }
}

在這裏插入圖片描述

6、設置單元格背景色

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (FileStream fs = new FileStream(@"C:\Users\dongshenfeng\Desktop\test.xlsx", FileMode.Create))
            {
                NPOI.SS.UserModel.IWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
                NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet("測試表");
                NPOI.SS.UserModel.IRow row = null;
                NPOI.SS.UserModel.ICell cell = null;

                // 背景色
                NPOI.SS.UserModel.ICellStyle cellStyle = workbook.CreateCellStyle();
                cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
                cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
                cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                cellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index;
                cellStyle.FillPattern = NPOI.SS.UserModel.FillPattern.LessDots;

                // 寫入字段名
                row = sheet.CreateRow(0);
                cell = row.CreateCell(0);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("姓名");

                cell = row.CreateCell(1);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("性別");

                cell = row.CreateCell(2);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("年齡");

                // 寫入字段值
                row = sheet.CreateRow(1);
                cell = row.CreateCell(0);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("張三");

                cell = row.CreateCell(1);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("男");

                cell = row.CreateCell(2);
                cell.CellStyle = cellStyle;
                cell.SetCellValue("22");

                // 寫入文件流
                workbook.Write(fs);
                workbook.Close();
            }
        }
    }
}

在這裏插入圖片描述

7、讀取Excel

讀取Excel需要注意,有時候表格的起始位置並不是第一行第一列,如下圖所示:
在這裏插入圖片描述
讀取方法如下:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (FileStream fs = new FileStream(@"C:\Users\dongshenfeng\Desktop\test.xlsx", FileMode.Open))
            {
                NPOI.SS.UserModel.IWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(fs);
                NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(0);
                for (int i = sheet.FirstRowNum; i <= sheet.LastRowNum; i++)
                {
                    NPOI.SS.UserModel.IRow row = sheet.GetRow(i);
                    for (int j = row.FirstCellNum; j < row.LastCellNum; j++)
                    {
                        Console.Write(row.GetCell(j).ToString() + "\t");
                    }
                    Console.WriteLine();
                }
            }
        }
    }
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章