当前位置:网站首页>WeihanLi.Npoi 1.11.0/1.12.0 Release Notes

WeihanLi.Npoi 1.11.0/1.12.0 Release Notes

2020-11-06 01:15:00 Weihanli.

WeihanLi.Npoi 1.11.0/1.12.0 Release Notes

Intro

lately NPOI The extension has two new versions , thank shaka chow Help and support , these two items. 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. .

Both of the most recently updated features are Excel Import side ,1.11.0 The version supports formula import processing ,1.12.0 Supported by Attribute Configure the range of columns allowed to be read , It can also be done through Fluent API How to set CellFilter To filter columns that you don't want to read more flexibly .

Support for formula import

In previous versions, there was no formula support , The cell that imports a formula will be a string to read , It may be used in some scenarios Excel It would be very useful , So I tried to add formula support , Import only , No formula is supported when exporting , On the one hand, when calculating the derived value according to the formula, it may be necessary 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 particularly needed , Therefore, there is no support for formula export .

Row/Cell Filter Support

stay 1.10.0 In the version , We supported one 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 come up with the basis of Fluent API to Sheet increase RowFilter and CellFilter To flexibly configure the data range you want to read , With this, we can easily achieve interlaced reading or multiple range reading .

Show the Code

Excel Examples of formula Import Support :

[Theory]
[InlineData(ExcelFormat.Xls)]
[InlineData(ExcelFormat.Xlsx)]
public void ExcelImportWithFormula(ExcelFormat excelFormat)
{
    var setting = FluentSettings.For<ExcelFormulaTestModel>();
    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<ExcelFormulaTestModel>(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 need to modify any code , And the original API Is fully 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

[Theory]
[InlineData(ExcelFormat.Xls)]
[InlineData(ExcelFormat.Xlsx)]
public void ExcelImportWithCellFilterAttributeTest(ExcelFormat excelFormat)
{
    IReadOnlyList<CellFilterAttributeTest> 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<CellFilterAttributeTest>(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 , Corresponding to the last column Description Attributes are always null

Cell Filter Use Fluent API Examples of ways

[Theory]
[InlineData(ExcelFormat.Xls)]
[InlineData(ExcelFormat.Xlsx)]
public void ExcelImportWithCellFilter(ExcelFormat excelFormat)
{
    IReadOnlyList<Notice> 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<Notice>();
    settings.HasSheetSetting(setting =>
    {
        setting.CellFilter = cell => cell.ColumnIndex == 0;
    });

    var importedList = ExcelHelper.ToEntityList<Notice>(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 relatively simple , Only the data of the first column was imported , The attributes corresponding to other column data are default values

More

Except for the two main ones Feature outside , There are also a few small updates , Reconstructed ExcelSetting and SheetSetting , Provides a method to configure based on delegation , The original method is used as an extension method , The other is to optimize the file reading , It is mainly when reading the file that a specified FileShare Mode( For details, please refer to the link at the end of the article ), It turns out that if another process has opened the file , At this point, the import will throw an exception , After optimization, even if the file is occupied by other processes , You can still read the contents of the file for import operation , The experience might be better .

More details can be referred to Github Examples and unit tests in the warehouse

Reference

版权声明
本文为[Weihanli.]所创,转载请带上原文链接,感谢