当前位置:网站首页>WeihanLi.Npoi 1.11.0/1.12.0 Release Notes
WeihanLi.Npoi 1.11.0/1.12.0 Release Notes
2020-11-06 01:35:47 【itread01】
# WeihanLi.Npoi 1.11.0/1.12.0 Release Notes ## Intro lately NPOI There are two new versions of the Expansion Kit , Thank you `shaka chow` The help and support of , These two Feature It's all from him , And tested several versions for me , It also provides me with a better way to read files , Thank you very much . The two features recently updated are Excel In terms of import ,1.11.0 Version supports formula import processing ,1.12.0 Support through Attribute Configure the range of columns allowed to be read , It can also be done by Fluent API How to set `CellFilter` To filter columns that you don't want to read more flexibly . ## Formula Import Support There was no formula support in previous versions , The cell into which a formula is imported will be a string to read , In some situations, you may use Excel It will be very useful , This is an attempt to add formula support , Just import , There is no support formula when exporting , On the one hand, when calculating the exported value according to the formula, you may need to fill in all the values first and then calculate the value of the formula , Such a scenario can lead to inefficiency , On the other hand, I think Fluent API The way to meet the needs of most scenarios , Not in particular , So there is no support for exporting formulas . ## Row/Cell Filter Support stay 1.10.0 In the version , We supported a `EndRowIndex` To configure an end line , To end reading data ahead of time , stay 1.12.0 In the version `shaka` It is suggested that it can be increased `StartColumnIndex` as well as `EndColumnIndex` Configure to specify the range of columns to read , On this basis, I came up with the idea that Fluent API to Sheet increase `RowFilter` and `CellFilter` To flexibly configure the data range you want to read , With this, we can easily implement interlaced reading or multiple range reading . ## Show the Code > Excel Examples of formula Import Support : ``` csharp [Theory] [InlineData(ExcelFormat.Xls)] [InlineData(ExcelFormat.Xlsx)] public void ExcelImportWithFormula(ExcelFormat excelFormat) { var setting = FluentSettings.For
(); setting.HasSheetConfiguration(0, "Test", 0); setting.Property(x => x.Num1).HasColumnIndex(0); setting.Property(x => x.Num2).HasColumnIndex(1); setting.Property(x => x.Sum).HasColumnIndex(2); var workbook = ExcelHelper.PrepareWorkbook(excelFormat); var sheet = workbook.CreateSheet(); var row = sheet.CreateRow(0); row.CreateCell(0, CellType.Numeric).SetCellValue(1); row.CreateCell(1, CellType.Numeric).SetCellValue(2); row.CreateCell(2, CellType.Formula).SetCellFormula("$A1+$B1"); var excelBytes = workbook.ToExcelBytes(); var list = ExcelHelper.ToEntityList
(excelBytes, excelFormat); Assert.NotNull(list); Assert.NotEmpty(list); Assert.Equal(1, list[0].Num1); Assert.Equal(2, list[0].Num2); Assert.Equal(3, list[0].Sum); } ``` Formula support does not require any code changes , And the original API It's completely compatible , You can see that the imported value of the above formula has been successfully replaced with the calculated value > Cell Filter Use Attribute Examples of ways ``` csharp [Theory] [InlineData(ExcelFormat.Xls)] [InlineData(ExcelFormat.Xlsx)] public void ExcelImportWithCellFilterAttributeTest(ExcelFormat excelFormat) { IReadOnlyList
list = Enumerable.Range(0, 10).Select(i => new CellFilterAttributeTest() { Id = i + 1, Description = $"content_{i}", Name = $"title_{i}", }).ToArray(); var excelBytes = list.ToExcelBytes(excelFormat); var importedList = ExcelHelper.ToEntityList
(excelBytes, excelFormat); Assert.NotNull(importedList); Assert.Equal(list.Count, importedList.Count); for (var i = 0; i < importedList.Count; i++) { Assert.Equal(list[i].Id, importedList[i].Id); Assert.Equal(list[i].Name, importedList[i].Name); Assert.Null(importedList[i].Description); } } [Sheet(SheetName = "test", AutoColumnWidthEnabled = true, StartColumnIndex = 0, EndColumnIndex = 1)] private class CellFilterAttributeTest { [Column(Index = 0)] public int Id { get; set; } [Column(Index = 1)] public string Name { get; set; } [Column(Index = 2)] public string Description { get; set; } } ``` You can see that the values in the last column are actually ignored , The last column corresponds to `Description` Attributes are always `null` > Cell Filter Use Fluent API Examples of ways ``` csharp [Theory] [InlineData(ExcelFormat.Xls)] [InlineData(ExcelFormat.Xlsx)] public void ExcelImportWithCellFilter(ExcelFormat excelFormat) { IReadOnlyList
list = Enumerable.Range(0, 10).Select(i => new Notice() { Id = i + 1, Content = $"content_{i}", Title = $"title_{i}", PublishedAt = DateTime.UtcNow.AddDays(-i), Publisher = $"publisher_{i}" }).ToArray(); var excelBytes = list.ToExcelBytes(excelFormat); var settings = FluentSettings.For
(); settings.HasSheetSetting(setting => { setting.CellFilter = cell => cell.ColumnIndex == 0; }); var importedList = ExcelHelper.ToEntityList
(excelBytes, excelFormat); Assert.Equal(list.Count, importedList.Count); for (var i = 0; i < list.Count; i++) { if (list[i] == null) { Assert.Null(importedList[i]); } else { Assert.Equal(list[i].Id, importedList[i].Id); Assert.Null(importedList[i].Title); Assert.Null(importedList[i].Content); Assert.Null(importedList[i].Publisher); Assert.Equal(default(DateTime).ToStandardTimeString(), importedList[i].PublishedAt.ToStandardTimeString()); } } settings.HasSheetSetting(setting => { setting.CellFilter = null; }); } ``` This example is simple , Only the first column of data is imported , The properties corresponding to other columns of data are default values ## More Except for these two main Feature outside , There are also a few small updates , Reconstructed `ExcelSetting` and `SheetSetting` , Provides delegate based configuration methods , The original method is used as an extension kit method , The other is to optimize the file reading , It is mainly when reading the file that one is specified `FileShare` Mode( For details, please refer to the link at the end of the article ), It turns out that if another program has opened the file , At this time, if you import again, you will throw an exception , After optimization, even if the file is occupied by other programs , You can still read the contents of the file for import operation , The experience might be better . For more details, please refer to Github Examples and unit tests in the repository ## Reference -
-
-
-
版权声明
本文为[itread01]所创,转载请带上原文链接,感谢
边栏推荐
- C++ 数字、string和char*的转换
- C++学习——centos7上部署C++开发环境
- C++学习——一步步学会写Makefile
- C++学习——临时对象的产生与优化
- C++学习——对象的引用的用法
- C++编程经验(6):使用C++风格的类型转换
- Won the CKA + CKS certificate with the highest gold content in kubernetes in 31 days!
- C + + number, string and char * conversion
- C + + Learning -- capacity() and resize() in C + +
- C + + Learning -- about code performance optimization
猜你喜欢
-
C + + programming experience (6): using C + + style type conversion
-
Latest party and government work report ppt - Park ppt
-
在线身份证号码提取生日工具
-
Online ID number extraction birthday tool
-
️野指针?悬空指针?️ 一文带你搞懂!
-
Field pointer? Dangling pointer? This article will help you understand!
-
HCNA Routing&Switching之GVRP
-
GVRP of hcna Routing & Switching
-
Seq2Seq实现闲聊机器人
-
【闲聊机器人】seq2seq模型的原理
随机推荐
- LeetCode 91. 解码方法
- Seq2seq implements chat robot
- [chat robot] principle of seq2seq model
- Leetcode 91. Decoding method
- HCNA Routing&Switching之GVRP
- GVRP of hcna Routing & Switching
- HDU7016 Random Walk 2
- [Code+#1]Yazid 的新生舞会
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- HDU7016 Random Walk 2
- [code + 1] Yazid's freshman ball
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- Qt Creator 自动补齐变慢的解决
- HALCON 20.11:如何处理标定助手品质问题
- HALCON 20.11:标定助手使用注意事项
- Solution of QT creator's automatic replenishment slowing down
- Halcon 20.11: how to deal with the quality problem of calibration assistant
- Halcon 20.11: precautions for use of calibration assistant
- “十大科学技术问题”揭晓!|青年科学家50²论坛
- "Top ten scientific and technological issues" announced| Young scientists 50 ² forum
- 求反转链表
- Reverse linked list
- js的数据类型
- JS data type
- 记一次文件读写遇到的bug
- Remember the bug encountered in reading and writing a file
- 单例模式
- Singleton mode
- 在这个 N 多编程语言争霸的世界,C++ 究竟还有没有未来?
- In this world of N programming languages, is there a future for C + +?
- es6模板字符
- js Promise
- js 数组方法 回顾
- ES6 template characters
- js Promise
- JS array method review
- 【Golang】️走进 Go 语言️ 第一课 Hello World
- [golang] go into go language lesson 1 Hello World