diff --git a/ClosedXML/ClosedXML/ClosedXML/ClosedXML.csproj b/ClosedXML/ClosedXML/ClosedXML/ClosedXML.csproj index 19f3929..1dc3636 100644 --- a/ClosedXML/ClosedXML/ClosedXML/ClosedXML.csproj +++ b/ClosedXML/ClosedXML/ClosedXML/ClosedXML.csproj @@ -150,6 +150,7 @@ + diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/IXLCell.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/IXLCell.cs index 7b35775..98bf6b1 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/IXLCell.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/IXLCell.cs @@ -25,7 +25,7 @@ /// Gets this cell's address, relative to the worksheet. /// The cell's address. - IXLAddress Address { get; } + IXLAddress Address { get; } /// /// Gets or sets the type of this cell's data. diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs index 17892f9..0c1681d 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs @@ -25,7 +25,7 @@ this.worksheet = worksheet; } - public IXLAddress Address { get; private set; } + public IXLAddress Address { get; internal set; } public String InnerText { get diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/NamedRanges/XLNamedRange.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/NamedRanges/XLNamedRange.cs index 481c828..e68be0a 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/NamedRanges/XLNamedRange.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/NamedRanges/XLNamedRange.cs @@ -20,7 +20,7 @@ public XLNamedRange(XLNamedRanges namedRanges, String rangeName, IXLRanges ranges, String comment = null) { Name = rangeName; - ranges.ForEach(r => rangeList.Add(r.ToString())); + ranges.ForEach(r => rangeList.Add(r.ToStringFixed())); Comment = comment; this.namedRanges = namedRanges; } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/IXLRange.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/IXLRange.cs index 26fae12..dec3f79 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/IXLRange.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/IXLRange.cs @@ -8,6 +8,7 @@ { public enum XLShiftDeletedCells { ShiftCellsUp, ShiftCellsLeft } public enum XLTransposeOptions { MoveCells, ReplaceCells } + public enum XLSortOrder { Ascending, Descending } public interface IXLRange: IXLRangeBase { /// @@ -216,7 +217,10 @@ IXLRange RangeUsed(); - IXLRange SortBy(String columns); + //IXLRange SortRows(); + //IXLRange SortRows(XLSortOrder sortOrder); + //IXLRange SortRows(String columnToSortBy); + //IXLRange SortRows(XLSortOrder sortOrder, String columnOrder); void CopyTo(IXLCell target); void CopyTo(IXLRangeBase target); diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/Sort/XLSortColumn.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/Sort/XLSortColumn.cs new file mode 100644 index 0000000..c0c6590 --- /dev/null +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/Sort/XLSortColumn.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace ClosedXML.Excel +{ + public class XLSortColumn + { + + } +} diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRange.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRange.cs index 4785a00..431029b 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRange.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRange.cs @@ -455,15 +455,69 @@ ^ this.Worksheet.GetHashCode(); } - public IXLRange SortBy(String columns) - { - SortRange(this, columns.Split(',')); - return this; - } - private void SortRange(XLRange xLRange, string[] columns) { throw new NotImplementedException(); } + + public IXLRange Sort(String columnsToSort) + { + var cols = columnsToSort.Split(',').ToList(); + q_sort(1, this.RowCount(), cols); + return this; + } + + public void q_sort(int left, int right, List columnsToSort) + { + int i, j; + XLRangeRow x, y; + + i = left; j = right; + x = (XLRangeRow)Row(((left + right) / 2)); + + do + { + while ((((XLRangeRow)Row(i)).CompareTo(x, columnsToSort) < 0) && (i < right)) i++; + while ((0 < ((XLRangeRow)Row(j)).CompareTo(x, columnsToSort)) && (j > left)) j--; + + if (i < j) + { + SwapRows(i, j); + i++; j--; + } + else if (i == j) + { + i++; j--; + } + } while (i <= j); + + if (left < j) q_sort(left, j, columnsToSort); + if (i < right) q_sort(i, right, columnsToSort); + + } + + public void SwapRows(Int32 row1, Int32 row2) + { + + Int32 cellCount = ColumnCount(); + + for (Int32 co = 1; co <= cellCount; co++) + { + + var cell1 = (XLCell)Row(row1).Cell(co); + var cell1Address = cell1.Address; + var cell2 = (XLCell)Row(row2).Cell(co); + + cell1.Address = cell2.Address; + + cell2.Address = cell1Address; + Worksheet.Internals.CellsCollection.Remove(cell1.Address); + Worksheet.Internals.CellsCollection.Remove(cell2.Address); + Worksheet.Internals.CellsCollection.Add(cell1.Address, cell1); + Worksheet.Internals.CellsCollection.Add(cell2.Address, cell2); + + } + + } } } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeRow.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeRow.cs index c6dcd2f..72206d5 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeRow.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeRow.cs @@ -113,7 +113,23 @@ return this.RangeAddress.LastAddress.ColumnNumber - this.RangeAddress.FirstAddress.ColumnNumber + 1; } - //public Int32 CompareTo( + public Int32 CompareTo(XLRangeRow otherRow, List columnsToSort) + { + foreach (String co in columnsToSort) + { + var thisCell = (XLCell)this.Cell(Int32.Parse(co)); + var otherCell = (XLCell)otherRow.Cell(Int32.Parse(co)); + Int32 comparison; + if (thisCell.DataType == otherCell.DataType) + comparison = thisCell.InnerText.CompareTo(otherCell.InnerText); + else + comparison = thisCell.GetString().CompareTo(otherCell.GetString()); + + if (comparison != 0) + return comparison; + } + return 0; + } } } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs index 0a5250e..eebf1d7 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs @@ -732,8 +732,8 @@ foreach (var printArea in worksheet.PageSetup.PrintAreas) { definedNameText += "'" + worksheet.Name + "'!" - + printArea.RangeAddress.FirstAddress.ToString() - + ":" + printArea.RangeAddress.LastAddress.ToString() + ","; + + printArea.RangeAddress.FirstAddress.ToStringFixed() + + ":" + printArea.RangeAddress.LastAddress.ToStringFixed() + ","; } definedName.Text = definedNameText.Substring(0, definedNameText.Length - 1); definedNames.Append(definedName); diff --git a/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs b/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs index bceb144..e9a160c 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs @@ -15,53 +15,16 @@ static void Main(string[] args) { //var fileName = "BasicTable"; - //var fileName = "Sandbox"; - var fileName = "Issue_6429"; - var sourceFile = new XLWorkbook(String.Format(@"c:\Excel Files\ForTesting\{0}.xlsm", fileName)); - //var wb = new XLWorkbook(); - - using (var tempStream = new FileStream(String.Format(@"c:\Excel Files\ForTesting\{0}_Saved.xlsm", fileName), FileMode.Create)) - { - sourceFile.SaveAs(tempStream); - } + var fileName = "Sandbox"; + //var fileName = "Issue_6429"; + //var wb = new XLWorkbook(String.Format(@"c:\Excel Files\ForTesting\{0}.xlsx", fileName)); + var wb = new XLWorkbook(); + var wsData = wb.Worksheets.Add("Data"); + var xcell = wsData.Cell(1, 1); + xcell.Value = "Test"; + wsData.NamedRanges.Add("RangeName", xcell.AsRange(), "Comment"); - sourceFile.SaveAs(@"c:\Excel Files\ForTesting\Issue_6429_FileToFile.xlsm"); - using (var stream = new FileStream(@"c:\Excel Files\ForTesting\Issue_6429_FileToStream.xlsm", FileMode.Create)) - { - sourceFile.SaveAs(stream); - } - - using (var stream = new FileStream( - String.Format(@"c:\Excel Files\ForTesting\{0}.xlsm", fileName), FileMode.Open)) - { - var sourceStream = new XLWorkbook(stream); - sourceStream.SaveAs(@"c:\Excel Files\ForTesting\Issue_6429_StreamToFile.xlsm"); - - using (var stream2 = new FileStream(@"c:\Excel Files\ForTesting\Issue_6429_StreamToStream.xlsm", FileMode.Create)) - { - sourceStream.SaveAs(stream2); - } - } - - //var ws = wb.Worksheet(1); - //var table = ws.RangeUsed(); - //var newColumn = table.Column(1).InsertColumnsAfter(1).Single(); - //table.Column(1).CopyTo(newColumn); - //newColumn.SetAutoFilter(); - - //CopyWorksheets(String.Format(source, "UsingTables"), wb); - //CopyWorksheets(String.Format(source, "NamedRanges"), wb); - //CopyWorksheets(String.Format(source, "MergedCells"), wb); - //CopyWorksheets(String.Format(source, "HideSheets"), wb); - //CopyWorksheets(String.Format(source, "HideUnhide"), wb); - //CopyWorksheets(String.Format(source, "Outline"), wb); - //CopyWorksheets(String.Format(source, "RowCollection"), wb); - //CopyWorksheets(String.Format(source, "ColumnCollection"), wb); - //CopyWorksheets(String.Format(source, "ShowCase"), wb); - //CopyWorksheets(String.Format(source, "DataValidation"), wb); - //CopyWorksheets(String.Format(source, "Hyperlinks"), wb); - - //wb.SaveAs(String.Format(@"c:\Excel Files\ForTesting\{0}_Saved.xlsm", fileName)); + wb.SaveAs(String.Format(@"c:\Excel Files\ForTesting\{0}_Saved.xlsx", fileName)); } static void CopyWorksheets(String source, XLWorkbook target)