当前位置:网站首页>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]所创,转载请带上原文链接,感谢