Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions src/MiniExcel.Core/Helpers/NetStandardHelper.cs

This file was deleted.

63 changes: 63 additions & 0 deletions src/MiniExcel.Core/Helpers/Polyfills.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.ComponentModel;
using System.IO.Compression;

namespace MiniExcelLib.Core.Helpers;

/* todo: instead of using the EditorBrowsableAttribute consider making this class internal and link it for compilation
in the other projects that require it so as to prevent the consumers' IDEs to be polluted with these extension methods */
[EditorBrowsable(EditorBrowsableState.Advanced)]
public static class Polyfills
{
#if NETSTANDARD2_0
[EditorBrowsable(EditorBrowsableState.Advanced)]
public static TValue? GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue? defaultValue = default)
{
return dictionary.TryGetValue(key, out var value) ? value : defaultValue;
}

extension(Math)
{
[EditorBrowsable(EditorBrowsableState.Advanced)]
public static TNumber Clamp<TNumber>(TNumber value, TNumber min, TNumber max) where TNumber : unmanaged, IComparable<TNumber>
Comment thread
michelebastione marked this conversation as resolved.
{
if (value.CompareTo(min) < 0) return min;
if (value.CompareTo(max) > 0) return max;
return value;
}
}
#endif

#if !NET10_0_OR_GREATER
extension(ZipArchiveEntry entry)
{
[EditorBrowsable(EditorBrowsableState.Advanced)]
public ValueTask<Stream> OpenAsync(CancellationToken cancellationToken = default)
{
var stream = entry.Open();
return new ValueTask<Stream>(stream);
}
}

extension(ZipArchive)
{
[EditorBrowsable(EditorBrowsableState.Advanced)]
public static ValueTask<ZipArchive> CreateAsync(Stream stream, ZipArchiveMode mode, bool leaveOpen, Encoding? entryNameEncoding = null, CancellationToken cancellationToken = default)
{
ZipArchive? archive = null;

try
{
archive = new ZipArchive(stream, mode, leaveOpen, entryNameEncoding);
var result = new ValueTask<ZipArchive>(archive);

archive = null;
return result;
}
finally
{
archive?.Dispose();
}
}
}
#endif
Comment thread
michelebastione marked this conversation as resolved.
}
4 changes: 2 additions & 2 deletions src/MiniExcel.Core/Helpers/SynchronousHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class SynchronousHelper
{
extension(ZipArchive)
{
public static ZipArchive Create(Stream stream, ZipArchiveMode mode, bool leaveOpen, Encoding? encoding = null)
=> new(stream, mode, leaveOpen, encoding);
public static ZipArchive Create(Stream stream, ZipArchiveMode mode, bool leaveOpen, Encoding? entryNameEncoding = null)
=> new(stream, mode, leaveOpen, entryNameEncoding);
}
}
40 changes: 40 additions & 0 deletions src/MiniExcel.OpenXml/Api/OpenXmlExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,44 @@ public async Task<int[]> ExportAsync(Stream stream, object value, bool printHead

return await writer.SaveAsAsync(progress, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Modify the properties of a worksheet in the specified document.
/// </summary>
/// <param name="path">The path to the OpenXml document.</param>
/// <param name="sheetName">The name of the worksheet to modify.</param>
/// <param name="newSheetName">The new name to assign to the worksheet, or <c>null</c> to leave as is.</param>
/// <param name="newSheetIndex">The position in the workbook to assign to the worksheet, or <c>null</c> to leave as is.</param>
/// <param name="newSheetState">The visibility state to assign to the worksheet, or <c>null</c> to leave as is.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests</param>
[CreateSyncVersion]
public async Task AlterSheetAsync(string path, string sheetName, string? newSheetName = null, int? newSheetIndex = null, SheetState? newSheetState = null, CancellationToken cancellationToken = default)
{
#if NET8_0_OR_GREATER
var stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
await using var disposableStream = stream.ConfigureAwait(false);
#else
using var stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
#endif
await AlterSheetAsync(stream, sheetName, newSheetName, newSheetIndex, newSheetState, cancellationToken).ConfigureAwait(false);
Comment thread
michelebastione marked this conversation as resolved.
}

/// <summary>
/// Modify the properties of a worksheet in the specified document.
/// </summary>
/// <param name="stream">The stream to the OpenXml document.</param>
/// <param name="sheetName">The name of the worksheet to modify.</param>
/// <param name="newSheetName">The new name to assign to the worksheet, or <c>null</c> to leave as is.</param>
/// <param name="newSheetIndex">The position in the workbook to assign to the worksheet, or <c>null</c> to leave as is.</param>
/// <param name="newSheetState">The visibility state to assign to the worksheet, or <c>null</c> to leave as is.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests</param>
[CreateSyncVersion]
public async Task AlterSheetAsync(Stream stream, string sheetName, string? newSheetName = null, int? newSheetIndex = null, SheetState? newSheetState = null, CancellationToken cancellationToken = default)
Comment thread
michelebastione marked this conversation as resolved.
{
var writer = await OpenXmlWriter
.CreateAsync(stream, null, sheetName, false, new OpenXmlConfiguration { FastMode = true }, cancellationToken)
.ConfigureAwait(false);

await writer.AlterWorksheetAsync(sheetName, newSheetName, newSheetIndex, newSheetState, cancellationToken).ConfigureAwait(false);
}
}
45 changes: 27 additions & 18 deletions src/MiniExcel.OpenXml/FluentMapping/MappingTemplateApplicator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System.IO.Compression;
using Zomp.SyncMethodGenerator;

namespace MiniExcelLib.OpenXml.FluentMapping;

internal static partial class MappingTemplateApplicator<T> where T : class
Expand Down Expand Up @@ -37,12 +34,20 @@ public static async Task ApplyTemplateAsync(
}

templateStream.Position = 0;


#if NET10_0_OR_GREATER
Comment thread
michelebastione marked this conversation as resolved.
// Open template archive for reading
using var templateArchive = new ZipArchive(templateStream, ZipArchiveMode.Read, leaveOpen: true);

var templateArchive = await ZipArchive.CreateAsync(templateStream, ZipArchiveMode.Read, true, null, cancellationToken: cancellationToken).ConfigureAwait(false);
await using var disposableTemplateArchive = templateArchive.ConfigureAwait(false);

// Create output archive
using var outputArchive = new ZipArchive(outputStream, ZipArchiveMode.Create, leaveOpen: true);
var outputArchive = await ZipArchive.CreateAsync(outputStream, ZipArchiveMode.Create, true, null, cancellationToken).ConfigureAwait(false);
await using var disposableOutputArchive = outputArchive.ConfigureAwait(false);
Comment thread
michelebastione marked this conversation as resolved.
#else
using var templateArchive = new ZipArchive(templateStream, ZipArchiveMode.Read, true);
using var outputArchive = new ZipArchive(outputStream, ZipArchiveMode.Create, true);
#endif


// Process each entry
foreach (var entry in templateArchive.Entries)
Expand Down Expand Up @@ -106,17 +111,18 @@ private static async Task CopyEntryAsync(
targetEntry.LastWriteTime = sourceEntry.LastWriteTime;

// Copy content
#if NET10_0_OR_GREATER
using var sourceStream = await sourceEntry.OpenAsync(cancellationToken).ConfigureAwait(false);
using var targetStream = await targetEntry.OpenAsync(cancellationToken).ConfigureAwait(false);
#if NET8_0_OR_GREATER
var sourceStream = await sourceEntry.OpenAsync(cancellationToken).ConfigureAwait(false);
var targetStream = await targetEntry.OpenAsync(cancellationToken).ConfigureAwait(false);

await using var disposableSourceStream = sourceStream.ConfigureAwait(false);
await using var disposableTargetStream = targetStream.ConfigureAwait(false);

await sourceStream.CopyToAsync(targetStream, cancellationToken).ConfigureAwait(false);
#else
using var sourceStream = sourceEntry.Open();
using var targetStream = targetEntry.Open();
#endif

#if NETCOREAPP2_1_OR_GREATER
await sourceStream.CopyToAsync(targetStream, cancellationToken).ConfigureAwait(false);
#else

await sourceStream.CopyToAsync(targetStream).ConfigureAwait(false);
#endif
}
Expand All @@ -135,9 +141,12 @@ private static async Task ProcessWorksheetAsync(
targetEntry.LastWriteTime = sourceEntry.LastWriteTime;

// Open streams
#if NET10_0_OR_GREATER
using var sourceStream = await sourceEntry.OpenAsync(cancellationToken).ConfigureAwait(false);
using var targetStream = await targetEntry.OpenAsync(cancellationToken).ConfigureAwait(false);
#if NET8_0_OR_GREATER
var sourceStream = await sourceEntry.OpenAsync(cancellationToken).ConfigureAwait(false);
var targetStream = await targetEntry.OpenAsync(cancellationToken).ConfigureAwait(false);

await using var disposableSourceStream = sourceStream.ConfigureAwait(false);
await using var disposableTargetStream = targetStream.ConfigureAwait(false);
#else
using var sourceStream = sourceEntry.Open();
using var targetStream = targetEntry.Open();
Expand Down
15 changes: 15 additions & 0 deletions src/MiniExcel.OpenXml/Models/ExcelSheetInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace MiniExcelLib.OpenXml.Models;

internal class ExcelSheetInfo
{
public object Key { get; set; }
public string? ExcelSheetName { get; set; }
public SheetState ExcelSheetState { get; set; }

public SheetDto ToDto(int sheetIndex) => new()
{
Name = ExcelSheetName,
SheetIdx = sheetIndex,
State = ExcelSheetState.ToString().ToLower()
};
}
15 changes: 0 additions & 15 deletions src/MiniExcel.OpenXml/Models/ExcellSheetInfo.cs

This file was deleted.

8 changes: 4 additions & 4 deletions src/MiniExcel.OpenXml/Models/SheetInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ namespace MiniExcelLib.OpenXml.Models;
public class SheetInfo(uint id, uint index, string name, SheetState sheetState, bool active)
{
/// <summary>
/// Internal sheet id - depends on the order in which the sheet is added
/// Internal sheet id - depends on the order in which the sheet is added.
/// </summary>
public uint Id { get; } = id;

/// <summary>
/// Next sheet index - numbered from 0
/// The 0-based index of the worksheet in the workbook
/// </summary>
public uint Index { get; } = index;

/// <summary>
/// Sheet name
/// The name of the worksheet
/// </summary>
public string Name { get; } = name;

Expand All @@ -23,7 +23,7 @@ public class SheetInfo(uint id, uint index, string name, SheetState sheetState,
public SheetState State { get; } = sheetState;

/// <summary>
/// Indicates whether the sheet is active
/// Indicates whether the worksheet was active the last time
/// </summary>
public bool Active { get; } = active;
}
Expand Down
Loading
Loading