Skip to content

Commit

Permalink
Add compression level option to ExcelDataWriter.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkPflug committed Aug 16, 2023
1 parent 3eb8b16 commit 318192c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 19 deletions.
5 changes: 5 additions & 0 deletions docs/ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Sylvan.Data.Excel Release Notes

_0.4.14_
- Add `CompressionLevel` as a configuration option for ExcelDataWriter.
Changes the default compression level to `Fastest`, which produces
*slightly* larger files, but takes significantly less time.

_0.4.13_
- Fix for .xlsx/.xlsb files that contain OpenPackage relations with rooted paths.
Third party libraries sometimes write files in this way.
Expand Down
16 changes: 16 additions & 0 deletions source/Sylvan.Data.Excel/ExcelDataWriterOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace Sylvan.Data.Excel;

using System.IO.Compression;

/// <summary>
/// Options for controlling the behavior of an <see cref="ExcelDataWriter"/>.
/// </summary>
Expand All @@ -12,9 +14,23 @@ public sealed class ExcelDataWriterOptions
// shared string behavior?
// binary as hex/basxe64

/// <summary>
/// Creates a new ExcelDataWriterOptions instance when the default settings.
/// </summary>
public ExcelDataWriterOptions()
{
this.TruncateStrings = false;
this.CompressionLevel = CompressionLevel.Fastest;
}

/// <summary>
/// Indicates if string values should be truncated to the limit of Excel, which allows a maximum of 32k characters.
/// </summary>
/// <remarks>When false, an exception will be thrown if a string values exceeds the limit.</remarks>
public bool TruncateStrings { get; set; }

/// <summary>
/// The compression level to use.
/// </summary>
public CompressionLevel CompressionLevel { get; set; }
}
2 changes: 1 addition & 1 deletion source/Sylvan.Data.Excel/OpenPackaging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ static string MakeRelative(string root, string path)

internal static void WriteAppProps(ZipArchive zipArchive)
{
var appEntry = zipArchive.CreateEntry(AppPath, CompressionLevel.Optimal);
var appEntry = zipArchive.CreateEntry(AppPath, CompressionLevel.Fastest);
using var appStream = appEntry.Open();
using var xw = XmlWriter.Create(appStream, XmlSettings);
xw.WriteStartElement("Properties", PropNS);
Expand Down
2 changes: 1 addition & 1 deletion source/Sylvan.Data.Excel/Sylvan.Data.Excel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.1;netstandard2.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<VersionPrefix>0.4.13</VersionPrefix>
<VersionPrefix>0.4.14</VersionPrefix>
<Description>A cross-platform .NET library for reading Excel data files.</Description>
<PackageTags>excel;xls;xlsx;xlsb;datareader</PackageTags>
<Nullable>enable</Nullable>
Expand Down
17 changes: 9 additions & 8 deletions source/Sylvan.Data.Excel/Xlsb/XlsbDataWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,13 @@ public int GetString(string str)
"hh:mm:ss",
};

const CompressionLevel Compression = CompressionLevel.Optimal;
CompressionLevel compression;

public XlsbDataWriter(Stream stream, ExcelDataWriterOptions options) : base(stream, options)
{
this.sharedStrings = new SharedStringTable();
this.zipArchive = new ZipArchive(stream, ZipArchiveMode.Create, true);
this.compression = options.CompressionLevel;
this.worksheets = new List<string>();
}

Expand Down Expand Up @@ -437,7 +438,7 @@ async Task<WriteResult> WriteInternal(DbDataReader data, string? worksheetName,
this.worksheets.Add(worksheetName);
var idx = this.worksheets.Count;
var entryName = "xl/worksheets/sheet" + idx + ".bin";
var entry = zipArchive.CreateEntry(entryName, Compression);
var entry = zipArchive.CreateEntry(entryName, compression);
using var es = entry.Open();
using var bs = new BufferedStream(es, 0x8000);
using var bw = new BinaryWriter(bs);
Expand Down Expand Up @@ -558,7 +559,7 @@ async Task<WriteResult> WriteInternal(DbDataReader data, string? worksheetName,

void WriteSharedStrings()
{
var e = this.zipArchive.CreateEntry("xl/sharedStrings.bin", Compression);
var e = this.zipArchive.CreateEntry("xl/sharedStrings.bin", compression);
using var s = e.Open();
using var bw = new BinaryWriter(s);

Expand All @@ -584,7 +585,7 @@ void WriteSharedStrings()
void WriteWorkbook()
{
var wbName = WorkbookPath;
var e = this.zipArchive.CreateEntry(wbName, Compression);
var e = this.zipArchive.CreateEntry(wbName, compression);

using var s = e.Open();
using var bw = new BinaryWriter(s);
Expand All @@ -605,7 +606,7 @@ void WritePkgMeta()
{
// pkg rels
{
var entry = zipArchive.CreateEntry("_rels/.rels", Compression);
var entry = zipArchive.CreateEntry("_rels/.rels", compression);
using var appStream = entry.Open();
using var xw = XmlWriter.Create(appStream, XmlSettings);
xw.WriteStartElement("Relationships", PkgRelNS);
Expand All @@ -629,7 +630,7 @@ void WritePkgMeta()

// workbook rels
{
var entry = zipArchive.CreateEntry("xl/_rels/workbook.bin.rels", Compression);
var entry = zipArchive.CreateEntry("xl/_rels/workbook.bin.rels", compression);
using var appStream = entry.Open();
using var xw = XmlWriter.Create(appStream, XmlSettings);
xw.WriteStartElement("Relationships", PkgRelNS);
Expand Down Expand Up @@ -661,7 +662,7 @@ void WritePkgMeta()

// content types
{
var entry = zipArchive.CreateEntry("[Content_Types].xml", Compression);
var entry = zipArchive.CreateEntry("[Content_Types].xml", compression);
using var appStream = entry.Open();
using var xw = XmlWriter.Create(appStream, XmlSettings);
xw.WriteStartElement("Types", ContentTypeNS);
Expand Down Expand Up @@ -706,7 +707,7 @@ static void Override(XmlWriter xw, string path, string type)

void WriteStyles()
{
var styleEntry = zipArchive.CreateEntry("xl/styles.bin", Compression);
var styleEntry = zipArchive.CreateEntry("xl/styles.bin", compression);
using var styleStream = styleEntry.Open();
using var bs = new BufferedStream(styleStream, 0x4000);
using var bw = new BinaryWriter(bs);
Expand Down
19 changes: 10 additions & 9 deletions source/Sylvan.Data.Excel/Xlsx/XlsxDataWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ public string GetString(string str)
"hh:mm:ss",
};

const CompressionLevel Compression = CompressionLevel.Optimal;
CompressionLevel compression;

public XlsxDataWriter(Stream stream, ExcelDataWriterOptions options) : base(stream, options)
{
this.sharedStrings = new SharedStringTable();
this.zipArchive = new ZipArchive(stream, ZipArchiveMode.Create, true);
this.compression = options.CompressionLevel;
this.worksheets = new List<string>();
}

Expand Down Expand Up @@ -136,7 +137,7 @@ async Task<WriteResult> WriteInternal(DbDataReader data, string? worksheetName,
this.worksheets.Add(worksheetName);
var idx = this.worksheets.Count;
var entryName = "xl/worksheets/sheet" + idx + ".xml";
var entry = zipArchive.CreateEntry(entryName, Compression);
var entry = zipArchive.CreateEntry(entryName, compression);
using var es = entry.Open();
using var xw = new StreamWriter(es, Encoding.UTF8, 0x4000);
xw.Write($"<worksheet xmlns=\"{NS}\">");
Expand Down Expand Up @@ -245,7 +246,7 @@ async Task<WriteResult> WriteInternal(DbDataReader data, string? worksheetName,

void WriteSharedStrings()
{
var e = this.zipArchive.CreateEntry("xl/sharedStrings.xml", Compression);
var e = this.zipArchive.CreateEntry("xl/sharedStrings.xml", compression);
using var s = e.Open();
using var xw = XmlWriter.Create(s, OpenPackaging.XmlSettings);
xw.WriteStartElement("sst", NS);
Expand Down Expand Up @@ -290,7 +291,7 @@ void WriteWorkbook()
{
var ns = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";
var wbName = "xl/workbook.xml";
var e = this.zipArchive.CreateEntry(wbName, Compression);
var e = this.zipArchive.CreateEntry(wbName, compression);

using var s = e.Open();
using var xw = XmlWriter.Create(s, OpenPackaging.XmlSettings);
Expand Down Expand Up @@ -324,7 +325,7 @@ void WriteWorkbook()

void WriteCoreProps()
{
var appEntry = zipArchive.CreateEntry("docProps/core.xml", Compression);
var appEntry = zipArchive.CreateEntry("docProps/core.xml", compression);
using var appStream = appEntry.Open();
using var xw = XmlWriter.Create(appStream, OpenPackaging.XmlSettings);
xw.WriteStartElement("coreProperties", CoreNS);
Expand All @@ -339,7 +340,7 @@ void WritePkgMeta()
{
// pkg rels
{
var entry = zipArchive.CreateEntry("_rels/.rels", Compression);
var entry = zipArchive.CreateEntry("_rels/.rels", compression);
using var appStream = entry.Open();
using var xw = XmlWriter.Create(appStream, OpenPackaging.XmlSettings);
xw.WriteStartElement("Relationships", PkgRelNS);
Expand All @@ -363,7 +364,7 @@ void WritePkgMeta()

// workbook rels
{
var entry = zipArchive.CreateEntry("xl/_rels/workbook.xml.rels", Compression);
var entry = zipArchive.CreateEntry("xl/_rels/workbook.xml.rels", compression);
using var appStream = entry.Open();
using var xw = XmlWriter.Create(appStream, OpenPackaging.XmlSettings);
xw.WriteStartElement("Relationships", PkgRelNS);
Expand Down Expand Up @@ -395,7 +396,7 @@ void WritePkgMeta()

// content types
{
var entry = zipArchive.CreateEntry("[Content_Types].xml", Compression);
var entry = zipArchive.CreateEntry("[Content_Types].xml", compression);
using var appStream = entry.Open();
using var xw = XmlWriter.Create(appStream, OpenPackaging.XmlSettings);
xw.WriteStartElement("Types", ContentTypeNS);
Expand Down Expand Up @@ -434,7 +435,7 @@ static void Override(XmlWriter xw, string path, string type)

void WriteStyles()
{
var appEntry = zipArchive.CreateEntry("xl/styles.xml", Compression);
var appEntry = zipArchive.CreateEntry("xl/styles.xml", compression);
using var appStream = appEntry.Open();
using var wx = XmlWriter.Create(appStream, OpenPackaging.XmlSettings);
wx.WriteStartElement("styleSheet", NS);
Expand Down

0 comments on commit 318192c

Please sign in to comment.