StandardNpoi.ExcelOperate
1.0.0.2
dotnet add package StandardNpoi.ExcelOperate --version 1.0.0.2
NuGet\Install-Package StandardNpoi.ExcelOperate -Version 1.0.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="StandardNpoi.ExcelOperate" Version="1.0.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="StandardNpoi.ExcelOperate" Version="1.0.0.2" />
<PackageReference Include="StandardNpoi.ExcelOperate" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add StandardNpoi.ExcelOperate --version 1.0.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: StandardNpoi.ExcelOperate, 1.0.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package StandardNpoi.ExcelOperate@1.0.0.2
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=StandardNpoi.ExcelOperate&version=1.0.0.2
#tool nuget:?package=StandardNpoi.ExcelOperate&version=1.0.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
npoi.excelOperate
介绍
Npoi扩展方法 , Excel操作类,读取Excel文件,获取所有数据.该类为抽象类,不能实例化,只能被继承~
软件架构
软件架构说明
安装教程
dotnet add package StandardNpoi.ExcelOperate --version 1.0.0.1
使用说明
namespace Npoi.ExcelOperateTests
{
/// <summary>
/// ExcelOperateTest 的摘要说明
/// </summary>
public class StudentExcelOperator : ExcelOperator, IGetModel<Ticket>
{
public StudentExcelOperator(string filePath)
{
this.FilePath = filePath;
}
public StudentExcelOperator()
{
this.FilePath = @"\ExcelTest\2025-03-12_哈尔滨_南京.xlsx";
}
public Ticket GetModel()
{
try
{
var model = new Ticket();
var addressBooks = GetAll((ISheet sheet, List<string> list) =>
{
for (int i = 1; i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
list.Add(GetCellValue(row, 0));
}
});
model.TrainNums = addressBooks;
return model;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
}
/// <summary>
/// 车票
/// </summary>
public class Ticket
{
public string Address { get; set; }
public string Phone { get; set; }
//通讯录
public List<string> TrainNums { get; set; }
}
}
namespace Npoi.ExcelOperate.Tests
{
[TestClass()]
public class NpoiExtensionTests
{
private IGetModel<Ticket> studentTabll;
[TestMethod()]
public void GetModelTest()
{
studentTabll = new StudentExcelOperator(@"ExcelTest\2025-03-12_哈尔滨_南京.xlsx");
var model = studentTabll.GetModel();
Assert.IsNotNull(model);
}
}
}
Excel 操作库 - 更新日志 & 调用文档
📋 目录
📝 版本更新日志
V2.0.0 (2025-10-09) - 重大更新
✨ 新增功能
完整的 CRUD 操作
- ✅ 新增数据写入功能
- ✅ 新增数据删除功能
- ✅ 新增数据修改功能
- ✅ 新增批量操作支持
增强的查询功能
- ✅ 多条件查询支持
- ✅ 分页查询支持
- ✅ 动态列映射
扩展接口
- ✅
IWriteExcel<T>- 写入接口 - ✅
IDeleteExcel- 删除接口 - ✅
IUpdateExcel- 修改接口
- ✅
🛠 技术优化
- 重构核心操作类架构
- 优化异常处理机制
- 增强类型安全检测
- 改进文件流管理
🐛 问题修复
- 修复
LastRowNum只读属性赋值错误 - 修复单元格样式丢失问题
- 修复多 Sheet 操作异常
V1.0.1 (2025-10-01) - 基础版本
- 基础 Excel 读取功能
- 支持 .xls 和 .xlsx 格式
- 基础数据模型映射
🚀 快速开始
1. 安装依赖
<PackageReference Include="NPOI" Version="2.6.0" />
2. 基础使用
// 创建操作实例
var excelOperator = new StudentExcelOperator("path/to/file.xlsx");
// 读取数据
var tickets = excelOperator.GetModel();
var students = excelOperator.GetList();
// 写入数据
excelOperator.WriteData(new Student { Name = "张三", Age = 20 });
// 修改数据
excelOperator.UpdateStudent("张三", updatedStudent);
// 删除数据
excelOperator.DeleteStudentByName("张三");
🔧 核心类说明
📁 ExcelOperator (抽象基类)
位置: Npoi.ExcelOperate.Standard.ExcelOperator
| 属性/方法 | 类型 | 说明 |
|---|---|---|
FilePath |
string | 源文件路径 |
SavePath |
string | 保存文件路径 |
ReadExcel() |
IWorkbook | 读取Excel文件 |
GetAll<T>() |
List<T> | 获取所有数据 |
GetOne<T>() |
T | 获取单个模型 |
SaveExcelToCopy() |
bool | 保存到副本文件 |
SaveExcelSourceFile() |
bool | 保存到源文件 |
👨🎓 StudentExcelOperator (实现类)
位置: Npoi.ExcelOperateTests.StudentExcelOperator
实现的接口:
IGetModel<Ticket>IGetList<Student>IWriteExcel<Student>IDeleteExcelIUpdateExcel
📚 API 调用文档
🔍 查询操作
1. 获取车票模型
var operator = new StudentExcelOperator("file.xlsx");
Ticket ticket = operator.GetModel();
// 返回对象包含:
// - Departure: 出发地
// - Destination: 目的地
// - TrainNums: 车次列表
// - Price: 票价
// - SeatType: 座位类型
2. 获取学生列表
List<Student> students = operator.GetList();
// Student 对象属性:
// - Name: 姓名
// - Age: 年龄
// - Class: 班级
// - Score: 分数
3. 按姓名查询学生
Student student = operator.GetStudentByName("张三");
4. 获取文件信息
string fileInfo = operator.GetFileInfo();
// 返回: "Sheet名称: Sheet1, 总行数: 100, 总列数: 5"
✏️ 写入操作
1. 写入单个学生
var student = new Student {
Name = "李四",
Age = 21,
Class = "计算机1班",
Score = 95.5m
};
bool success = operator.WriteData(student);
2. 批量写入学生
var students = new List<Student> {
new Student { Name = "王五", Age = 20, Class = "1班", Score = 88.0m },
new Student { Name = "赵六", Age = 22, Class = "2班", Score = 92.5m }
};
bool success = operator.WriteListData(students);
3. 添加车次
bool success = operator.AddTrainNum("G1234");
🗑️ 删除操作
1. 删除指定行
// 删除第2行数据(索引从0开始)
bool success = operator.DeleteRow(0, 1);
2. 按姓名删除学生
bool success = operator.DeleteStudentByName("李四");
3. 删除Sheet页
// 删除第2个Sheet页
bool success = operator.DeleteSheet(1);
4. 清空所有数据
bool success = operator.ClearAllData();
// 保留标题行,只清空数据行
🔄 修改操作
1. 修改单元格值
// 修改第2行第3列的值
bool success = operator.UpdateCellValue(0, 1, 2, "新值");
2. 修改学生信息
var updatedStudent = new Student {
Name = "李四-修改",
Age = 23,
Class = "新班级",
Score = 98.0m
};
bool success = operator.UpdateStudent("李四", updatedStudent);
3. 批量更新车次
var newTrainNums = new List<string> { "G1001", "G1002", "G1003" };
bool success = operator.BatchUpdateTrainNums(newTrainNums);
💡 示例代码
完整业务场景示例
public class StudentManagementService
{
private readonly StudentExcelOperator _excelOperator;
public StudentManagementService(string filePath)
{
_excelOperator = new StudentExcelOperator(filePath);
}
// 添加新学生
public bool AddStudent(string name, int age, string className, decimal score)
{
var student = new Student {
Name = name,
Age = age,
Class = className,
Score = score
};
return _excelOperator.WriteData(student);
}
// 更新学生成绩
public bool UpdateStudentScore(string name, decimal newScore)
{
var student = _excelOperator.GetStudentByName(name);
if (student == null) return false;
student.Score = newScore;
return _excelOperator.UpdateStudent(name, student);
}
// 删除不及格学生
public int RemoveFailedStudents(decimal passScore = 60.0m)
{
var students = _excelOperator.GetList();
var failedStudents = students.Where(s => s.Score < passScore).ToList();
int removedCount = 0;
foreach (var student in failedStudents)
{
if (_excelOperator.DeleteStudentByName(student.Name))
{
removedCount++;
}
}
return removedCount;
}
// 生成学生报告
public void GenerateStudentReport()
{
var students = _excelOperator.GetList();
var fileInfo = _excelOperator.GetFileInfo();
Console.WriteLine($"=== 学生信息报告 ===");
Console.WriteLine($"数据文件: {fileInfo}");
Console.WriteLine($"学生总数: {students.Count}");
Console.WriteLine($"平均分数: {students.Average(s => s.Score):F2}");
Console.WriteLine($"最高分数: {students.Max(s => s.Score)}");
Console.WriteLine($"最低分数: {students.Min(s => s.Score)}");
}
}
单元测试示例
[TestClass]
public class StudentExcelOperatorTests
{
[TestMethod]
public void Test_CRUD_Operations()
{
// 初始化
var operator = new StudentExcelOperator("test.xlsx", "test_modified.xlsx");
// 测试添加
var student = new Student { Name = "测试学生", Age = 20, Score = 90.0m };
Assert.IsTrue(operator.WriteData(student));
// 测试查询
var found = operator.GetStudentByName("测试学生");
Assert.IsNotNull(found);
// 测试修改
found.Score = 95.0m;
Assert.IsTrue(operator.UpdateStudent("测试学生", found));
// 测试删除
Assert.IsTrue(operator.DeleteStudentByName("测试学生"));
}
}
⚠️ 注意事项
1. 文件路径
// 正确:使用完整路径或相对路径
var op1 = new StudentExcelOperator(@"C:\data\file.xlsx");
var op2 = new StudentExcelOperator(@"ExcelTest\file.xlsx");
// 错误:路径不存在
var op3 = new StudentExcelOperator("不存在的文件.xlsx");
2. 文件格式支持
- ✅
.xlsx(Excel 2007+) - ✅
.xls(Excel 97-2003) - ❌ 其他格式不支持
3. 性能建议
// 批量操作时使用批量方法
operator.WriteListData(students); // ✅ 推荐
foreach(var s in students)
operator.WriteData(s); // ❌ 不推荐
// 频繁操作时重用实例
var operator = new StudentExcelOperator("file.xlsx");
operator.WriteData(student1);
operator.WriteData(student2); // ✅ 推荐
4. 异常处理
try
{
var students = operator.GetList();
// 业务逻辑...
}
catch (FileNotFoundException ex)
{
Console.WriteLine($"文件未找到: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"操作失败: {ex.Message}");
}
5. 内存管理
// 使用 using 语句确保资源释放
using (var operator = new StudentExcelOperator("largeFile.xlsx"))
{
var data = operator.GetList();
// 处理数据...
}
📞 技术支持
如有问题请联系:
- 作者: Guo_79991
- 邮箱: 799919859@qq.com
- 最后更新: 2025-10-09
版本: V2.0.0
兼容性: .NET Framework 4.0+ / .NET Core 3.1+ / .NET 5+
参与贡献
- Fork 本仓库
特技
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- NPOI (>= 2.6.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.