diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/IXLColumn.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/IXLColumn.cs index 95fa0da..9f65f5a 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/IXLColumn.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/IXLColumn.cs @@ -164,6 +164,7 @@ IXLColumn Sort(XLSortOrder sortOrder, Boolean matchCase); IXLRangeColumn Column(Int32 start, Int32 end); + IXLRangeColumn Column(IXLCell start, IXLCell end); IXLRangeColumns Columns(String columns); /// diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/XLColumn.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/XLColumn.cs index 4516fd6..8fc67b2 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/XLColumn.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/XLColumn.cs @@ -578,6 +578,11 @@ return Range(start, 1, end, 1).Column(1); } + public IXLRangeColumn Column(IXLCell start, IXLCell end) + { + return Column(start.Address.RowNumber, end.Address.RowNumber); + } + public IXLRangeColumns Columns(String columns) { var retVal = new XLRangeColumns(); diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/IXLRangeColumn.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/IXLRangeColumn.cs index 7420664..1c3f687 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/IXLRangeColumn.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/IXLRangeColumn.cs @@ -87,6 +87,7 @@ IXLRangeColumn Sort(XLSortOrder sortOrder, Boolean matchCase); IXLRangeColumn Column(Int32 start, Int32 end); + IXLRangeColumn Column(IXLCell start, IXLCell end); IXLRangeColumns Columns(String columns); IXLRangeColumn SetDataType(XLCellValues dataType); diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/IXLRangeRow.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/IXLRangeRow.cs index 07dd5c2..8793a32 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/IXLRangeRow.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/IXLRangeRow.cs @@ -95,6 +95,7 @@ IXLRangeRow Row(Int32 start, Int32 end); + IXLRangeRow Row(IXLCell start, IXLCell end); IXLRangeRows Rows(String rows); IXLRangeRow SetDataType(XLCellValues dataType); diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRange.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRange.cs index b781930..b254049 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRange.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRange.cs @@ -144,7 +144,7 @@ String lastColumn; if (tPair.Contains(':') || tPair.Contains('-')) { - string[] columnRange = tPair.Contains('-') ? tPair.Replace('-', ':').Split(':') : tPair.Split(':'); + string[] columnRange = ExcelHelper.SplitRange(tPair); firstColumn = columnRange[0]; lastColumn = columnRange[1]; @@ -248,7 +248,7 @@ String lastRow; if (tPair.Contains(':') || tPair.Contains('-')) { - string[] rowRange = tPair.Contains('-') ? tPair.Replace('-', ':').Split(':') : tPair.Split(':'); + string[] rowRange = ExcelHelper.SplitRange(tPair); firstRow = rowRange[0]; lastRow = rowRange[1]; diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeColumn.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeColumn.cs index 7179771..c0ffac6 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeColumn.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeColumn.cs @@ -137,6 +137,11 @@ return Range(start, end).FirstColumn(); } + public IXLRangeColumn Column(IXLCell start, IXLCell end) + { + return Column(start.Address.RowNumber, end.Address.RowNumber); + } + public IXLRangeColumns Columns(string columns) { var retVal = new XLRangeColumns(); diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeRow.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeRow.cs index a4cd964..7706021 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeRow.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeRow.cs @@ -149,6 +149,11 @@ return Range(1, start, 1, end).Row(1); } + public IXLRangeRow Row(IXLCell start, IXLCell end) + { + return Row(start.Address.ColumnNumber, end.Address.ColumnNumber); + } + public IXLRangeRows Rows(string rows) { var retVal = new XLRangeRows(); diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Rows/IXLRow.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Rows/IXLRow.cs index 7063ff4..81cdef2 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Rows/IXLRow.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Rows/IXLRow.cs @@ -167,6 +167,7 @@ IXLRow Sort(XLSortOrder sortOrder, Boolean matchCase); IXLRangeRow Row(Int32 start, Int32 end); + IXLRangeRow Row(IXLCell start, IXLCell end); IXLRangeRows Rows(String columns); /// diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Rows/XLRow.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Rows/XLRow.cs index 7e20390..6c8045d 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Rows/XLRow.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Rows/XLRow.cs @@ -492,6 +492,11 @@ return Range(1, start, 1, end).Row(1); } + public IXLRangeRow Row(IXLCell start, IXLCell end) + { + return Row(start.Address.ColumnNumber, end.Address.ColumnNumber); + } + public IXLRangeRows Rows(String rows) { var retVal = new XLRangeRows(); diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/XLTable.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/XLTable.cs index dd18433..5af07de 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/XLTable.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/XLTable.cs @@ -162,7 +162,7 @@ String lastRow; if (tPair.Contains(':') || tPair.Contains('-')) { - string[] rowRange = tPair.Contains('-') ? tPair.Replace('-', ':').Split(':') : tPair.Split(':'); + string[] rowRange = ExcelHelper.SplitRange(tPair); firstRow = rowRange[0]; lastRow = rowRange[1]; diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.NestedTypes.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.NestedTypes.cs index fc40876..c7185a5 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.NestedTypes.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.NestedTypes.cs @@ -1,10 +1,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using A = DocumentFormat.OpenXml.Drawing; -using Ap = DocumentFormat.OpenXml.ExtendedProperties; -using Op = DocumentFormat.OpenXml.CustomProperties; -using Vt = DocumentFormat.OpenXml.VariantTypes; namespace ClosedXML.Excel { @@ -15,53 +11,53 @@ { #region Private fields [DebuggerBrowsable(DebuggerBrowsableState.Never)] - private readonly RelIdGenerator m_relIdGenerator; + private readonly RelIdGenerator _relIdGenerator; [DebuggerBrowsable(DebuggerBrowsableState.Never)] - private readonly Dictionary m_sharedStyles; + private readonly Dictionary _sharedStyles; [DebuggerBrowsable(DebuggerBrowsableState.Never)] - private readonly Dictionary m_sharedFonts; + private readonly Dictionary _sharedFonts; [DebuggerBrowsable(DebuggerBrowsableState.Never)] - private readonly HashSet m_tableNames; + private readonly HashSet _tableNames; [DebuggerBrowsable(DebuggerBrowsableState.Never)] - private uint m_tableId; + private uint _tableId; #endregion #region Constructor public SaveContext() { - m_relIdGenerator = new RelIdGenerator(); - m_sharedStyles = new Dictionary(); - m_sharedFonts = new Dictionary(); - m_tableNames = new HashSet(); - m_tableId = 0; + _relIdGenerator = new RelIdGenerator(); + _sharedStyles = new Dictionary(); + _sharedFonts = new Dictionary(); + _tableNames = new HashSet(); + _tableId = 0; } #endregion #region Public properties public RelIdGenerator RelIdGenerator { [DebuggerStepThrough] - get { return m_relIdGenerator; } + get { return _relIdGenerator; } } public Dictionary SharedStyles { [DebuggerStepThrough] - get { return m_sharedStyles; } + get { return _sharedStyles; } } public Dictionary SharedFonts { [DebuggerStepThrough] - get { return m_sharedFonts; } + get { return _sharedFonts; } } public HashSet TableNames { [DebuggerStepThrough] - get { return m_tableNames; } + get { return _tableNames; } } public uint TableId { [DebuggerStepThrough] - get { return m_tableId; } + get { return _tableId; } [DebuggerStepThrough] - set { m_tableId = value; } + set { _tableId = value; } } #endregion } @@ -69,30 +65,27 @@ #region Nested type: RelType private enum RelType { - General, - Workbook, - Worksheet, - Drawing + Workbook } #endregion #region Nested type: RelIdGenerator private sealed class RelIdGenerator { - private readonly Dictionary> m_relIds = new Dictionary>(); + private readonly Dictionary> _relIds = new Dictionary>(); public String GetNext(RelType relType) { - if (!m_relIds.ContainsKey(relType)) + if (!_relIds.ContainsKey(relType)) { - m_relIds.Add(relType, new List()); + _relIds.Add(relType, new List()); } Int32 id = 1; while (true) { String relId = String.Format("rId{0}", id); - if (!m_relIds[relType].Contains(relId)) + if (!_relIds[relType].Contains(relId)) { - m_relIds[relType].Add(relId); + _relIds[relType].Add(relId); return relId; } id++; @@ -100,11 +93,11 @@ } public void AddValues(List values, RelType relType) { - if (!m_relIds.ContainsKey(relType)) + if (!_relIds.ContainsKey(relType)) { - m_relIds.Add(relType, new List()); + _relIds.Add(relType, new List()); } - m_relIds[relType].AddRange(values); + _relIds[relType].AddRange(values); } } #endregion diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs index 02d1158..e3794e3 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs @@ -63,8 +63,8 @@ _rowHeight = workbook.RowHeight; RowHeightChanged = workbook.RowHeight != XLWorkbook.DefaultRowHeight; Name = sheetName; - RangeShiftedRows += XLWorksheet_RangeShiftedRows; - RangeShiftedColumns += XLWorksheet_RangeShiftedColumns; + RangeShiftedRows += XLWorksheetRangeShiftedRows; + RangeShiftedColumns += XLWorksheetRangeShiftedColumns; Charts = new XLCharts(); ShowFormulas = workbook.ShowFormulas; ShowGridLines = workbook.ShowGridLines; @@ -200,9 +200,7 @@ public XLRow FirstRowUsed(Boolean includeFormats) { var rngRow = AsRange().FirstRowUsed(includeFormats); - if (rngRow != null) - return Row(rngRow.RangeAddress.FirstAddress.RowNumber); - return null; + return rngRow != null ? Row(rngRow.RangeAddress.FirstAddress.RowNumber) : null; } IXLRow IXLWorksheet.FirstRowUsed(Boolean includeFormats) @@ -221,10 +219,7 @@ public XLRow LastRowUsed(Boolean includeFormats) { var rngRow = AsRange().LastRowUsed(includeFormats); - if (rngRow != null) - return Row(rngRow.RangeAddress.LastAddress.RowNumber); - - return null; + return rngRow != null ? Row(rngRow.RangeAddress.LastAddress.RowNumber) : null; } IXLRow IXLWorksheet.LastRowUsed(Boolean includeFormats) { @@ -297,10 +292,7 @@ public XLColumn LastColumnUsed(Boolean includeFormats) { var rngColumn = AsRange().LastColumnUsed(includeFormats); - if (rngColumn != null) - return Column(rngColumn.RangeAddress.LastAddress.ColumnNumber); - - return null; + return rngColumn != null ? Column(rngColumn.RangeAddress.LastAddress.ColumnNumber) : null; } IXLColumn IXLWorksheet.LastColumnUsed(Boolean includeFormats) { @@ -320,7 +312,7 @@ columnList.AddRange(Internals.ColumnsCollection.Keys.Where(c => !columnList.Contains(c))); foreach (int c in columnList) - retVal.Add((XLColumn)Column(c)); + retVal.Add(Column(c)); return retVal; } @@ -329,17 +321,13 @@ { var retVal = new XLColumns(null); var columnPairs = columns.Split(','); - foreach (string pair in columnPairs) + foreach (string tPair in columnPairs.Select(pair => pair.Trim())) { - string tPair = pair.Trim(); String firstColumn; String lastColumn; if (tPair.Contains(':') || tPair.Contains('-')) { - if (tPair.Contains('-')) - tPair = tPair.Replace('-', ':'); - - var columnRange = tPair.Split(':'); + var columnRange = ExcelHelper.SplitRange(tPair); firstColumn = columnRange[0]; lastColumn = columnRange[1]; } @@ -375,7 +363,7 @@ var retVal = new XLColumns(null); for (int co = firstColumn; co <= lastColumn; co++) - retVal.Add((XLColumn)Column(co)); + retVal.Add(Column(co)); return retVal; } @@ -391,7 +379,7 @@ rowList.AddRange(Internals.RowsCollection.Keys.Where(r => !rowList.Contains(r))); foreach (int r in rowList) - retVal.Add((XLRow)Row(r)); + retVal.Add(Row(r)); return retVal; } @@ -400,17 +388,13 @@ { var retVal = new XLRows(null); var rowPairs = rows.Split(','); - foreach (string pair in rowPairs) + foreach (string tPair in rowPairs.Select(pair => pair.Trim())) { - string tPair = pair.Trim(); String firstRow; String lastRow; if (tPair.Contains(':') || tPair.Contains('-')) { - if (tPair.Contains('-')) - tPair = tPair.Replace('-', ':'); - - var rowRange = tPair.Split(':'); + var rowRange = ExcelHelper.SplitRange(tPair); firstRow = rowRange[0]; lastRow = rowRange[1]; } @@ -430,7 +414,7 @@ var retVal = new XLRows(null); for (int ro = firstRow; ro <= lastRow; ro++) - retVal.Add((XLRow)Row(ro)); + retVal.Add(Row(ro)); return retVal; } @@ -556,7 +540,7 @@ public IXLWorksheet CollapseRows(Int32 outlineLevel) { if (outlineLevel < 1 || outlineLevel > 8) - throw new ArgumentOutOfRangeException("Outline level must be between 1 and 8."); + throw new ArgumentOutOfRangeException("outlineLevel", "Outline level must be between 1 and 8."); Internals.RowsCollection.Values.Where(r => r.OutlineLevel == outlineLevel).ForEach(r => r.Collapse()); return this; @@ -664,12 +648,10 @@ foreach (XLTable t in Tables.Cast()) { - XLTable table; String tableName = t.Name; - if (targetSheet.Tables.Any(tt => tt.Name == tableName)) - table = new XLTable(targetSheet.Range(t.RangeAddress.ToString()), true); - else - table = new XLTable(targetSheet.Range(t.RangeAddress.ToString()), tableName, true); + XLTable table = targetSheet.Tables.Any(tt => tt.Name == tableName) + ? new XLTable(targetSheet.Range(t.RangeAddress.ToString()), true) + : new XLTable(targetSheet.Range(t.RangeAddress.ToString()), tableName, true); table.RelId = t.RelId; table.EmphasizeFirstColumn = t.EmphasizeFirstColumn; @@ -973,67 +955,55 @@ public void IncrementColumnOutline(Int32 level) { - if (level > 0) - { - if (!_columnOutlineCount.ContainsKey(level)) - _columnOutlineCount.Add(level, 0); + if (level <= 0) return; + if (!_columnOutlineCount.ContainsKey(level)) + _columnOutlineCount.Add(level, 0); - _columnOutlineCount[level]++; - } + _columnOutlineCount[level]++; } public void DecrementColumnOutline(Int32 level) { - if (level > 0) - { - if (!_columnOutlineCount.ContainsKey(level)) - _columnOutlineCount.Add(level, 0); + if (level <= 0) return; + if (!_columnOutlineCount.ContainsKey(level)) + _columnOutlineCount.Add(level, 0); - if (_columnOutlineCount[level] > 0) - _columnOutlineCount[level]--; - } + if (_columnOutlineCount[level] > 0) + _columnOutlineCount[level]--; } public Int32 GetMaxColumnOutline() { - if (_columnOutlineCount.Count == 0) - return 0; - return _columnOutlineCount.Where(kp => kp.Value > 0).Max(kp => kp.Key); + return _columnOutlineCount.Count == 0 ? 0 : _columnOutlineCount.Where(kp => kp.Value > 0).Max(kp => kp.Key); } public void IncrementRowOutline(Int32 level) { - if (level > 0) - { - if (!_rowOutlineCount.ContainsKey(level)) - _rowOutlineCount.Add(level, 0); + if (level <= 0) return; + if (!_rowOutlineCount.ContainsKey(level)) + _rowOutlineCount.Add(level, 0); - _rowOutlineCount[level]++; - } + _rowOutlineCount[level]++; } public void DecrementRowOutline(Int32 level) { - if (level > 0) - { - if (!_rowOutlineCount.ContainsKey(level)) - _rowOutlineCount.Add(level, 0); + if (level <= 0) return; + if (!_rowOutlineCount.ContainsKey(level)) + _rowOutlineCount.Add(level, 0); - if (_rowOutlineCount[level] > 0) - _rowOutlineCount[level]--; - } + if (_rowOutlineCount[level] > 0) + _rowOutlineCount[level]--; } public Int32 GetMaxRowOutline() { - if (_rowOutlineCount.Count == 0) - return 0; - return _rowOutlineCount.Where(kp => kp.Value > 0).Max(kp => kp.Key); + return _rowOutlineCount.Count == 0 ? 0 : _rowOutlineCount.Where(kp => kp.Value > 0).Max(kp => kp.Key); } #endregion - private void XLWorksheet_RangeShiftedColumns(XLRange range, int columnsShifted) + private void XLWorksheetRangeShiftedColumns(XLRange range, int columnsShifted) { var newMerge = new XLRanges(); foreach (IXLRange rngMerged in Internals.MergedRanges) @@ -1057,7 +1027,7 @@ Internals.MergedRanges = newMerge; } - private void XLWorksheet_RangeShiftedRows(XLRange range, int rowsShifted) + private void XLWorksheetRangeShiftedRows(XLRange range, int rowsShifted) { var newMerge = new XLRanges(); foreach (IXLRange rngMerged in Internals.MergedRanges) @@ -1121,7 +1091,7 @@ Internals.RowsCollection.Add(row, new XLRow(row, new XLRowParameters(this, styleToUse, false))); } - return new XLRow(row, new XLRowParameters(this, styleToUse, true)); + return new XLRow(row, new XLRowParameters(this, styleToUse)); } private IXLRange GetRangeForSort() diff --git a/ClosedXML/ClosedXML/ClosedXML/ExcelHelper.cs b/ClosedXML/ClosedXML/ClosedXML/ExcelHelper.cs index 6e084f0..077cc4c 100644 --- a/ClosedXML/ClosedXML/ClosedXML/ExcelHelper.cs +++ b/ClosedXML/ClosedXML/ClosedXML/ExcelHelper.cs @@ -152,5 +152,10 @@ ? cellAddressString.Substring(startPos, rowPos - 1) : cellAddressString.Substring(startPos, rowPos)); } + + public static string[] SplitRange(string range) + { + return range.Contains('-') ? range.Replace('-', ':').Split(':') : range.Split(':'); + } } } \ No newline at end of file