diff --git a/ClosedXML/ClosedXML/ClosedXML/ClosedXML.csproj b/ClosedXML/ClosedXML/ClosedXML/ClosedXML.csproj index 72c7c7c..611cd20 100644 --- a/ClosedXML/ClosedXML/ClosedXML/ClosedXML.csproj +++ b/ClosedXML/ClosedXML/ClosedXML/ClosedXML.csproj @@ -16,6 +16,7 @@ SAK SAK SAK + true diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/IXLRange.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/IXLRange.cs index 2819147..acfe46e 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/IXLRange.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/IXLRange.cs @@ -65,16 +65,16 @@ return range.LastCellAddress.Column - range.FirstCellAddress.Column + 1; } - public static XLRange Range(this IXLRange range, String rangeAddress) + public static IXLRange Range(this IXLRange range, String rangeAddress) { String[] arrRange = rangeAddress.Split(':'); return range.Range(arrRange[0], arrRange[1]); } - public static XLRange Range(this IXLRange range, String firstCellAddress, String lastCellAddress) + public static IXLRange Range(this IXLRange range, String firstCellAddress, String lastCellAddress) { return range.Range(new XLAddress(firstCellAddress), new XLAddress(lastCellAddress)); } - public static XLRange Range(this IXLRange range, IXLAddress firstCellAddress, IXLAddress lastCellAddress) + public static IXLRange Range(this IXLRange range, IXLAddress firstCellAddress, IXLAddress lastCellAddress) { return new XLRange( new XLRangeParameters() @@ -86,12 +86,11 @@ } ); } - public static XLRange Range(this IXLRange range, IXLCell firstCell, IXLCell lastCell) + public static IXLRange Range(this IXLRange range, IXLCell firstCell, IXLCell lastCell) { return range.Range(firstCell.Address, lastCell.Address); } - public static IEnumerable Cells(this IXLRange range) { foreach(var row in Enumerable.Range(1, range.RowCount())) @@ -103,7 +102,31 @@ } } - + public static void Merge(this IXLRange range) + { + range.MergedCells.Add(range.FirstCellAddress.ToString() + ":" + range.LastCellAddress.ToString()); + } + public static void Unmerge(this IXLRange range) + { + range.MergedCells.Add(range.FirstCellAddress.ToString() + ":" + range.LastCellAddress.ToString()); + } + + public static IXLRange FirstColumn(this IXLRange range) + { + return range.Column(1); + } + public static IXLRange LastColumn(this IXLRange range) + { + return range.Column(range.ColumnCount()); + } + public static IXLRange FirstRow(this IXLRange range) + { + return range.Row(1); + } + public static IXLRange LastRow(this IXLRange range) + { + return range.Row(range.RowCount()); + } } } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLAlignment.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLAlignment.cs index 0036ff4..e074f3c 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLAlignment.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLAlignment.cs @@ -82,6 +82,9 @@ } set { + if (Horizontal == XLAlignmentHorizontalValues.General) + Horizontal = XLAlignmentHorizontalValues.Left; + if (value > 0 && !( Horizontal == XLAlignmentHorizontalValues.Left || Horizontal == XLAlignmentHorizontalValues.Right diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLRange.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLRange.cs index 2b7e571..b35f164 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLRange.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLRange.cs @@ -19,16 +19,6 @@ this.defaultStyle = new XLStyle(this, this.FirstCell().Style); } - public void Merge() - { - this.MergedCells.Add(this.FirstCellAddress.ToString() + ":" + this.LastCellAddress.ToString()); - } - - public void Unmerge() - { - this.MergedCells.Remove(this.FirstCellAddress.ToString() + ":" + this.LastCellAddress.ToString()); - } - #region IXLRange Members public Dictionary CellsCollection { get; private set; } diff --git a/ClosedXML/ClosedXML/ClosedXML_Examples/BasicTable.cs b/ClosedXML/ClosedXML/ClosedXML_Examples/BasicTable.cs index 1fa174c..5f58812 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Examples/BasicTable.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Examples/BasicTable.cs @@ -13,43 +13,48 @@ public void Create(String filePath) { // Creating a new workbook - var workbook = new XLWorkbook(); + var wb = new XLWorkbook(); //Adding a worksheet - var ws = workbook.Worksheets.Add("Contacts"); + var ws = wb.Worksheets.Add("Contacts"); //Adding text + //Title + ws.Cell("B2").Value = "Contacts"; //First Names - ws.Cell("A1").Value = "FName"; - ws.Cell("A2").Value = "John"; - ws.Cell("A3").Value = "Hank"; - ws.Cell("A4").Value = "Dagny"; + ws.Cell("B3").Value = "FName"; + ws.Cell("B4").Value = "John"; + ws.Cell("B5").Value = "Hank"; + ws.Cell("B6").Value = "Dagny"; //Last Names - ws.Cell("B1").Value = "LName"; - ws.Cell("B2").Value = "Galt"; - ws.Cell("B3").Value = "Rearden"; - ws.Cell("B4").Value = "Taggart"; + ws.Cell("C3").Value = "LName"; + ws.Cell("C4").Value = "Galt"; + ws.Cell("C5").Value = "Rearden"; + ws.Cell("C6").Value = "Taggart"; //Adding more data types //Is an outcast? - ws.Cell("C1").Value = "Outcast"; - ws.Cell("C2").Value = true.ToString(); - ws.Cell("C3").Value = false.ToString(); - ws.Cell("C4").Value = false.ToString(); + ws.Cell("D3").Value = "Outcast"; + ws.Cell("D4").Value = true.ToString(); + ws.Cell("D5").Value = false.ToString(); + ws.Cell("D6").Value = false.ToString(); //Date of Birth - ws.Cell("D1").Value = "DOB"; - ws.Cell("D2").Value = new DateTime(1919, 1, 21).ToString(); - ws.Cell("D3").Value = new DateTime(1907, 3, 4).ToString(); - ws.Cell("D4").Value = new DateTime(1921, 12, 15).ToString(); + ws.Cell("E3").Value = "DOB"; + ws.Cell("E4").Value = new DateTime(1919, 1, 21).ToString(); + ws.Cell("E5").Value = new DateTime(1907, 3, 4).ToString(); + ws.Cell("E6").Value = new DateTime(1921, 12, 15).ToString(); //Income - ws.Cell("E1").Value = "Income"; - ws.Cell("E2").Value = "2000"; - ws.Cell("E3").Value = "40000"; - ws.Cell("E4").Value = "10000"; + ws.Cell("F3").Value = "Income"; + ws.Cell("F4").Value = "2000"; + ws.Cell("F5").Value = "40000"; + ws.Cell("F6").Value = "10000"; //Defining ranges - var rngDates = ws.Range("D2:D4"); - var rngNumbers = ws.Range("E2:E4"); + //From worksheet + var rngTable = ws.Range("B2:F6"); + //From another range + var rngDates = rngTable.Range("D3:D5"); // The address is relative to rngTable (NOT the worksheet) + var rngNumbers = rngTable.Range("E3:E5"); // The address is relative to rngTable (NOT the worksheet) //Formatting dates and numbers //Using a OpenXML's predefined formats @@ -58,20 +63,37 @@ rngNumbers.Style.NumberFormat.Format = "$ #,##0"; // Adjust column width - ws.Column("D").Width = 12; + ws.Column("E").Width = 12; //Formatting headers - var rngHeaders = ws.Range("A1:E1"); + var rngHeaders = rngTable.Range("A2:E2"); // The address is relative to rngTable (NOT the worksheet) rngHeaders.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; rngHeaders.Style.Font.Bold = true; rngHeaders.Style.Fill.BackgroundColor = Color.Aqua; //Adding grid lines - var rngTable = ws.Range("A1:E4"); rngTable.Style.Border.BottomBorder = XLBorderStyleValues.Thin; + //Format title cell + rngTable.Cell(1, 1).Style.Font.Bold = true; + rngTable.Cell(1, 1).Style.Fill.BackgroundColor = Color.CornflowerBlue; + rngTable.Cell(1, 1).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; + + //Merge title cells + rngTable.Row(1).Merge(); // We could've also used: rngTable.Range("A1:E1").Merge() + + //Add thick borders + //Left border + rngTable.FirstColumn().Style.Border.LeftBorder = XLBorderStyleValues.Thick; + //Right border + rngTable.LastColumn().Style.Border.RightBorder = XLBorderStyleValues.Thick; + //Top border + rngTable.FirstRow().Style.Border.TopBorder = XLBorderStyleValues.Thick; + //Bottom border + rngTable.LastRow().Style.Border.BottomBorder = XLBorderStyleValues.Thick; + //Saving the workbook - workbook.SaveAs(filePath); + wb.SaveAs(filePath); } } } diff --git a/ClosedXML/ClosedXML/ClosedXML_Examples/Styles/StyleAlignment.cs b/ClosedXML/ClosedXML/ClosedXML_Examples/Styles/StyleAlignment.cs index 6699c5f..ebeb408 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Examples/Styles/StyleAlignment.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Examples/Styles/StyleAlignment.cs @@ -69,7 +69,6 @@ ws.Cell(ro, co).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right; ws.Cell(++ro, co).Value = "Indent = 2"; - ws.Cell(ro, co).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right; ws.Cell(ro, co).Style.Alignment.Indent = 2; ws.Cell(++ro, co).Value = "JustifyLastLine = true"; @@ -96,7 +95,6 @@ ws.Cell(++ro, co).Value = "WrapText = true"; ws.Cell(ro, co).Style.Alignment.WrapText = true; - workbook.SaveAs(filePath); } diff --git a/ClosedXML/ClosedXML/ClosedXML_Examples/Styles/StyleBorder.cs b/ClosedXML/ClosedXML/ClosedXML_Examples/Styles/StyleBorder.cs index a0c467f..b158caa 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Examples/Styles/StyleBorder.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Examples/Styles/StyleBorder.cs @@ -22,17 +22,17 @@ ws.Cell(ro, co).Style.Border.BottomBorder = XLBorderStyleValues.Thick; ws.Cell(ro, co).Style.Border.BottomBorderColor = Color.Red; - ws.Cell(++ro, co).Value = "LeftBorder = Thick; LeftBorderColor = Red"; + ws.Cell(++ro, co).Value = "LeftBorder = Thick; LeftBorderColor = Blue"; ws.Cell(ro, co).Style.Border.LeftBorder = XLBorderStyleValues.Thick; - ws.Cell(ro, co).Style.Border.LeftBorderColor = Color.Red; + ws.Cell(ro, co).Style.Border.LeftBorderColor = Color.Blue; - ws.Cell(++ro, co).Value = "TopBorder = Thick; TopBorderColor = Red"; + ws.Cell(++ro, co).Value = "TopBorder = Thick; TopBorderColor = Yellow"; ws.Cell(ro, co).Style.Border.TopBorder = XLBorderStyleValues.Thick; - ws.Cell(ro, co).Style.Border.TopBorderColor = Color.Red; + ws.Cell(ro, co).Style.Border.TopBorderColor = Color.Yellow; - ws.Cell(++ro, co).Value = "RightBorder = Thick; RightBorderColor = Red"; + ws.Cell(++ro, co).Value = "RightBorder = Thick; RightBorderColor = Black"; ws.Cell(ro, co).Style.Border.RightBorder = XLBorderStyleValues.Thick; - ws.Cell(ro, co).Style.Border.RightBorderColor = Color.Red; + ws.Cell(ro, co).Style.Border.RightBorderColor = Color.Black; ws.Cell(++ro, co).Value = "DiagonalBorder = Thin; DiagonalBorderColor = Red; DiagonalUp = true"; ws.Cell(ro, co).Style.Border.DiagonalBorder = XLBorderStyleValues.Thin; diff --git a/ClosedXML/ClosedXML/ClosedXML_Examples/Styles/StyleFill.cs b/ClosedXML/ClosedXML/ClosedXML_Examples/Styles/StyleFill.cs index 9ad35f4..9c17307 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Examples/Styles/StyleFill.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Examples/Styles/StyleFill.cs @@ -18,12 +18,12 @@ var co = 2; var ro = 1; - ws.Cell(++ro, co).Value = "BackgroundColor = Red"; + ws.Cell(++ro, co + 1).Value = "BackgroundColor = Red"; ws.Cell(ro, co).Style.Fill.BackgroundColor = Color.Red; - ws.Cell(++ro, co).Value = "PatternType = DarkTrellis; PatternColor = Red; PatternBackgroundColor = Blue"; + ws.Cell(++ro, co + 1).Value = "PatternType = DarkTrellis; PatternColor = Orange; PatternBackgroundColor = Blue"; ws.Cell(ro, co).Style.Fill.PatternType = XLFillPatternValues.DarkTrellis; - ws.Cell(ro, co).Style.Fill.PatternColor = Color.Red; + ws.Cell(ro, co).Style.Fill.PatternColor = Color.Orange; ws.Cell(ro, co).Style.Fill.PatternBackgroundColor = Color.Blue; workbook.SaveAs(filePath);