C# NPOI操作Excel汇总
C#操作Excel有多种方法,如通过数据库的方式来读写Excel的OleDb方式,但是OleDb方式需要安装微软office,还可以通过COM组件方式操作Excel,也需要安装微软Excel。如果不想安装微软办公套餐可以使用ClosedXML、EPPlus、NPOI。本文主要是介绍NPOI的常用使用方法。
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
NuGet安装NPOI
NPO依赖beta版本的SixLabors.Fonts,需要先安装SixLabors.Fonts。
然后安装NPOI:
主要对象
对象 | 对象说明 |
---|---|
HSSFWorkbook | excel文档对象 工作簿 .xls文件 |
XSSFWorkbook | excel文档对象 工作簿 .xlsx文件 |
HSSFSheet | excel的sheet 工作表 |
HSSFRow | excel的行 |
HSSFName | 名称 |
HSSFDateFomat | 日期格式 |
HSSFHeader | sheet头 |
HSSFFooter | sheet尾 |
HSSFCellStyle | cell样式 |
HSSFDateUtil | 日期 |
HSSFPrintSetup | 打印 |
HSSFErrorConstants | 错误信息表 |
HSSF是Horrible SpreadSheet Format的缩写,通过HSSF,你可以使用纯Java代码来读取、写入、修改Microsoft Excel BIFF(Excel 97-2003, xls)文件。HSSF为读取操作提供了两类API:usermodel
和eventusermodel
,即"用户模型"和"用户事件模型"。
读写Excel
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace WorkbookFactoryDemo
{class Program{static void Main(string[] args){using (var stream= File.OpenRead("test.xlsx")){IWorkbook workbook = WorkbookFactory.Create(stream);//workbook.GetSheetAt(0).CopySheet("Folha1");/*for (int i = 0; i < workbook.NumberOfSheets; i++){ISheet sheet = workbook.GetSheetAt(i);Console.WriteLine(sheet.SheetName);}*/using (FileStream fileWriter = new FileStream("TestOutput.xlsx", FileMode.Create, FileAccess.ReadWrite, FileShare.None)){workbook.Write(fileWriter, false);}Console.ReadLine();}}}
}
遍历Excel所有单元格
using System;
using NPOI.SS.UserModel;namespace ReadAndPrintData
{class Program{static void Main(string[] args){Console.OutputEncoding = System.Text.Encoding.UTF8;using (var workbook = WorkbookFactory.Create("data.xlsx")){ISheet sheet = workbook.GetSheetAt(0);foreach (IRow row in sheet){for (var i = 0; i < row.LastCellNum; i++){var cell = row.GetCell(i);if (cell != null){Console.Write(cell.ToString());Console.Write("\t");}}Console.WriteLine();}}Console.Read();}}
}
设置文件属性
using NPOI;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.IO;namespace CreateCustomProperties
{class Program{static void Main(string[] args){XSSFWorkbook workbook = new XSSFWorkbook();ISheet sheet1 = workbook.CreateSheet("Sheet1");POIXMLProperties props = workbook.GetProperties();props.CoreProperties.Creator = "NPOI 2.5.1";props.CoreProperties.Created = DateTime.Now;if (!props.CustomProperties.Contains("NPOI Team"))props.CustomProperties.AddProperty("NPOI Team", "Hello World!");FileStream sw = File.Create("test.xlsx");workbook.Write(sw);sw.Close();}}
}
设置单元格值:SetCellValue
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;namespace NPOI.Examples.XSSF.SetCellValuesInXlsx
{class Program{static void Main(string[] args){IWorkbook workbook = new XSSFWorkbook();ISheet sheet1 = workbook.CreateSheet("Sheet1");sheet1.CreateRow(0).CreateCell(0).SetCellValue("This is a Sample");int x = 1;for (int i = 1; i <= 15; i++){IRow row = sheet1.CreateRow(i);for (int j = 0; j < 15; j++){row.CreateCell(j).SetCellValue(x++);}}FileStream sw = File.Create("test.xlsx");workbook.Write(sw);sw.Close();}}
}
填充日期
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;using (IWorkbook workbook = new XSSFWorkbook())
{ISheet sheet = workbook.CreateSheet("Sheet1");//increase the width of Column Asheet.SetColumnWidth(0, 5000);//create the format instanceIDataFormat format = workbook.CreateDataFormat();//Chinese date stringICell cell7 = sheet.CreateRow(6).CreateCell(0);SetValueAndFormat(workbook, cell7, new DateOnly(2004, 5, 6), format.GetFormat("yyyy年m月d日"));using (FileStream sw = File.Create("test.xlsx")){workbook.Write(sw, false);}
}static void SetValueAndFormat(IWorkbook workbook, ICell cell, DateOnly value, short formatId)
{//set value for the cellcell.SetCellValue(value);ICellStyle cellStyle = workbook.CreateCellStyle();cellStyle.DataFormat = formatId;cell.CellStyle = cellStyle;
}
设置背景色
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;namespace NPOI.Examples.XSSF.FillBackgroundInXlsx
{class Program{static void Main(string[] args){using (IWorkbook workbook = new XSSFWorkbook()){ISheet sheet1 = workbook.CreateSheet("Sheet1");//fill backgroundICellStyle style1 = workbook.CreateCellStyle();style1.FillForegroundColor = IndexedColors.Blue.Index;style1.FillPattern = FillPattern.BigSpots;style1.FillBackgroundColor = IndexedColors.Pink.Index;sheet1.CreateRow(0).CreateCell(0).CellStyle = style1;//fill backgroundICellStyle style2 = workbook.CreateCellStyle();style2.FillForegroundColor = IndexedColors.Yellow.Index;style2.FillPattern = FillPattern.AltBars;style2.FillBackgroundColor = IndexedColors.Rose.Index;sheet1.CreateRow(1).CreateCell(0).CellStyle = style2;//fill backgroundICellStyle style3 = workbook.CreateCellStyle();style3.FillForegroundColor = IndexedColors.Lime.Index;style3.FillPattern = FillPattern.LessDots;style3.FillBackgroundColor = IndexedColors.LightGreen.Index;sheet1.CreateRow(2).CreateCell(0).CellStyle = style3;//fill backgroundICellStyle style4 = workbook.CreateCellStyle();style4.FillForegroundColor = IndexedColors.Yellow.Index;style4.FillPattern = FillPattern.LeastDots;style4.FillBackgroundColor = IndexedColors.Rose.Index;sheet1.CreateRow(3).CreateCell(0).CellStyle = style4;//fill backgroundICellStyle style5 = workbook.CreateCellStyle();style5.FillForegroundColor = IndexedColors.LightBlue.Index;style5.FillPattern = FillPattern.Bricks;style5.FillBackgroundColor = IndexedColors.Plum.Index;sheet1.CreateRow(4).CreateCell(0).CellStyle = style5;//fill backgroundICellStyle style6 = workbook.CreateCellStyle();style6.FillForegroundColor = IndexedColors.SeaGreen.Index;style6.FillPattern = FillPattern.FineDots;style6.FillBackgroundColor = IndexedColors.White.Index;sheet1.CreateRow(5).CreateCell(0).CellStyle = style6;//fill backgroundICellStyle style7 = workbook.CreateCellStyle();style7.FillForegroundColor = IndexedColors.Orange.Index;style7.FillPattern = FillPattern.Diamonds;style7.FillBackgroundColor = IndexedColors.Orchid.Index;sheet1.CreateRow(6).CreateCell(0).CellStyle = style7;//fill backgroundICellStyle style8 = workbook.CreateCellStyle();style8.FillForegroundColor = IndexedColors.White.Index;style8.FillPattern = FillPattern.Squares;style8.FillBackgroundColor = IndexedColors.Red.Index;sheet1.CreateRow(7).CreateCell(0).CellStyle = style8;//fill backgroundICellStyle style9 = workbook.CreateCellStyle();style9.FillForegroundColor = IndexedColors.RoyalBlue.Index;style9.FillPattern = FillPattern.SparseDots;style9.FillBackgroundColor = IndexedColors.Yellow.Index;sheet1.CreateRow(8).CreateCell(0).CellStyle = style9;//fill backgroundICellStyle style10 = workbook.CreateCellStyle();style10.FillForegroundColor = IndexedColors.RoyalBlue.Index;style10.FillPattern = FillPattern.ThinBackwardDiagonals;style10.FillBackgroundColor = IndexedColors.Yellow.Index;sheet1.CreateRow(9).CreateCell(0).CellStyle = style10;//fill backgroundICellStyle style11 = workbook.CreateCellStyle();style11.FillForegroundColor = IndexedColors.RoyalBlue.Index;style11.FillPattern = FillPattern.ThickForwardDiagonals;style11.FillBackgroundColor = IndexedColors.Yellow.Index;sheet1.CreateRow(10).CreateCell(0).CellStyle = style11;//fill backgroundICellStyle style12 = workbook.CreateCellStyle();style12.FillForegroundColor = IndexedColors.RoyalBlue.Index;style12.FillPattern = FillPattern.ThickHorizontalBands;style12.FillBackgroundColor = IndexedColors.Yellow.Index;sheet1.CreateRow(11).CreateCell(0).CellStyle = style12;//fill backgroundICellStyle style13 = workbook.CreateCellStyle();style13.FillForegroundColor = IndexedColors.RoyalBlue.Index;style13.FillPattern = FillPattern.ThickVerticalBands;style13.FillBackgroundColor = IndexedColors.Yellow.Index;sheet1.CreateRow(12).CreateCell(0).CellStyle = style13;//fill backgroundICellStyle style14 = workbook.CreateCellStyle();style14.FillForegroundColor = IndexedColors.RoyalBlue.Index;style14.FillPattern = FillPattern.ThickBackwardDiagonals;style14.FillBackgroundColor = IndexedColors.Yellow.Index;sheet1.CreateRow(13).CreateCell(0).CellStyle = style14;//fill backgroundICellStyle style15 = workbook.CreateCellStyle();style15.FillForegroundColor = IndexedColors.RoyalBlue.Index;style15.FillPattern = FillPattern.ThinForwardDiagonals;style15.FillBackgroundColor = IndexedColors.Yellow.Index;sheet1.CreateRow(14).CreateCell(0).CellStyle = style15;//fill backgroundICellStyle style16 = workbook.CreateCellStyle();style16.FillForegroundColor = IndexedColors.RoyalBlue.Index;style16.FillPattern = FillPattern.ThinHorizontalBands;style16.FillBackgroundColor = IndexedColors.Yellow.Index;sheet1.CreateRow(15).CreateCell(0).CellStyle = style16;//fill backgroundICellStyle style17 = workbook.CreateCellStyle();style17.FillForegroundColor = IndexedColors.RoyalBlue.Index;style17.FillPattern = FillPattern.ThinVerticalBands;style17.FillBackgroundColor = IndexedColors.Yellow.Index;sheet1.CreateRow(16).CreateCell(0).CellStyle = style17;//fill background with gradient color, this only works with NPOI 2.6.0XSSFCellStyle style18 = (XSSFCellStyle)workbook.CreateCellStyle();style18.FillPattern = FillPattern.SolidForeground;var fillidx = (int)style18.GetCoreXf().fillId;var ctfill = ((XSSFWorkbook)workbook).GetStylesSource().GetFillAt(fillidx).GetCTFill();ctfill.UnsetPatternFill();byte[] rgb1 = new byte[3];rgb1[0] = (byte)0; // redrgb1[1] = (byte)0; // greenrgb1[2] = (byte)255; // bluebyte[] rgb2 = new byte[3];rgb2[0] = (byte)255; // redrgb2[1] = (byte)255; // greenrgb2[2] = (byte)255; // bluectfill.gradientFill = new OpenXmlFormats.Spreadsheet.CT_GradientFill();var ctgradientfill = ctfill.gradientFill;ctgradientfill.degree = 90;ctgradientfill.AddNewStop().position = 0;ctgradientfill.GetStopArray(0).AddNewColor().SetRgb(rgb1);ctgradientfill.AddNewStop().position = 0.5;ctgradientfill.GetStopArray(1).AddNewColor().SetRgb(rgb2);ctgradientfill.AddNewStop().position = 1.0;ctgradientfill.GetStopArray(2).AddNewColor().SetRgb(rgb1);sheet1.CreateRow(16).CreateCell(0).CellStyle = style18;using (FileStream sw = File.Create("test.xlsx")){workbook.Write(sw, false);}}}}
}
设置宽高
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;namespace NPOI.Examples.XSSF.SetWidthAndHeightInXlsx
{class Program{static void Main(string[] args){using (IWorkbook workbook = new XSSFWorkbook()){ISheet sheet1 = workbook.CreateSheet("Sheet1");//set the width of columnssheet1.SetColumnWidth(0, 50 * 256);sheet1.SetColumnWidth(1, 100 * 256);sheet1.SetColumnWidth(2, 150 * 256);//set the width of heightsheet1.CreateRow(0).Height = 100 * 20;sheet1.CreateRow(1).Height = 200 * 20;sheet1.CreateRow(2).Height = 300 * 20;using (FileStream sw = File.Create("test.xlsx")){workbook.Write(sw, false);}}}}
}
折线图
using NPOI.SS.UserModel;
using NPOI.SS.UserModel.Charts;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
using System.IO;namespace LineChart
{class Program{const int NUM_OF_ROWS = 3;const int NUM_OF_COLUMNS = 10;static void CreateChart(IDrawing drawing, ISheet sheet, IClientAnchor anchor, string serie1, string serie2, bool enableMajorGridline=false){XSSFChart chart = (XSSFChart)drawing.CreateChart(anchor);chart.SetTitle("Test 1");IChartLegend legend = chart.GetOrCreateLegend();legend.Position = LegendPosition.TopRight;ILineChartData<double, double> data = chart.ChartDataFactory.CreateLineChartData<double, double>();// Use a category axis for the bottom axis.IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);leftAxis.Crosses = AxisCrosses.AutoZero;IChartDataSource<double> xs = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));IChartDataSource<double> ys1 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));IChartDataSource<double> ys2 = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));var s1 = data.AddSeries(xs, ys1);s1.SetTitle(serie1);var s2 = data.AddSeries(xs, ys2);s2.SetTitle(serie2);chart.Plot(data, bottomAxis, leftAxis);//add major gridline, available since NPOI 2.5.5var plotArea = chart.GetCTChart().plotArea;plotArea.catAx[0].AddNewMajorGridlines();plotArea.valAx[0].AddNewMajorGridlines();}static void Main(string[] args){using (IWorkbook wb = new XSSFWorkbook()){ISheet sheet = wb.CreateSheet("linechart");// Create a row and put some cells in it. Rows are 0 based.IRow row;ICell cell;for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++){row = sheet.CreateRow((short)rowIndex);for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++){cell = row.CreateCell((short)colIndex);cell.SetCellValue(colIndex * (rowIndex + 1));}}IDrawing drawing = sheet.CreateDrawingPatriarch();IClientAnchor anchor1 = drawing.CreateAnchor(0, 0, 0, 0, 0, 5, 10, 15);CreateChart(drawing, sheet, anchor1, "title1", "title2");IClientAnchor anchor2 = drawing.CreateAnchor(0, 0, 0, 0, 0, 20, 10, 35);CreateChart(drawing, sheet, anchor2, "s1", "s2", true);using (FileStream fs = File.Create("test.xlsx")){wb.Write(fs, false);}}}}
}
条形图
using NPOI.SS.UserModel;
using NPOI.SS.UserModel.Charts;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
using System;
using System.IO;namespace BarChart
{class Program{const int NUM_OF_ROWS = 10;const int NUM_OF_COLUMNS = 2;private static void CreateChart(ISheet sheet, IDrawing drawing, IClientAnchor anchor, string serieTitle, int startDataRow, int endDataRow, int columnIndex){XSSFChart chart = (XSSFChart)drawing.CreateChart(anchor);IBarChartData<string, double> barChartData = chart.ChartDataFactory.CreateBarChartData<string, double>();IChartLegend legend = chart.GetOrCreateLegend();legend.Position = LegendPosition.Bottom;IChartAxis bottomAxis = chart.ChartAxisFactory.CreateCategoryAxis(AxisPosition.Bottom);bottomAxis.MajorTickMark = AxisTickMark.None;IValueAxis leftAxis = chart.ChartAxisFactory.CreateValueAxis(AxisPosition.Left);leftAxis.Crosses = AxisCrosses.AutoZero;leftAxis.SetCrossBetween(AxisCrossBetween.Between);IChartDataSource<string> categoryAxis = DataSources.FromStringCellRange(sheet, new CellRangeAddress(startDataRow, endDataRow, 0, 0));IChartDataSource<double> valueAxis = DataSources.FromNumericCellRange(sheet, new CellRangeAddress(startDataRow, endDataRow, columnIndex, columnIndex));var serie = barChartData.AddSeries(categoryAxis, valueAxis);serie.SetTitle(serieTitle);chart.Plot(barChartData, bottomAxis, leftAxis);}static void Main(string[] args){using (IWorkbook wb = new XSSFWorkbook()){ISheet sheet = wb.CreateSheet();// Create a row and put some cells in it. Rows are 0 based.IRow row;ICell cell;for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++){row = sheet.CreateRow((short)rowIndex);for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++){cell = row.CreateCell((short)colIndex);if (colIndex == 0)cell.SetCellValue("X" + rowIndex);else{var x = colIndex * (rowIndex + 1);cell.SetCellValue(x * x + 2 * x + 1);}}}XSSFDrawing drawing = (XSSFDrawing)sheet.CreateDrawingPatriarch();XSSFClientAnchor anchor = (XSSFClientAnchor)drawing.CreateAnchor(0, 0, 0, 0, 3, 3, 10, 12);CreateChart(sheet, drawing, anchor, "s1", 0, 9, 1);using (FileStream fs = File.Create("test.xlsx")){wb.Write(fs, false);}}}}
}
设置打印范围
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.IO;namespace SetPrintArea
{class Program{static IWorkbook workbook;static void Main(string[] args){InitializeWorkbook(args);ISheet sheet = workbook.CreateSheet("Timesheet");sheet.CreateRow(0).CreateCell(0).SetCellValue("Test");workbook.SetPrintArea(0, "$A$1:$C$5");//workbook.SetPrintArea(0, "$A$1:$C$5,$E$9:$I$16"); not working in xlsWriteToFile();}static void WriteToFile(){string filename = "timesheet.xls";if (workbook is XSSFWorkbook) filename += "x";//Write the stream data of workbook to the root directoryusing (FileStream file = new FileStream(filename, FileMode.Create)){workbook.Write(file);}}static void InitializeWorkbook(string[] args){if (args.Length > 0 && args[0].Equals("-xls"))workbook = new HSSFWorkbook();elseworkbook = new XSSFWorkbook();}}
}
参考
https://github.com/nissl-lab/npoi
https://github.com/nissl-lab/npoi-examples
https://blog.csdn.net/weixin_44899642/article/details/124687499
C# NPOI操作Excel汇总相关推荐
- NPOI 操作Excel学习总结
NPOI读写Excel http://www.cnblogs.com/luxiaoxun/p/3374992.html 1.整个Excel表格叫做工作表:WorkBook(工作薄),包含的叫页(工作表 ...
- npoi获取合并单元格_梦琪小生 C# 如何使用NPOI操作Excel以及读取合并单元格等
C#操作Excel方法有很多,以前用的需要电脑安装office才能用,但因为版权问题公司不允许安装office.所以改用NPOI进行Excel操作,基本上一些简单的Excel操作都没有问题,读写合并单 ...
- C# 使用NPOI 操作 Excel 文件
C# 使用NPOI 操作 Excel 文件 使用 创建 Excel 文件 创建 XLS 文件 创建 XLSX 文件 合并单元格 设置单元格格式 对齐 字体 边框 填充 保护 数字 下拉框 直接传递下拉 ...
- .NET/C#使用NPOI操作Excel
前言 Asp.net/C#操作Excel最惨的就是环境配置了:使用NPOI能够帮助开发者在没有安装微软Office的情况下读写Office 97-2003的文件,支持的文件格式包括xls, doc, ...
- .NET——NPOI操作excel
一.引言 C#操作excel的方法大概有这几种: 1.采用OleDB读取EXCEL文件,把EXCEL文件当做一个数据源来进行数据的读取操作 2.引用的com组件:Microsoft.Office.In ...
- hssfwork 导出excel 文件已损坏_C# NPOI 操作EXCEL文件的读取和导出
在实际项目中有很多场景遇到需要操作EXCEL文件,而常用到的库就有NPOI:NPOI是开源的POI 项目的.NET版,POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目, ...
- C#NPOI操作Excel
C#NPOI 一.NPOI的引用 二.操作Excel 1.创建新的Excel工作簿 2.读取现有的Excel工作簿 3.操作sheet工作表 4.操作workbook工作簿 5.跨workbook工作 ...
- NPOI 操作Excel
以下简介--来自百度百科 NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. 使用 NPOI 你就可以在没有安装 Offic ...
- DotNet操作Excel汇总
在VS下开发应用程序是很方便的,对Excel也一样,下面是我对操作Excel的总结 ExcelBorders.cs using System; using System.Collection ...
- NPOI操作Excel 002:读取Excel
本文讲述如何通过NPOI来读取Excel. 需要准备的dll见: http://blog.csdn.net/yysyangyangyangshan/article/details/42614181 环 ...
最新文章
- 增加行星轮减速后机械臂调试
- android工程创建,3.2.1 创建Android 项目(2)
- kali linux 中文输入法,Kali Linux安装中文输入法
- Spring Cloud Config 规范
- Java 获取 URL 响应头信息
- Linux系统间文件双向同步搭建Unison版
- java string sscanf_倾情奉献——JAVA sscanf函数!!!
- Visual Studio各组件说明
- armv6、armv7、armv7s、arm64 与开发静态库(.a)
- 中国第一大忽悠终于倒下了
- 4.STACEY矩阵及其对应的开发模型、敏捷开发评估方法
- MPC5744-LINFlexD
- 大数据应用对企业税务风险管理影响
- win10和win7哪个好用_Win10和Win7到底哪个好用(真实评测)
- VisualVM的配置和使用
- 转行程序员后,我开始后悔没做这件事
- 测试环境搭建(APP)
- matplotlib通过二维矩阵画散点图并且对每个点进行标记
- Set中的HashSet和TreeSet特点及用法
- No6-6.从零搭建spring-cloud-alibaba微服务框架,添加用户鉴权逻辑,动态数据权限(使用AOP实现)等(六,no6-6)