Skip to content

v1.16.0

Compare
Choose a tag to compare
@sveinungf sveinungf released this 30 Jun 22:31
· 357 commits to main since this release

Features

  • Added attribute ColumnWidth, which can be used to set fixed column widths when using the source generator. The column widths must be set when starting a worksheet, and a new overload of Spreadsheet.StartWorksheetAsync has been added for convenience. Here is an example:
    public class Book
    {
        [ColumnWidth(50)]
        public string Title { get; set; }
    
        [ColumnWidth(20)]
        public string Author { get; set; }
    }
    
    [WorksheetRow(typeof(Book))]
    public partial class BookContext : WorksheetRowContext;
    // Alternative 1: Use the new overload of StartWorksheetAsync.
    // It takes the context type generated by the source generator as the second parameter.
    await spreadsheet.StartWorksheetAsync("Sheet", BookContext.Default.Book);
    
    var book = new Book { Title = "A Game of Thrones", Author = "George R. R. Martin" };
    await spreadsheet.AddAsRowAsync(book, BookContext.Default.Book);
    In case you need to use WorksheetOptions, you can instead create an instance with the column widths like this:
    // Alternative 2: Create an instance of WorksheetOptions from the context type.
    // The WorksheetOptions instance will be created with the column widths from the class attributes.
    var options = BookContext.Default.Book.CreateWorksheetOptions();
    
    // Can now set additional options, for example frozen columns.
    options.FrozenColumns = 1;
    
    await spreadsheet.StartWorksheetAsync("Sheet", options);
    
    var book = new Book { Title = "A Game of Thrones", Author = "George R. R. Martin" };
    await spreadsheet.AddAsRowAsync(book, BookContext.Default.Book);
  • Added attribute CellValueTruncate, which can be used to truncate string values when using the source generator. Here is an example:
    public class LoremIpsum
    {
        [CellValueTruncate(15)]
        public string Text { get; set; }
    }
    
    [WorksheetRow(typeof(LoremIpsum))]
    public partial class LoremIpsumContext : WorksheetRowContext;
    var loremIpsum = new LoremIpsum { Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit." };
    await spreadsheet.AddAsRowAsync(loremIpsum, LoremIpsumContext.Default.LoremIpsum);
    In this example, the value in cell A1 will be Lorem ipsum dol (the 15 first characters). A string consisting of fewer characters than the truncate length will have its whole value written to the cell. Internally the implementation will use ReadOnlyMemory<char> to avoid additional string allocations when truncating strings.

Bug fixes

  • Fixed a compilation issue for UWP when using .NET Native (issue #58).
  • WorksheetOptions.Column(int columnNumber) will no longer allow a column number larger than 16384 (maximum in Excel). An exception will be thrown if attempting to use a larger number.

Performance improvements

  • Smaller loop performance improvements in the hot path when adding rows to a worksheet.
  • Smaller improvements to memory allocations.