diff --git a/ClosedXML/ClosedXML/ClosedXML/ClosedXML.csproj b/ClosedXML/ClosedXML/ClosedXML/ClosedXML.csproj index 664ecd9..72c7c7c 100644 --- a/ClosedXML/ClosedXML/ClosedXML/ClosedXML.csproj +++ b/ClosedXML/ClosedXML/ClosedXML/ClosedXML.csproj @@ -56,6 +56,7 @@ + diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/IXLRange.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/IXLRange.cs index 671fe45..2819147 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/IXLRange.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/IXLRange.cs @@ -9,6 +9,7 @@ public interface IXLRange: IXLStylized { Dictionary CellsCollection { get; } + List MergedCells { get; } IXLAddress FirstCellAddress { get; } IXLAddress LastCellAddress { get; } @@ -64,24 +65,28 @@ return range.LastCellAddress.Column - range.FirstCellAddress.Column + 1; } - public static IXLRange Range(this IXLRange range, String rangeAddress) + public static XLRange Range(this IXLRange range, String rangeAddress) { String[] arrRange = rangeAddress.Split(':'); return range.Range(arrRange[0], arrRange[1]); } - public static IXLRange Range(this IXLRange range, String firstCellAddress, String lastCellAddress) + public static XLRange Range(this IXLRange range, String firstCellAddress, String lastCellAddress) { return range.Range(new XLAddress(firstCellAddress), new XLAddress(lastCellAddress)); } - public static IXLRange Range(this IXLRange range, IXLAddress firstCellAddress, IXLAddress lastCellAddress) + public static XLRange Range(this IXLRange range, IXLAddress firstCellAddress, IXLAddress lastCellAddress) { return new XLRange( - (XLAddress)firstCellAddress + (XLAddress)range.FirstCellAddress - 1, - (XLAddress)lastCellAddress + (XLAddress)range.FirstCellAddress - 1, - range.CellsCollection + new XLRangeParameters() + { + FirstCellAddress = (XLAddress)firstCellAddress + (XLAddress)range.FirstCellAddress - 1, + LastCellAddress = (XLAddress)lastCellAddress + (XLAddress)range.FirstCellAddress - 1, + CellsCollection = range.CellsCollection, + MergedCells = range.MergedCells + } ); } - public static IXLRange Range(this IXLRange range, IXLCell firstCell, IXLCell lastCell) + public static XLRange Range(this IXLRange range, IXLCell firstCell, IXLCell lastCell) { return range.Range(firstCell.Address, lastCell.Address); } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLColumn.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLColumn.cs index 0be5d8f..d1edce1 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLColumn.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLColumn.cs @@ -22,6 +22,7 @@ #region IXLRange Members public Dictionary CellsCollection { get; private set; } + public List MergedCells { get; private set; } public IXLAddress FirstCellAddress { get; private set; } public IXLAddress LastCellAddress { get; private set; } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLRange.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLRange.cs index 256561b..2b7e571 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLRange.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLRange.cs @@ -9,17 +9,30 @@ public class XLRange: IXLRange { private IXLStyle defaultStyle; - public XLRange(IXLAddress firstCellAddress, IXLAddress lastCellAddress, Dictionary cellCollection) + + public XLRange(XLRangeParameters xlRangeParameters) { - FirstCellAddress = firstCellAddress; - LastCellAddress = lastCellAddress; - CellsCollection = cellCollection; + FirstCellAddress = xlRangeParameters.FirstCellAddress; + LastCellAddress = xlRangeParameters.LastCellAddress; + CellsCollection = xlRangeParameters.CellsCollection; + MergedCells = xlRangeParameters.MergedCells; 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; } + public List MergedCells { get; private set; } public IXLAddress FirstCellAddress { get; private set; } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLRangeParameters.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLRangeParameters.cs new file mode 100644 index 0000000..fb03cee --- /dev/null +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLRangeParameters.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace ClosedXML.Excel +{ + public class XLRangeParameters + { + #region Variables + + // Public + + // Private + + + #endregion + + #region Properties + + // Public + public IXLAddress FirstCellAddress { get; set; } + public IXLAddress LastCellAddress { get; set; } + public Dictionary CellsCollection { get; set; } + public List MergedCells { get; set; } + + // Private + + // Override + + + #endregion + + #region Constructors + + // Public + public XLRangeParameters() + { + + } + + + // Private + + + #endregion + + #region Events + + // Public + + // Private + + // Override + + + #endregion + + #region Methods + + // Public + + // Private + + // Override + + + #endregion + } +} diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLRow.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLRow.cs index 1d5b2dc..15a27d0 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLRow.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLRow.cs @@ -22,6 +22,7 @@ #region IXLRange Members public Dictionary CellsCollection { get; private set; } + public List MergedCells { get; private set; } public IXLAddress FirstCellAddress { get; private set; } public IXLAddress LastCellAddress { get; private set; } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs index c4a4bad..8fc4fe0 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs @@ -691,15 +691,15 @@ } MergeCells mergeCells = null; - //if (xlWorksheet.MergedCells.Count > 0) - //{ - // mergeCells = new MergeCells() { Count = (UInt32Value)(UInt32)xlWorksheet.MergedCells.Count }; - // foreach (var merged in xlWorksheet.MergedCells) - // { - // MergeCell mergeCell = new MergeCell() { Reference = merged }; - // mergeCells.Append(mergeCell); - // } - //} + if (xlWorksheet.MergedCells.Count > 0) + { + mergeCells = new MergeCells() { Count = (UInt32Value)(UInt32)xlWorksheet.MergedCells.Count }; + foreach (var merged in xlWorksheet.MergedCells) + { + MergeCell mergeCell = new MergeCell() { Reference = merged }; + mergeCells.Append(mergeCell); + } + } PageMargins pageMargins = new PageMargins() { Left = 0.7D, Right = 0.7D, Top = 0.75D, Bottom = 0.75D, Header = 0.3D, Footer = 0.3D }; Drawing drawing1 = new Drawing() { Id = "rId1" }; diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs index 37a4f7e..31a21cd 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs @@ -24,12 +24,13 @@ var defaultAddress = new XLAddress(0,0); DefaultCell = new XLCell(defaultAddress, XLWorkbook.DefaultStyle); cellsCollection.Add(defaultAddress, DefaultCell); + MergedCells = new List(); var tmp = this.Cell(1, 1).Value; this.Name = sheetName; } #region IXLRange Members - + public List MergedCells { get; private set; } public Dictionary CellsCollection { get { return cellsCollection; } diff --git a/ClosedXML/ClosedXML/ClosedXML_Examples/BasicTable.cs b/ClosedXML/ClosedXML/ClosedXML_Examples/BasicTable.cs index ee837df..1fa174c 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Examples/BasicTable.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Examples/BasicTable.cs @@ -53,13 +53,13 @@ //Formatting dates and numbers //Using a OpenXML's predefined formats - rngDates.Style.NumberFormat.NumberFormatId = 15; // .Format = "mm-dd-yyyy"; + rngDates.Style.NumberFormat.NumberFormatId = 15; //Using a custom format rngNumbers.Style.NumberFormat.Format = "$ #,##0"; // Adjust column width ws.Column("D").Width = 12; - + //Formatting headers var rngHeaders = ws.Range("A1:E1"); rngHeaders.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; diff --git a/ClosedXML/ClosedXML/ClosedXML_Examples/ClosedXML_Examples.csproj b/ClosedXML/ClosedXML/ClosedXML_Examples/ClosedXML_Examples.csproj index cc8d034..f2ca730 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Examples/ClosedXML_Examples.csproj +++ b/ClosedXML/ClosedXML/ClosedXML_Examples/ClosedXML_Examples.csproj @@ -51,6 +51,7 @@ + diff --git a/ClosedXML/ClosedXML/ClosedXML_Examples/Misc/MergeCells.cs b/ClosedXML/ClosedXML/ClosedXML_Examples/Misc/MergeCells.cs new file mode 100644 index 0000000..a9b82db --- /dev/null +++ b/ClosedXML/ClosedXML/ClosedXML_Examples/Misc/MergeCells.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using ClosedXML.Excel; +using ClosedXML.Excel.Style; +using System.Drawing; + +namespace ClosedXML_Examples.Misc +{ + public class MergeCells + { + #region Variables + + // Public + + // Private + + + #endregion + + #region Properties + + // Public + + // Private + + // Override + + + #endregion + + #region Constructors + + // Public + + + // Private + + + #endregion + + #region Events + + // Public + + // Private + + // Override + + + #endregion + + #region Methods + + // Public + public void Create(String filePath) + { + var workbook = new XLWorkbook(); + var ws = workbook.Worksheets.Add("Merge Cells"); + + ws.Cell("B2").Value = "Merged Cells (B2 - D2)"; + ws.Range("B2:D2").Merge(); + + ws.Cell("B4").Value = "Merged Cells (B4 - D6)"; + ws.Cell("B4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; + ws.Cell("B4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center; + ws.Range("B4:D6").Merge(); + + ws.Cell("B8").Value = "Unmerged"; + ws.Range("B8:D8").Merge(); + ws.Range("B8:D8").Unmerge(); + + workbook.SaveAs(filePath); + } + + // Private + + // Override + + + #endregion + } +} diff --git a/ClosedXML/ClosedXML/ClosedXML_Examples/Program.cs b/ClosedXML/ClosedXML/ClosedXML_Examples/Program.cs index 048a34e..03d0f85 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Examples/Program.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Examples/Program.cs @@ -5,6 +5,7 @@ using ClosedXML_Examples.Styles; using ClosedXML_Examples.Columns; using ClosedXML_Examples.Rows; +using ClosedXML_Examples.Misc; namespace ClosedXML_Examples { @@ -17,6 +18,7 @@ new StyleExamples().Create(); new ColumnSettings().Create(@"c:\ColumnSettings.xlsx"); new RowSettings().Create(@"c:\RowSettings.xlsx"); + new MergeCells().Create(@"c:\MergedCells.xlsx"); } } } \ No newline at end of file