Initial commit of the ClosedXML library
Establishes the foundational structure, core Excel functionality, and tooling. - Implements base components for cells, ranges, columns, and rows. - Integrates a custom calculation engine with a wide range of Excel functions. - Adds comprehensive support for styling, conditional formatting, data validation, comments, pictures, and charts. - Configures build setup, project metadata (NuGet), and developer guidelines. - Includes editor and Git attributes for consistent code style and line endings.
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
using ClosedXML.Excel;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace ClosedXML_Tests.Excel.Styles
|
||||
{
|
||||
public class BorderTests
|
||||
{
|
||||
[Test]
|
||||
public void SetInsideBorderPreservesOutsideBorders()
|
||||
{
|
||||
using (var wb = new XLWorkbook())
|
||||
{
|
||||
var ws = wb.AddWorksheet();
|
||||
|
||||
ws.Cells("B2:C2").Style
|
||||
.Border.SetOutsideBorder(XLBorderStyleValues.Thin)
|
||||
.Border.SetOutsideBorderColor(XLColor.FromTheme(XLThemeColor.Accent1, 0.5));
|
||||
|
||||
//Check pre-conditions
|
||||
Assert.AreEqual(XLBorderStyleValues.Thin, ws.Cell("B2").Style.Border.LeftBorder);
|
||||
Assert.AreEqual(XLBorderStyleValues.Thin, ws.Cell("B2").Style.Border.RightBorder);
|
||||
Assert.AreEqual(XLThemeColor.Accent1, ws.Cell("B2").Style.Border.LeftBorderColor.ThemeColor);
|
||||
Assert.AreEqual(XLThemeColor.Accent1, ws.Cell("B2").Style.Border.RightBorderColor.ThemeColor);
|
||||
|
||||
ws.Range("B2:C2").Style.Border.SetInsideBorder(XLBorderStyleValues.None);
|
||||
|
||||
Assert.AreEqual(XLBorderStyleValues.Thin, ws.Cell("B2").Style.Border.LeftBorder);
|
||||
Assert.AreEqual(XLBorderStyleValues.None, ws.Cell("B2").Style.Border.RightBorder);
|
||||
Assert.AreEqual(XLBorderStyleValues.None, ws.Cell("C2").Style.Border.LeftBorder);
|
||||
Assert.AreEqual(XLBorderStyleValues.Thin, ws.Cell("C2").Style.Border.RightBorder);
|
||||
Assert.AreEqual(XLThemeColor.Accent1, ws.Cell("B2").Style.Border.LeftBorderColor.ThemeColor);
|
||||
Assert.AreEqual(XLThemeColor.Accent1, ws.Cell("C2").Style.Border.RightBorderColor.ThemeColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
using ClosedXML.Excel;
|
||||
using ClosedXML.Utils;
|
||||
using DocumentFormat.OpenXml.Spreadsheet;
|
||||
using NUnit.Framework;
|
||||
using Color = System.Drawing.Color;
|
||||
using X14 = DocumentFormat.OpenXml.Office2010.Excel;
|
||||
|
||||
namespace ClosedXML_Tests.Excel
|
||||
{
|
||||
/// <summary>
|
||||
/// Summary description for UnitTest1
|
||||
/// </summary>
|
||||
[TestFixture]
|
||||
public class ColorTests
|
||||
{
|
||||
[Test]
|
||||
public void ColorEqualOperatorInPlace()
|
||||
{
|
||||
Assert.IsTrue(XLColor.Black == XLColor.Black);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ColorNotEqualOperatorInPlace()
|
||||
{
|
||||
Assert.IsFalse(XLColor.Black != XLColor.Black);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ColorNamedVsHTML()
|
||||
{
|
||||
Assert.IsTrue(XLColor.Black == XLColor.FromHtml("#000000"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DefaultColorIndex64isTransparentWhite()
|
||||
{
|
||||
var wb = new XLWorkbook();
|
||||
IXLWorksheet ws = wb.AddWorksheet("Sheet1");
|
||||
XLColor color = ws.FirstCell().Style.Fill.BackgroundColor;
|
||||
Assert.AreEqual(XLColorType.Indexed, color.ColorType);
|
||||
Assert.AreEqual(64, color.Indexed);
|
||||
Assert.AreEqual(Color.Transparent, color.Color);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CanConvertXLColorToColorType()
|
||||
{
|
||||
var xlColor1 = XLColor.Red;
|
||||
var xlColor2 = XLColor.FromIndex(20);
|
||||
var xlColor3 = XLColor.FromTheme(XLThemeColor.Accent1);
|
||||
var xlColor4 = XLColor.FromTheme(XLThemeColor.Accent2, 0.4);
|
||||
|
||||
var color1 = new ForegroundColor().FromClosedXMLColor<ForegroundColor>(xlColor1);
|
||||
var color2 = new ForegroundColor().FromClosedXMLColor<ForegroundColor>(xlColor2);
|
||||
var color3 = new BackgroundColor().FromClosedXMLColor<BackgroundColor>(xlColor3);
|
||||
var color4 = new BackgroundColor().FromClosedXMLColor<BackgroundColor>(xlColor4);
|
||||
|
||||
Assert.AreEqual("FFFF0000", color1.Rgb.Value);
|
||||
Assert.IsNull(color1.Indexed);
|
||||
Assert.IsNull(color1.Theme);
|
||||
Assert.IsNull(color1.Tint);
|
||||
|
||||
Assert.IsNull(color2.Rgb);
|
||||
Assert.AreEqual(20, color2.Indexed.Value);
|
||||
Assert.IsNull(color2.Theme);
|
||||
Assert.IsNull(color2.Tint);
|
||||
|
||||
Assert.IsNull(color3.Rgb);
|
||||
Assert.IsNull(color3.Indexed);
|
||||
Assert.AreEqual(4, color3.Theme.Value);
|
||||
Assert.IsNull(color3.Tint);
|
||||
|
||||
Assert.IsNull(color4.Rgb);
|
||||
Assert.IsNull(color4.Indexed);
|
||||
Assert.AreEqual(5, color4.Theme.Value);
|
||||
Assert.AreEqual(0.4, color4.Tint.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CanConvertXlColorToX14ColorType()
|
||||
{
|
||||
var xlColor1 = XLColor.Red;
|
||||
var xlColor2 = XLColor.FromIndex(20);
|
||||
var xlColor3 = XLColor.FromTheme(XLThemeColor.Accent1);
|
||||
var xlColor4 = XLColor.FromTheme(XLThemeColor.Accent2, 0.4);
|
||||
|
||||
var color1 = new X14.AxisColor().FromClosedXMLColor<X14.AxisColor>(xlColor1);
|
||||
var color2 = new X14.BorderColor().FromClosedXMLColor<X14.BorderColor>(xlColor2);
|
||||
var color3 = new X14.FillColor().FromClosedXMLColor<X14.FillColor>(xlColor3);
|
||||
var color4 = new X14.HighMarkerColor().FromClosedXMLColor<X14.HighMarkerColor>(xlColor4);
|
||||
|
||||
Assert.AreEqual("FFFF0000", color1.Rgb.Value);
|
||||
Assert.IsNull(color1.Indexed);
|
||||
Assert.IsNull(color1.Theme);
|
||||
Assert.IsNull(color1.Tint);
|
||||
|
||||
Assert.IsNull(color2.Rgb);
|
||||
Assert.AreEqual(20, color2.Indexed.Value);
|
||||
Assert.IsNull(color2.Theme);
|
||||
Assert.IsNull(color2.Tint);
|
||||
|
||||
Assert.IsNull(color3.Rgb);
|
||||
Assert.IsNull(color3.Indexed);
|
||||
Assert.AreEqual(4, color3.Theme.Value);
|
||||
Assert.IsNull(color3.Tint);
|
||||
|
||||
Assert.IsNull(color4.Rgb);
|
||||
Assert.IsNull(color4.Indexed);
|
||||
Assert.AreEqual(5, color4.Theme.Value);
|
||||
Assert.AreEqual(0.4, color4.Tint.Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CanConvertColorTypeToXlColor()
|
||||
{
|
||||
var color1 = new ForegroundColor { Rgb = new DocumentFormat.OpenXml.HexBinaryValue("FFFF0000") };
|
||||
var color2 = new ForegroundColor { Indexed = new DocumentFormat.OpenXml.UInt32Value((uint)20) };
|
||||
var color3 = new BackgroundColor { Theme = new DocumentFormat.OpenXml.UInt32Value((uint)4) };
|
||||
var color4 = new BackgroundColor
|
||||
{
|
||||
Theme = new DocumentFormat.OpenXml.UInt32Value((uint)4),
|
||||
Tint = new DocumentFormat.OpenXml.DoubleValue(0.4)
|
||||
};
|
||||
|
||||
var xlColor1 = color1.ToClosedXMLColor();
|
||||
var xlColor2 = color2.ToClosedXMLColor();
|
||||
var xlColor3 = color3.ToClosedXMLColor();
|
||||
var xlColor4 = color4.ToClosedXMLColor();
|
||||
|
||||
Assert.AreEqual(XLColorType.Color, xlColor1.ColorType);
|
||||
Assert.AreEqual(XLColor.Red.Color, xlColor1.Color);
|
||||
|
||||
Assert.AreEqual(XLColorType.Indexed, xlColor2.ColorType);
|
||||
Assert.AreEqual(20, xlColor2.Indexed);
|
||||
|
||||
Assert.AreEqual(XLColorType.Theme, xlColor3.ColorType);
|
||||
Assert.AreEqual(XLThemeColor.Accent1, xlColor3.ThemeColor);
|
||||
Assert.AreEqual(0, xlColor3.ThemeTint, XLHelper.Epsilon);
|
||||
|
||||
Assert.AreEqual(XLColorType.Theme, xlColor4.ColorType);
|
||||
Assert.AreEqual(XLThemeColor.Accent1, xlColor4.ThemeColor);
|
||||
Assert.AreEqual(0.4, xlColor4.ThemeTint, XLHelper.Epsilon);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CanConvertX14ColorTypeToXlColor()
|
||||
{
|
||||
var color1 = new X14.AxisColor { Rgb = new DocumentFormat.OpenXml.HexBinaryValue("FFFF0000") };
|
||||
var color2 = new X14.BorderColor { Indexed = new DocumentFormat.OpenXml.UInt32Value((uint)20) };
|
||||
var color3 = new X14.FillColor { Theme = new DocumentFormat.OpenXml.UInt32Value((uint)4) };
|
||||
var color4 = new X14.HighMarkerColor
|
||||
{
|
||||
Theme = new DocumentFormat.OpenXml.UInt32Value((uint)4),
|
||||
Tint = new DocumentFormat.OpenXml.DoubleValue(0.4)
|
||||
};
|
||||
|
||||
var xlColor1 = color1.ToClosedXMLColor();
|
||||
var xlColor2 = color2.ToClosedXMLColor();
|
||||
var xlColor3 = color3.ToClosedXMLColor();
|
||||
var xlColor4 = color4.ToClosedXMLColor();
|
||||
|
||||
Assert.AreEqual(XLColorType.Color, xlColor1.ColorType);
|
||||
Assert.AreEqual(XLColor.Red.Color, xlColor1.Color);
|
||||
|
||||
Assert.AreEqual(XLColorType.Indexed, xlColor2.ColorType);
|
||||
Assert.AreEqual(20, xlColor2.Indexed);
|
||||
|
||||
Assert.AreEqual(XLColorType.Theme, xlColor3.ColorType);
|
||||
Assert.AreEqual(XLThemeColor.Accent1, xlColor3.ThemeColor);
|
||||
Assert.AreEqual(0, xlColor3.ThemeTint, XLHelper.Epsilon);
|
||||
|
||||
Assert.AreEqual(XLColorType.Theme, xlColor4.ColorType);
|
||||
Assert.AreEqual(XLThemeColor.Accent1, xlColor4.ThemeColor);
|
||||
Assert.AreEqual(0.4, xlColor4.ThemeTint, XLHelper.Epsilon);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using ClosedXML.Excel;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace ClosedXML_Tests.Excel.Styles
|
||||
{
|
||||
public class FontTests
|
||||
{
|
||||
[Test]
|
||||
public void XLFontKey_GetHashCode_IsCaseInsensitive()
|
||||
{
|
||||
var fontKey1 = new XLFontKey { FontName = "Arial" };
|
||||
var fontKey2 = new XLFontKey { FontName = "Times New Roman" };
|
||||
var fontKey3 = new XLFontKey { FontName = "TIMES NEW ROMAN" };
|
||||
|
||||
Assert.AreNotEqual(fontKey1.GetHashCode(), fontKey2.GetHashCode());
|
||||
Assert.AreEqual(fontKey2.GetHashCode(), fontKey3.GetHashCode());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void XLFontKey_Equals_IsCaseInsensitive()
|
||||
{
|
||||
var fontKey1 = new XLFontKey { FontName = "Arial" };
|
||||
var fontKey2 = new XLFontKey { FontName = "Times New Roman" };
|
||||
var fontKey3 = new XLFontKey { FontName = "TIMES NEW ROMAN" };
|
||||
|
||||
Assert.IsFalse(fontKey1.Equals(fontKey2));
|
||||
Assert.IsTrue(fontKey2.Equals(fontKey3));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using ClosedXML.Excel;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace ClosedXML_Tests.Excel
|
||||
{
|
||||
public class NumberFormatTests
|
||||
{
|
||||
[Test]
|
||||
public void PreserveCellFormat()
|
||||
{
|
||||
using (var wb = new XLWorkbook())
|
||||
{
|
||||
var ws = wb.AddWorksheet("Sheet1");
|
||||
|
||||
var table = new DataTable();
|
||||
table.Columns.Add("Date", typeof(DateTime));
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
table.Rows.Add(new DateTime(2017, 1, 1).AddMonths(i));
|
||||
}
|
||||
|
||||
ws.Column(1).Style.NumberFormat.Format = "yy-MM-dd";
|
||||
ws.Cell("A1").InsertData(table);
|
||||
Assert.AreEqual("yy-MM-dd", ws.Cell("A5").Style.DateFormat.Format);
|
||||
|
||||
ws.Row(1).Style.NumberFormat.Format = "yy-MM-dd";
|
||||
ws.Cell("A1").InsertData(table.Rows, true);
|
||||
Assert.AreEqual("yy-MM-dd", ws.Cell("E1").Style.DateFormat.Format);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestExcelNumberFormats()
|
||||
{
|
||||
using (var wb = new XLWorkbook())
|
||||
{
|
||||
var ws = wb.AddWorksheet("Sheet1");
|
||||
var c = ws.FirstCell()
|
||||
.SetValue(41573.875)
|
||||
.SetDataType(XLDataType.DateTime);
|
||||
|
||||
c.Style.NumberFormat.SetFormat("m/d/yy\\ h:mm;@");
|
||||
|
||||
Assert.AreEqual("10/26/13 21:00", c.GetFormattedString());
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadAndWriteColumnNumberFormat()
|
||||
{
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
using (var wb = new XLWorkbook())
|
||||
{
|
||||
var ws = wb.AddWorksheet();
|
||||
var sourceColumn = ws.Column(1);
|
||||
sourceColumn.Style.NumberFormat.Format = "0.000";
|
||||
wb.SaveAs(memoryStream);
|
||||
}
|
||||
|
||||
memoryStream.Position = 0;
|
||||
|
||||
using (var wb = new XLWorkbook(memoryStream))
|
||||
{
|
||||
var column = wb.Worksheets.Single().Column(1);
|
||||
Assert.AreEqual("0.000", column.Style.NumberFormat.Format);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void XLNumberFormatKey_GetHashCode_IsCaseSensitive()
|
||||
{
|
||||
var numberFormatKey1 = new XLNumberFormatKey { Format = "MM" };
|
||||
var numberFormatKey2 = new XLNumberFormatKey { Format = "mm" };
|
||||
|
||||
Assert.AreNotEqual(numberFormatKey1.GetHashCode(), numberFormatKey2.GetHashCode());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void XLNumberFormatKey_Equals_IsCaseSensitive()
|
||||
{
|
||||
var numberFormatKey1 = new XLNumberFormatKey { Format = "MM" };
|
||||
var numberFormatKey2 = new XLNumberFormatKey { Format = "mm" };
|
||||
|
||||
Assert.IsFalse(numberFormatKey1.Equals(numberFormatKey2));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
using ClosedXML.Excel;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ClosedXML_Tests.Excel.Styles
|
||||
{
|
||||
[TestFixture]
|
||||
public class StyleChangeTests
|
||||
{
|
||||
[Test]
|
||||
public void ChangeFontColorDoesNotAffectOtherProperties()
|
||||
{
|
||||
using (var wb = new XLWorkbook())
|
||||
{
|
||||
// Arrange
|
||||
var ws = wb.AddWorksheet("Sheet1");
|
||||
var a1 = ws.Cell("A1");
|
||||
var a2 = ws.Cell("A2");
|
||||
var b1 = ws.Cell("B1");
|
||||
var b2 = ws.Cell("B2");
|
||||
|
||||
ws.Range("A1:B2").Value = "Test";
|
||||
|
||||
a1.Style.Fill.BackgroundColor = XLColor.Red;
|
||||
a2.Style.Fill.BackgroundColor = XLColor.Green;
|
||||
b1.Style.Fill.BackgroundColor = XLColor.Blue;
|
||||
b2.Style.Fill.BackgroundColor = XLColor.Pink;
|
||||
|
||||
a1.Style.Font.FontName = "Arial";
|
||||
a2.Style.Font.FontName = "Times New Roman";
|
||||
b1.Style.Font.FontName = "Calibri";
|
||||
b2.Style.Font.FontName = "Cambria";
|
||||
|
||||
// Act
|
||||
ws.Range("A1:B2").Style.Font.FontColor = XLColor.PowderBlue;
|
||||
|
||||
//Assert
|
||||
Assert.AreEqual(XLColor.Red, ws.Cell("A1").Style.Fill.BackgroundColor);
|
||||
Assert.AreEqual(XLColor.Green, ws.Cell("A2").Style.Fill.BackgroundColor);
|
||||
Assert.AreEqual(XLColor.Blue, ws.Cell("B1").Style.Fill.BackgroundColor);
|
||||
Assert.AreEqual(XLColor.Pink, ws.Cell("B2").Style.Fill.BackgroundColor);
|
||||
|
||||
Assert.AreEqual("Arial", ws.Cell("A1").Style.Font.FontName);
|
||||
Assert.AreEqual("Times New Roman", ws.Cell("A2").Style.Font.FontName);
|
||||
Assert.AreEqual("Calibri", ws.Cell("B1").Style.Font.FontName);
|
||||
Assert.AreEqual("Cambria", ws.Cell("B2").Style.Font.FontName);
|
||||
|
||||
Assert.AreEqual(XLColor.PowderBlue, ws.Cell("A1").Style.Font.FontColor);
|
||||
Assert.AreEqual(XLColor.PowderBlue, ws.Cell("A2").Style.Font.FontColor);
|
||||
Assert.AreEqual(XLColor.PowderBlue, ws.Cell("B1").Style.Font.FontColor);
|
||||
Assert.AreEqual(XLColor.PowderBlue, ws.Cell("B2").Style.Font.FontColor);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void ChangeDetachedStyleAlignment()
|
||||
{
|
||||
var style = XLStyle.Default;
|
||||
|
||||
style.Alignment.Horizontal = XLAlignmentHorizontalValues.Justify;
|
||||
|
||||
Assert.AreEqual(XLAlignmentHorizontalValues.Justify, style.Alignment.Horizontal);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ChangeDetachedStyleBorder()
|
||||
{
|
||||
var style = XLStyle.Default;
|
||||
|
||||
style.Border.DiagonalBorder = XLBorderStyleValues.Double;
|
||||
|
||||
Assert.AreEqual(XLBorderStyleValues.Double, style.Border.DiagonalBorder);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ChangeDetachedStyleFill()
|
||||
{
|
||||
var style = XLStyle.Default;
|
||||
|
||||
style.Fill.BackgroundColor = XLColor.Red;
|
||||
|
||||
Assert.AreEqual(XLColor.Red, style.Fill.BackgroundColor);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void ChangeDetachedStyleFont()
|
||||
{
|
||||
var style = XLStyle.Default;
|
||||
|
||||
style.Font.FontSize = 50;
|
||||
|
||||
Assert.AreEqual(50, style.Font.FontSize);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void ChangeDetachedStyleNumberFormat()
|
||||
{
|
||||
var style = XLStyle.Default;
|
||||
|
||||
style.NumberFormat.Format = "YYYY";
|
||||
|
||||
Assert.AreEqual("YYYY", style.NumberFormat.Format);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ChangeDetachedStyleProtection()
|
||||
{
|
||||
var style = XLStyle.Default;
|
||||
|
||||
style.Protection.Hidden = true;
|
||||
|
||||
Assert.AreEqual(true, style.Protection.Hidden);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ChangeAttachedStyleAlignment()
|
||||
{
|
||||
using (var wb = new XLWorkbook())
|
||||
{
|
||||
var ws = wb.AddWorksheet("Sheet1");
|
||||
var a1 = ws.Cell("A1");
|
||||
|
||||
a1.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Justify;
|
||||
|
||||
Assert.AreEqual(XLAlignmentHorizontalValues.Justify, a1.Style.Alignment.Horizontal);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ClosedXML.Excel;
|
||||
using NUnit.Framework;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace ClosedXML_Tests.Excel
|
||||
{
|
||||
[TestFixture]
|
||||
public class StyleTests
|
||||
{
|
||||
[Test]
|
||||
public void EmptyCellWithQuotePrefixNotTreatedAsEmpty()
|
||||
{
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
using (var wb = new XLWorkbook())
|
||||
{
|
||||
var ws = wb.AddWorksheet("Sheet1");
|
||||
ws.FirstCell().SetValue("Empty cell with quote prefix:");
|
||||
var cell = ws.FirstCell().CellRight() as XLCell;
|
||||
|
||||
Assert.IsTrue(cell.IsEmpty());
|
||||
cell.Style.IncludeQuotePrefix = true;
|
||||
|
||||
Assert.IsTrue(cell.IsEmpty());
|
||||
Assert.IsFalse(cell.IsEmpty(XLCellsUsedOptions.All));
|
||||
|
||||
wb.SaveAs(ms);
|
||||
}
|
||||
|
||||
ms.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
using (var wb = new XLWorkbook(ms))
|
||||
{
|
||||
var ws = wb.Worksheets.First();
|
||||
var cell = ws.FirstCell().CellRight() as XLCell;
|
||||
Assert.AreEqual(1, cell.SharedStringId);
|
||||
|
||||
Assert.IsTrue(cell.IsEmpty());
|
||||
Assert.IsFalse(cell.IsEmpty(XLCellsUsedOptions.All));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[TestCase("A1", TestName = "First cell")]
|
||||
[TestCase("A2", TestName = "Cell from initialized row")]
|
||||
[TestCase("B1", TestName = "Cell from initialized column")]
|
||||
[TestCase("D4", TestName = "Initialized cell")]
|
||||
[TestCase("F6", TestName = "Non-initialized cell")]
|
||||
public void CellTakesWorksheetStyle(string cellAddress)
|
||||
{
|
||||
using (var wb = new XLWorkbook())
|
||||
{
|
||||
var ws = wb.AddWorksheet("Sheet1");
|
||||
ws.Column(2);
|
||||
ws.Row(2);
|
||||
ws.Cell("D4").Value = "Non empty";
|
||||
ws.Style.Font.SetFontName("Arial");
|
||||
ws.Style.Font.SetFontSize(9);
|
||||
|
||||
var cell = ws.Cell(cellAddress);
|
||||
Assert.AreEqual("Arial", cell.Style.Font.FontName);
|
||||
Assert.AreEqual(9, cell.Style.Font.FontSize);
|
||||
}
|
||||
}
|
||||
|
||||
[TestCaseSource(nameof(StylizedEntities))]
|
||||
public void WorksheetStyleAffectsAllNestedEntities(Func<IXLWorksheet, IXLStyle> getEntityStyle)
|
||||
{
|
||||
using (var wb = new XLWorkbook())
|
||||
{
|
||||
var ws = wb.AddWorksheet();
|
||||
|
||||
ws.Style.Font.FontSize = 8;
|
||||
|
||||
var style = getEntityStyle(ws);
|
||||
|
||||
Assert.AreEqual(8, style.Font.FontSize);
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<TestCaseData> StylizedEntities
|
||||
{
|
||||
get
|
||||
{
|
||||
var t = nameof(WorksheetStyleAffectsAllNestedEntities);
|
||||
yield return new TestCaseData(new Func<IXLWorksheet, IXLStyle>((ws) => ws.Style)).SetName(t + ": Worksheet");
|
||||
|
||||
yield return new TestCaseData(new Func<IXLWorksheet, IXLStyle>((ws) => ws.Columns().Style)).SetName(t + ": Columns()");
|
||||
yield return new TestCaseData(new Func<IXLWorksheet, IXLStyle>((ws) => ws.Columns(1, 3).Style)).SetName(t + ": Columns(1, 3)");
|
||||
yield return new TestCaseData(new Func<IXLWorksheet, IXLStyle>((ws) => ws.Columns("B:F").Style)).SetName(t + ": Columns(\"B:F\")");
|
||||
yield return new TestCaseData(new Func<IXLWorksheet, IXLStyle>((ws) => ws.Columns("B", "F").Style)).SetName(t + ": Columns(\"B\", \"F\")");
|
||||
yield return new TestCaseData(new Func<IXLWorksheet, IXLStyle>((ws) => ws.Column(5).Style)).SetName(t + ": Column(5)");
|
||||
yield return new TestCaseData(new Func<IXLWorksheet, IXLStyle>((ws) => ws.Column("D").Style)).SetName(t + ": Column(\"D\")");
|
||||
|
||||
yield return new TestCaseData(new Func<IXLWorksheet, IXLStyle>((ws) => ws.Rows().Style)).SetName(t + ": Rows()");
|
||||
yield return new TestCaseData(new Func<IXLWorksheet, IXLStyle>((ws) => ws.Rows(1, 3).Style)).SetName(t + ": Rows(1, 3)");
|
||||
yield return new TestCaseData(new Func<IXLWorksheet, IXLStyle>((ws) => ws.Rows("1:3").Style)).SetName(t + ": Rows(\"1:3\")");
|
||||
yield return new TestCaseData(new Func<IXLWorksheet, IXLStyle>((ws) => ws.Row(5).Style)).SetName(t + ": Row(5)");
|
||||
|
||||
yield return new TestCaseData(new Func<IXLWorksheet, IXLStyle>((ws) => ws.Cells().Style)).SetName(t + ": Cells()");
|
||||
yield return new TestCaseData(new Func<IXLWorksheet, IXLStyle>((ws) => ws.Cells("B2,D4").Style)).SetName(t + ": Cells(\"B2, D4\")");
|
||||
yield return new TestCaseData(new Func<IXLWorksheet, IXLStyle>((ws) => ws.Cell("F6").Style)).SetName(t + ": Cell(\"F6\")");
|
||||
yield return new TestCaseData(new Func<IXLWorksheet, IXLStyle>((ws) => ws.Cell(2, 3).Style)).SetName(t + ": Cell(2, 3)");
|
||||
|
||||
yield return new TestCaseData(new Func<IXLWorksheet, IXLStyle>((ws) => ws.Ranges("F6:H9,I8:K10").Style)).SetName(t + ": Ranges(\"F6:H9,I8:K10\")");
|
||||
yield return new TestCaseData(new Func<IXLWorksheet, IXLStyle>((ws) => ws.Range("G8:H10").Style)).SetName(t + ": Range(\"G8:H10\")");
|
||||
yield return new TestCaseData(new Func<IXLWorksheet, IXLStyle>((ws) => ws.Range("G8:H10").Column(1).Style)).SetName(t + ": Range(\"G8:H10\").Column(1)");
|
||||
yield return new TestCaseData(new Func<IXLWorksheet, IXLStyle>((ws) => ws.Range("G8:H10").Row(2).Style)).SetName(t + ": Range(\"G8:H10\").Row(2)");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
using ClosedXML.Excel;
|
||||
using NUnit.Framework;
|
||||
using System.IO;
|
||||
|
||||
namespace ClosedXML_Tests.Excel
|
||||
{
|
||||
/// <summary>
|
||||
/// Summary description for UnitTest1
|
||||
/// </summary>
|
||||
[TestFixture]
|
||||
public class XLFillTests
|
||||
{
|
||||
[Test]
|
||||
public void BackgroundColorSetsPattern()
|
||||
{
|
||||
var fill = new XLFill { BackgroundColor = XLColor.Blue };
|
||||
Assert.AreEqual(XLFillPatternValues.Solid, fill.PatternType);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BackgroundNoColorSetsPatternNone()
|
||||
{
|
||||
var fill = new XLFill { BackgroundColor = XLColor.NoColor };
|
||||
Assert.AreEqual(XLFillPatternValues.None, fill.PatternType);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BackgroundPatternEqualCheck()
|
||||
{
|
||||
var fill1 = new XLFill { BackgroundColor = XLColor.Blue };
|
||||
var fill2 = new XLFill { BackgroundColor = XLColor.Blue };
|
||||
Assert.IsTrue(fill1.Equals(fill2));
|
||||
Assert.AreEqual(fill1.GetHashCode(), fill2.GetHashCode());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BackgroundPatternNotEqualCheck()
|
||||
{
|
||||
var fill1 = new XLFill { PatternType = XLFillPatternValues.Solid, BackgroundColor = XLColor.Blue };
|
||||
var fill2 = new XLFill { PatternType = XLFillPatternValues.Solid, BackgroundColor = XLColor.Red };
|
||||
Assert.IsFalse(fill1.Equals(fill2));
|
||||
Assert.AreNotEqual(fill1.GetHashCode(), fill2.GetHashCode());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FillsWithTransparentColorEqual()
|
||||
{
|
||||
var fill1 = new XLFill { BackgroundColor = XLColor.ElectricUltramarine, PatternType = XLFillPatternValues.None };
|
||||
var fill2 = new XLFill { BackgroundColor = XLColor.EtonBlue, PatternType = XLFillPatternValues.None };
|
||||
var fill3 = new XLFill { BackgroundColor = XLColor.FromIndex(64) };
|
||||
var fill4 = new XLFill { BackgroundColor = XLColor.NoColor };
|
||||
|
||||
Assert.IsTrue(fill1.Equals(fill2));
|
||||
Assert.IsTrue(fill1.Equals(fill3));
|
||||
Assert.IsTrue(fill1.Equals(fill4));
|
||||
Assert.AreEqual(fill1.GetHashCode(), fill2.GetHashCode());
|
||||
Assert.AreEqual(fill1.GetHashCode(), fill3.GetHashCode());
|
||||
Assert.AreEqual(fill1.GetHashCode(), fill4.GetHashCode());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SolidFillsWithDifferentPatternColorEqual()
|
||||
{
|
||||
var fill1 = new XLFill
|
||||
{
|
||||
PatternType = XLFillPatternValues.Solid,
|
||||
BackgroundColor = XLColor.Red,
|
||||
PatternColor = XLColor.Blue
|
||||
};
|
||||
|
||||
var fill2 = new XLFill {
|
||||
PatternType = XLFillPatternValues.Solid,
|
||||
BackgroundColor = XLColor.Red,
|
||||
PatternColor = XLColor.Green
|
||||
};
|
||||
|
||||
Assert.IsTrue(fill1.Equals(fill2));
|
||||
Assert.AreEqual(fill1.GetHashCode(), fill2.GetHashCode());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BackgroundWithConditionalFormat()
|
||||
{
|
||||
var workbook = new XLWorkbook();
|
||||
var worksheet = workbook.AddWorksheet("Test");
|
||||
worksheet.Cell(2, 2).SetValue("Text");
|
||||
var cf = worksheet.Cell(2, 2).AddConditionalFormat();
|
||||
var style = cf.WhenNotBlank();
|
||||
style = style
|
||||
.Border.SetOutsideBorder(XLBorderStyleValues.Thick)
|
||||
.Border.SetOutsideBorderColor(XLColor.Blue);
|
||||
|
||||
Assert.AreEqual(style.Border.BottomBorder, XLBorderStyleValues.Thick);
|
||||
Assert.AreEqual(style.Border.TopBorder, XLBorderStyleValues.Thick);
|
||||
Assert.AreEqual(style.Border.LeftBorder, XLBorderStyleValues.Thick);
|
||||
Assert.AreEqual(style.Border.RightBorder, XLBorderStyleValues.Thick);
|
||||
|
||||
Assert.AreEqual(style.Border.BottomBorderColor, XLColor.Blue);
|
||||
Assert.AreEqual(style.Border.TopBorderColor, XLColor.Blue);
|
||||
Assert.AreEqual(style.Border.LeftBorderColor, XLColor.Blue);
|
||||
Assert.AreEqual(style.Border.RightBorderColor, XLColor.Blue);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void LoadAndSaveTransparentBackgroundFill()
|
||||
{
|
||||
using (var stream = TestHelper.GetStreamFromResource(TestHelper.GetResourcePath(@"Other\StyleReferenceFiles\TransparentBackgroundFill\inputfile.xlsx")))
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
TestHelper.CreateAndCompare(() =>
|
||||
{
|
||||
var wb = new XLWorkbook(stream);
|
||||
wb.SaveAs(ms);
|
||||
return wb;
|
||||
}, @"Other\StyleReferenceFiles\TransparentBackgroundFill\TransparentBackgroundFill.xlsx");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user