Skip to content

Commit

Permalink
Made the zip compression parametric.
Browse files Browse the repository at this point in the history
  • Loading branch information
Salvo Isaja committed Mar 7, 2021
1 parent c0c9a2c commit 7ee30be
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ The output is like:

The `XlsxWriter` class is the entry point for almost all functionality of the library. It is designed so that most of its methods can be chained to write the Excel file using a fluent syntax.\
Please note that an `XlsxWriter` object **must be disposed** to properly finalize the Excel file. Sandwitching its lifetime in a `using` statement is recommended.\
Pass the constructor a writeable `Stream` to save the Excel file into.
Pass the constructor a writeable `Stream` to save the Excel file into, and optionally the desired compression level of the underlying zip stream. The default `CompressionLevel.Level3` roughly matches file sizes produced by Excel.

```csharp
// class XlsxWriter
public XlsxWriter(Stream stream);
public XlsxWriter(
Stream stream,
SharpCompress.Compressors.Deflate.CompressionLevel compressionLevel = CompressionLevel.Level3);
```

The recipe is adding a worksheet with `BeginWorksheet`, adding a row with `BeginRow`, writing cells to that row with `Write`, and repeating as required. Rows and worksheets are implicitly finalized as soon as new rows or worksheets are added, or the `XlsxWriter` is disposed.
Expand Down
31 changes: 29 additions & 2 deletions examples/Examples/Large.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
using System;
/*
LargeXlsx - Minimalistic .net library to write large XLSX files
Copyright 2020-2021 Salvatore ISAJA. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using LargeXlsx;
using SharpCompress.Compressors.Deflate;

namespace Examples
{
Expand All @@ -15,7 +42,7 @@ public static void Run()
{
var stopwatch = Stopwatch.StartNew();
using (var stream = new FileStream($"{nameof(Large)}.xlsx", FileMode.Create, FileAccess.Write))
using (var xlsxWriter = new XlsxWriter(stream))
using (var xlsxWriter = new XlsxWriter(stream, CompressionLevel.Level3))
{
var whiteFont = new XlsxFont("Calibri", 11, Color.White, bold: true);
var blueFill = new XlsxFill(Color.FromArgb(0, 0x45, 0x86));
Expand Down
5 changes: 3 additions & 2 deletions examples/Examples/StyledLarge.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
LargeXlsx - Minimalistic .net library to write large XLSX files
Copyright 2020 Salvatore ISAJA. All rights reserved.
Copyright 2020-2021 Salvatore ISAJA. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -30,6 +30,7 @@ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
using System.IO;
using System.Linq;
using LargeXlsx;
using SharpCompress.Compressors.Deflate;

namespace Examples
{
Expand All @@ -44,7 +45,7 @@ public static void Run()
var rnd = new Random();
var stopwatch = Stopwatch.StartNew();
using (var stream = new FileStream($"{nameof(StyledLarge)}.xlsx", FileMode.Create, FileAccess.Write))
using (var xlsxWriter = new XlsxWriter(stream))
using (var xlsxWriter = new XlsxWriter(stream, CompressionLevel.Level3))
{
var headerStyle = new XlsxStyle(
new XlsxFont("Calibri", 11, Color.White, bold: true),
Expand Down
7 changes: 4 additions & 3 deletions src/LargeXlsx/XlsxWriter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
LargeXlsx - Minimalistic .net library to write large XLSX files
Copyright 2020 Salvatore ISAJA. All rights reserved.
Copyright 2020-2021 Salvatore ISAJA. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -30,6 +30,7 @@ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
using System.Linq;
using System.Text;
using SharpCompress.Common;
using SharpCompress.Compressors.Deflate;
using SharpCompress.Writers;
using SharpCompress.Writers.Zip;

Expand All @@ -51,13 +52,13 @@ public class XlsxWriter : IDisposable
public string GetRelativeColumnName(int offsetFromCurrentColumn) => Util.GetColumnName(CurrentColumnNumber + offsetFromCurrentColumn);
public static string GetColumnName(int columnIndex) => Util.GetColumnName(columnIndex);

public XlsxWriter(Stream stream)
public XlsxWriter(Stream stream, CompressionLevel compressionLevel = CompressionLevel.Level3)
{
_worksheets = new List<Worksheet>();
_stylesheet = new Stylesheet();
DefaultStyle = XlsxStyle.Default;

_zipWriter = (ZipWriter)WriterFactory.Open(stream, ArchiveType.Zip, new ZipWriterOptions(CompressionType.Deflate));
_zipWriter = (ZipWriter)WriterFactory.Open(stream, ArchiveType.Zip, new ZipWriterOptions(CompressionType.Deflate) { DeflateCompressionLevel = compressionLevel });
}

public void Dispose()
Expand Down

0 comments on commit 7ee30be

Please sign in to comment.