Skip to content

Commit

Permalink
Added row height and formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Salvo Isaja committed May 4, 2020
1 parent 20852d1 commit 972e2a6
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN

namespace Examples
{
public static class Columns
public static class ColumnFormatting
{
public static void Run()
{
var rnd = new Random();
using (var stream = new FileStream($"{nameof(Columns)}.xlsx", FileMode.Create, FileAccess.Write))
using (var stream = new FileStream($"{nameof(ColumnFormatting)}.xlsx", FileMode.Create, FileAccess.Write))
using (var xlsxWriter = new XlsxWriter(stream))
{
var blueStyle = new XlsxStyle(
Expand Down
3 changes: 2 additions & 1 deletion examples/Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ public static void Main(string[] args)
{
Simple.Run();
NumberFormats.Run();
Columns.Run();
ColumnFormatting.Run();
RowFormatting.Run();
Large.Run();
StyledLarge.Run();
}
Expand Down
56 changes: 56 additions & 0 deletions examples/Examples/RowFormatting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
LargeXlsx - Minimalistic .net library to write large XLSX files
Copyright 2020 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.Drawing;
using System.IO;
using LargeXlsx;

namespace Examples
{
public static class RowFormatting
{
public static void Run()
{
using (var stream = new FileStream($"{nameof(RowFormatting)}.xlsx", FileMode.Create, FileAccess.Write))
using (var xlsxWriter = new XlsxWriter(stream))
{
var blueStyle = new XlsxStyle(
new XlsxFont(XlsxFont.Default.FontName, XlsxFont.Default.FontSize, Color.White),
new XlsxFill(XlsxFill.Pattern.Solid, Color.FromArgb(0, 0x45, 0x86)),
XlsxBorder.None,
XlsxNumberFormat.General);

xlsxWriter
.BeginWorksheet("Sheet 1")
.BeginRow().Write("A1").Write("B1").Write("C1")
.BeginRow(hidden: true).Write("A2").Write("B2").Write("C2")
.BeginRow().Write("A3").Write("B3").Write("C3")
.BeginRow(height: 36.5).Write("A4").Write("B4").Write("C4")
.BeginRow(style: blueStyle).Write("A5").SkipColumns(1).Write("C5");
}
}
}
}
28 changes: 27 additions & 1 deletion src/LargeXlsx/Util.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
using System;
/*
LargeXlsx - Minimalistic .net library to write large XLSX files
Copyright 2020 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.Text;

namespace LargeXlsx
Expand Down
8 changes: 6 additions & 2 deletions src/LargeXlsx/Worksheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,16 @@ public void Dispose()
_stream.Dispose();
}

public void BeginRow()
public void BeginRow(double? height, bool hidden, XlsxStyle style)
{
CloseLastRow();
CurrentRowNumber++;
CurrentColumnNumber = 1;
_streamWriter.Write("<row r=\"{0}\">", CurrentRowNumber);
_streamWriter.Write("<row r=\"{0}\"", CurrentRowNumber);
if (height.HasValue) _streamWriter.Write(" ht=\"{0}\" customHeight=\"1\"", height);
if (hidden) _streamWriter.Write(" hidden=\"1\"");
if (style != null) _streamWriter.Write(" s=\"{0}\" customFormat=\"1\"", _stylesheet.ResolveStyleId(style));
_streamWriter.Write(">");
}

public void SkipRows(int rowCount)
Expand Down
28 changes: 27 additions & 1 deletion src/LargeXlsx/XlsxColumn.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
namespace LargeXlsx
/*
LargeXlsx - Minimalistic .net library to write large XLSX files
Copyright 2020 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.
*/
namespace LargeXlsx
{
public class XlsxColumn
{
Expand Down
4 changes: 2 additions & 2 deletions src/LargeXlsx/XlsxWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ public XlsxWriter SkipRows(int rowCount)
return DoOnWorksheet(() => _currentWorksheet.SkipRows(rowCount));
}

public XlsxWriter BeginRow()
public XlsxWriter BeginRow(double? height = null, bool hidden = false, XlsxStyle style = null)
{
return DoOnWorksheet(() => _currentWorksheet.BeginRow());
return DoOnWorksheet(() => _currentWorksheet.BeginRow(height, hidden, style));
}

public XlsxWriter SkipColumns(int columnCount)
Expand Down
75 changes: 75 additions & 0 deletions tests/LargeXlsx.Tests/XlsxWriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,80 @@ public void MultipleSheets()
}
}
}

[Test]
public static void ColumnFormatting()
{
using (var stream = new MemoryStream())
{
using (var xlsxWriter = new XlsxWriter(stream))
{
var blueStyle = new XlsxStyle(
new XlsxFont(XlsxFont.Default.FontName, XlsxFont.Default.FontSize, Color.White),
new XlsxFill(XlsxFill.Pattern.Solid, Color.FromArgb(0, 0x45, 0x86)),
XlsxBorder.None,
XlsxNumberFormat.General);

xlsxWriter
.BeginWorksheet("Sheet 1", columns: new[]
{
XlsxColumn.Formatted(count: 2, width: 20),
XlsxColumn.Unformatted(3),
XlsxColumn.Formatted(style: blueStyle, width: 9),
XlsxColumn.Formatted(hidden: true, width: 0)
});
}

using (var package = new ExcelPackage(stream))
{
package.Workbook.Worksheets.Count.Should().Be(1);
var sheet = package.Workbook.Worksheets[0];

sheet.Column(1).Width = 20;
sheet.Column(2).Width = 20;
sheet.Column(6).Style.Fill.PatternType.Should().Be(ExcelFillStyle.Solid);
sheet.Column(6).Style.Fill.BackgroundColor.Rgb.Should().Be("004586");
sheet.Column(6).Style.Font.Color.Rgb.Should().Be("ffffff");
sheet.Column(7).Hidden = true;
sheet.Column(7).Width = 0;
}
}
}

[Test]
public void RowFormatting()
{
using (var stream = new MemoryStream())
{
using (var xlsxWriter = new XlsxWriter(stream))
{
var blueStyle = new XlsxStyle(
new XlsxFont(XlsxFont.Default.FontName, XlsxFont.Default.FontSize, Color.White),
new XlsxFill(XlsxFill.Pattern.Solid, Color.FromArgb(0, 0x45, 0x86)),
XlsxBorder.None,
XlsxNumberFormat.General);

xlsxWriter
.BeginWorksheet("Sheet 1")
.BeginRow().Write("A1").Write("B1").Write("C1")
.BeginRow(hidden: true).Write("A2").Write("B2").Write("C2")
.BeginRow().Write("A3").Write("B3").Write("C3")
.BeginRow(height: 36.5).Write("A4").Write("B4").Write("C4")
.BeginRow(style: blueStyle).Write("A5").SkipColumns(1).Write("C5");
}

using (var package = new ExcelPackage(stream))
{
package.Workbook.Worksheets.Count.Should().Be(1);
var sheet = package.Workbook.Worksheets[0];

sheet.Row(2).Hidden = true;
sheet.Row(4).Height = 36.5;
sheet.Row(5).Style.Fill.PatternType.Should().Be(ExcelFillStyle.Solid);
sheet.Row(5).Style.Fill.BackgroundColor.Rgb.Should().Be("004586");
sheet.Row(5).Style.Font.Color.Rgb.Should().Be("ffffff");
}
}
}
}
}

0 comments on commit 972e2a6

Please sign in to comment.