当前位置:网站首页>C # use npoi to operate Excel
C # use npoi to operate Excel
2022-07-26 08:45:00 【Miracle Fan】
C# Use NPOI operation Excel
1. Introduce namespace
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;
using System.Data;
2. Read file stream
using (FileStream fs = File.OpenRead(filePath)){}
3. Read EXCEL
3.1 Create workbook object
IWorkbook workbook = null;
3.2 compatible .xls and .xlsx edition
if (filePath.IndexOf(".xlsx") > 0)
{
workbook = new XSSFWorkbook(fs);
}
// If it is 2003- Of Excel edition
else if (filePath.IndexOf(".xls") > 0)
{
workbook = new HSSFWorkbook(fs);
}
3.3 Get the contents of the form
function | explain |
---|---|
ISheet sheet = workbook.GetSheetAt(p) | Get workbook page p Page content |
IRow firstRow = sheet.GetRow(i) | Get form number i Row content |
ICell cell = firstRow.GetCell(i) | Get the first line i Cell contents |
IRow.LastRowNum/FirstRowNum | Get the last and first rows of the table |
IRow.LastCellNum/FirstCellNum | Get the last and first columns of the table |
ISheet.SheetName | Get the working page name |
3.4 Cell related styles
cell.CellType
Get cell style , Returns the value
Cell style | The number |
---|---|
CellType.Unknown | -1 |
CellType.Numeric | 0 |
CellType.String | 1 |
CellType.Formula The formula | 2 |
CellType.Blank | 3 |
CellType.Boolean | 4 |
CellType.Error | 5 |
Returns the cell value in the specified form
attribute | explain |
---|---|
cell.NumericCellValue; | Save value in numerical form |
cell.StringCellValue; | In string form |
cell.DateCellValue | In the form of a date |
cell.RichStringCellValue | In rich text |
4. write in Excel
4.1 Create relevant content
function | explain |
---|---|
workbook.CreateSheet() | Create Workbook |
Isheet.CreateRow(0) | Create data rows |
IRow.CreateCell(c) | Create cells |
4.2 Write the value
cell.SetCellValue(data);
4.3 Output as file
outpath Output path for file
using (FileStream fs = File.OpenWrite(Outpath))
{
workbook.Write(fs);// To the open one xls Write data to file
result = true;
}
边栏推荐
- Oracle 19C OCP 1z0-082 certification examination question bank (30-35)
- Oracle 19C OCP 1z0-082 certification examination question bank (13-18)
- Use index to optimize SQL query "suggestions collection"
- Lesson 3: gcc compiler
- 03异常处理,状态保持,请求钩子---04大型项目结构与蓝图
- After MySQL 8 OCP (1z0-908), hand in your homework
- Uninstallation of dual systems
- The effective condition of MySQL joint index and the invalid condition of index
- QT note 2
- Memory management based on C language - Simulation of dynamic partition allocation
猜你喜欢
内存管理-动态分区分配方式模拟
六、品达通用权限系统__pd-tools-log
Foundry tutorial: writing scalable smart contracts in various ways (Part 1)
[freeswitch development practice] use SIP client Yate to connect freeswitch for VoIP calls
Memory management - dynamic partition allocation simulation
C Entry series (31) -- operator overloading
03异常处理,状态保持,请求钩子---04大型项目结构与蓝图
Sub Chocolate & paint area
SSH,NFS,FTP
Excel find duplicate lines
随机推荐
Mysql8 dual master and dual slave +mycat2 read / write separation
Please tell me if there is any way to increase the write out rate when the Flink SQL client is in the sink table. When synchronizing through sink table
Kept dual machine hot standby
Oracle 19C OCP 1z0-082 certification examination question bank (36-41)
OA项目之我的会议(会议排座&送审)
CIS 2020 - alternative skills against cloud WAF (pyn3rd)
QT note 1
Ansible important components (playbook)
23.10 Admin features
One click deployment of lamp and LNMP scripts is worth having
Cve-2021-3156 duplicate of sudo heap overflow privilege raising vulnerability
【C语言】程序员筑基功法——《函数栈帧的创建与销毁》
Oracle 19C OCP 1z0-082 certification examination question bank 1
When developing flutter, idea_ ID cannot solve the problem
Oracle 19C OCP 1z0-082 certification examination question bank (24-29)
keepalived双机热备
Kotlin data type
2022年收益率最高的理财产品是哪个?
Pxe原理和概念
Use index to optimize SQL query "suggestions collection"