RSCG – spreadcheetah

RSCG – spreadcheetah
 
 

name spreadcheetah
nuget https://www.nuget.org/packages/spreadcheetah/
link https://github.com/sveinungf/spreadcheetah
author Sveinung

generating Excel from objects

 

This is how you can use spreadcheetah .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="SpreadCheetah" Version="1.10.0" />
  </ItemGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
</Project>


The code that you will use is


using SpreadCheetah;
using spreadcheetahDemo;

using var stream = File.Create("a.xlsx");
using var spreadsheet = await Spreadsheet.CreateNewAsync(stream);

// A spreadsheet must contain at least one worksheet.
await spreadsheet.StartWorksheetAsync("Sheet 1");

// Cells are inserted row by row.
var row = new List<Cell>();
row.Add(new Cell("Answer to the ultimate question:"));
row.Add(new Cell(42));

// Rows are inserted from top to bottom.
await spreadsheet.AddRowAsync(row);
var p=new Person();
p.FirstName = "Andrei";
p.LastName = "Ignat";
await spreadsheet.AddAsRowAsync(p, PersonRowContext.Default.Person);

// Remember to call Finish before disposing.
// This is important to properly finalize the XLSX file.
await spreadsheet.FinishAsync();
Console.WriteLine("see a.xlsx");



using SpreadCheetah.SourceGeneration;

namespace spreadcheetahDemo;

public class Person
{
    public string? FirstName { get; set; }
    public string? LastName { get; set; }
}

[WorksheetRow(typeof(Person))]
public partial class PersonRowContext : WorksheetRowContext
{
}

 

The code that is generated is

// <auto-generated />
#nullable enable
using SpreadCheetah;
using SpreadCheetah.SourceGeneration;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace spreadcheetahDemo
{
    public partial class PersonRowContext
    {
        private static PersonRowContext? _default;
        public static PersonRowContext Default => _default ??= new PersonRowContext();

        public PersonRowContext()
        {
        }

        private WorksheetRowTypeInfo<spreadcheetahDemo.Person>? _Person;
        public WorksheetRowTypeInfo<spreadcheetahDemo.Person> Person => _Person ??= WorksheetRowMetadataServices.CreateObjectInfo<spreadcheetahDemo.Person>(AddAsRowAsync, AddRangeAsRowsAsync);

        private static ValueTask AddAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, spreadcheetahDemo.Person? obj, CancellationToken token)
        {
            if (spreadsheet is null)
                throw new ArgumentNullException(nameof(spreadsheet));
            if (obj is null)
                return spreadsheet.AddRowAsync(ReadOnlyMemory<DataCell>.Empty, token);
            return AddAsRowInternalAsync(spreadsheet, obj, token);
        }

        private static ValueTask AddRangeAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable<spreadcheetahDemo.Person?> objs, CancellationToken token)
        {
            if (spreadsheet is null)
                throw new ArgumentNullException(nameof(spreadsheet));
            if (objs is null)
                throw new ArgumentNullException(nameof(objs));
            return AddRangeAsRowsInternalAsync(spreadsheet, objs, token);
        }

        private static async ValueTask AddAsRowInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, spreadcheetahDemo.Person obj, CancellationToken token)
        {
            var cells = ArrayPool<DataCell>.Shared.Rent(2);
            try
            {
                await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false);
            }
            finally
            {
                ArrayPool<DataCell>.Shared.Return(cells, true);
            }
        }

        private static async ValueTask AddRangeAsRowsInternalAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable<spreadcheetahDemo.Person?> objs, CancellationToken token)
        {
            var cells = ArrayPool<DataCell>.Shared.Rent(2);
            try
            {
                await AddEnumerableAsRowsAsync(spreadsheet, objs, cells, token).ConfigureAwait(false);
            }
            finally
            {
                ArrayPool<DataCell>.Shared.Return(cells, true);
            }
        }

        private static async ValueTask AddEnumerableAsRowsAsync(SpreadCheetah.Spreadsheet spreadsheet, IEnumerable<spreadcheetahDemo.Person?> objs, DataCell[] cells, CancellationToken token)
        {
            foreach (var obj in objs)
            {
                await AddCellsAsRowAsync(spreadsheet, obj, cells, token).ConfigureAwait(false);
            }
        }

        private static ValueTask AddCellsAsRowAsync(SpreadCheetah.Spreadsheet spreadsheet, spreadcheetahDemo.Person? obj, DataCell[] cells, CancellationToken token)
        {
            if (obj is null)
                return spreadsheet.AddRowAsync(ReadOnlyMemory<DataCell>.Empty, token);

            cells[0] = new DataCell(obj.FirstName);
            cells[1] = new DataCell(obj.LastName);
            return spreadsheet.AddRowAsync(cells.AsMemory(0, 2), token);
        }
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/spreadcheetah