diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/IXLCell.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/IXLCell.cs index f20106a..7a49d70 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/IXLCell.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/IXLCell.cs @@ -169,5 +169,7 @@ /// The scope for the named range. /// The comments for the named range. IXLCell AddToNamed(String rangeName, XLScope scope, String comment); + + IXLCell CopyFrom(IXLCell otherCell); } } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs index 2b0f160..7e785a1 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs @@ -1120,6 +1120,24 @@ this.formulaR1C1 = source.formulaR1C1; } + public IXLCell CopyFrom(IXLCell otherCell) + { + var source = otherCell as XLCell; + cellValue = source.cellValue; + dataType = source.dataType; + formulaA1 = source.formulaA1; + formulaR1C1 = source.formulaR1C1; + style = new XLStyle(this, source.style); + if (source.hyperlink != null) + { + SettingHyperlink = true; + Hyperlink = new XLHyperlink(source.Hyperlink); + SettingHyperlink = false; + } + + return this; + } + //internal void ShiftFormula(Int32 rowsToShift, Int32 columnsToShift) //{ // if (!StringExtensions.IsNullOrWhiteSpace(formulaA1)) diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/XLColumn.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/XLColumn.cs index 2dabeb6..c0184c3 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/XLColumn.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Columns/XLColumn.cs @@ -27,6 +27,18 @@ } } + public XLColumn(XLColumn column, XLWorksheet worksheet) + : base(new XLRangeAddress(new XLAddress(1, column.ColumnNumber(), false, false), new XLAddress(XLWorksheet.MaxNumberOfRows, column.ColumnNumber(), false, false))) + { + width = column.width; + IsReference = column.IsReference; + collapsed = column.collapsed; + isHidden = column.isHidden; + outlineLevel = column.outlineLevel; + this.Worksheet = worksheet; + style = new XLStyle(this, column.Style); + } + void Worksheet_RangeShiftedColumns(XLRange range, int columnsShifted) { if (range.RangeAddress.FirstAddress.ColumnNumber <= this.ColumnNumber()) diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/DataValidation/IXLDataValidation.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/DataValidation/IXLDataValidation.cs index 80ef4b1..214b046 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/DataValidation/IXLDataValidation.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/DataValidation/IXLDataValidation.cs @@ -12,7 +12,7 @@ { IXLRanges Ranges { get; set; } void Delete(); - void CopyFrom(IXLDataValidation dataValidation); + //void CopyFrom(IXLDataValidation dataValidation); Boolean ShowInputMessage { get; set; } Boolean ShowErrorMessage { get; set; } Boolean IgnoreBlanks { get; set; } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/DataValidation/XLDataValidation.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/DataValidation/XLDataValidation.cs index 8166472..6c6a3c8 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/DataValidation/XLDataValidation.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/DataValidation/XLDataValidation.cs @@ -19,6 +19,13 @@ Operator = XLOperator.Between; this.worksheet = worksheet; } + + public XLDataValidation(IXLDataValidation dataValidation, XLWorksheet worksheet) + { + this.worksheet = worksheet; + this.CopyFrom(dataValidation); + } + public IXLRanges Ranges { get; set; } public void Delete() @@ -30,6 +37,9 @@ } public void CopyFrom(IXLDataValidation dataValidation) { + Ranges = new XLRanges(worksheet.Internals.Workbook, worksheet.Style); + dataValidation.Ranges.ForEach(r => Ranges.Add(r)); + IgnoreBlanks = dataValidation.IgnoreBlanks; InCellDropdown = dataValidation.InCellDropdown; ShowErrorMessage = dataValidation.ShowErrorMessage; diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Hyperlinks/XLHyperlink_Internal.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Hyperlinks/XLHyperlink_Internal.cs index 1c80b6b..d4000a5 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Hyperlinks/XLHyperlink_Internal.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Hyperlinks/XLHyperlink_Internal.cs @@ -12,6 +12,14 @@ } + internal XLHyperlink(XLHyperlink hyperlink) + { + externalAddress = hyperlink.externalAddress; + internalAddress = hyperlink.internalAddress; + Tooltip = hyperlink.Tooltip; + IsExternal = hyperlink.IsExternal; + } + internal void SetValues(String address, String tooltip) { Tooltip = tooltip; diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/XLHFItem.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/XLHFItem.cs index 5c56763..2840a84 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/XLHFItem.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/XLHFItem.cs @@ -7,6 +7,12 @@ { internal class XLHFItem : IXLHFItem { + public XLHFItem() + { } + public XLHFItem(XLHFItem defaultHFItem) + { + defaultHFItem.texts.ForEach(kp => texts.Add(kp.Key, kp.Value)); + } private Dictionary texts = new Dictionary(); public String GetText(XLHFOccurrence occurrence) { diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/XLHeaderFooter.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/XLHeaderFooter.cs index 1bef2b5..15587bc 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/XLHeaderFooter.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/XLHeaderFooter.cs @@ -13,6 +13,15 @@ Right = new XLHFItem(); Center = new XLHFItem(); } + + public XLHeaderFooter(XLHeaderFooter defaultHF) + { + defaultHF.innerTexts.ForEach(kp => innerTexts.Add(kp.Key, kp.Value)); + Left = new XLHFItem(defaultHF.Left as XLHFItem); + Center = new XLHFItem(defaultHF.Center as XLHFItem); + Right = new XLHFItem(defaultHF.Right as XLHFItem); + } + public IXLHFItem Left { get; private set; } public IXLHFItem Center { get; private set; } public IXLHFItem Right { get; private set; } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/XLPageSetup.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/XLPageSetup.cs index c5a9761..4414107 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/XLPageSetup.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/XLPageSetup.cs @@ -9,9 +9,10 @@ { public XLPageSetup(IXLPageSetup defaultPageOptions, XLWorksheet worksheet) { - this.PrintAreas = new XLPrintAreas(worksheet); + if (defaultPageOptions != null) { + this.PrintAreas = new XLPrintAreas(defaultPageOptions.PrintAreas as XLPrintAreas, worksheet); this.CenterHorizontally = defaultPageOptions.CenterHorizontally; this.CenterVertically = defaultPageOptions.CenterVertically; this.FirstPageNumber = defaultPageOptions.FirstPageNumber; @@ -44,12 +45,20 @@ this.DraftQuality = defaultPageOptions.DraftQuality; this.PageOrder = defaultPageOptions.PageOrder; - this.ColumnBreaks = new List(); - this.RowBreaks = new List(); + this.ColumnBreaks = defaultPageOptions.ColumnBreaks.ToList(); + this.RowBreaks = defaultPageOptions.RowBreaks.ToList(); + Header = new XLHeaderFooter(defaultPageOptions.Header as XLHeaderFooter); + Footer = new XLHeaderFooter(defaultPageOptions.Footer as XLHeaderFooter); this.PrintErrorValue = defaultPageOptions.PrintErrorValue; } - Header = new XLHeaderFooter(); - Footer = new XLHeaderFooter(); + else + { + this.PrintAreas = new XLPrintAreas(worksheet); + Header = new XLHeaderFooter(); + Footer = new XLHeaderFooter(); + this.ColumnBreaks = new List(); + this.RowBreaks = new List(); + } } public IXLPrintAreas PrintAreas { get; private set; } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/XLPrintAreas.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/XLPrintAreas.cs index 2a3481e..a69efbf 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/XLPrintAreas.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/XLPrintAreas.cs @@ -14,6 +14,12 @@ this.worksheet = worksheet; } + public XLPrintAreas(XLPrintAreas defaultPrintAreas, XLWorksheet worksheet) + { + ranges = defaultPrintAreas.ranges.ToList(); + this.worksheet = worksheet; + } + public void Clear() { ranges.Clear(); diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/IXLRange.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/IXLRange.cs index 8856eb6..e509c5c 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/IXLRange.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/IXLRange.cs @@ -215,6 +215,8 @@ IXLTable CreateTable(String name); IXLRange RangeUsed(); + + IXLRange SortBy(String columns); } } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRange.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRange.cs index 98b6f30..4785a00 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRange.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRange.cs @@ -454,5 +454,16 @@ return RangeAddress.GetHashCode() ^ this.Worksheet.GetHashCode(); } + + public IXLRange SortBy(String columns) + { + SortRange(this, columns.Split(',')); + return this; + } + + private void SortRange(XLRange xLRange, string[] columns) + { + throw new NotImplementedException(); + } } } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeRow.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeRow.cs index ddd5c26..c6dcd2f 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeRow.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeRow.cs @@ -112,6 +112,8 @@ { return this.RangeAddress.LastAddress.ColumnNumber - this.RangeAddress.FirstAddress.ColumnNumber + 1; } + + //public Int32 CompareTo( } } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Rows/XLRow.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Rows/XLRow.cs index d1b3f8e..142920f 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Rows/XLRow.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Rows/XLRow.cs @@ -26,6 +26,18 @@ } } + public XLRow(XLRow row, XLWorksheet worksheet) + : base(new XLRangeAddress(new XLAddress(row.RowNumber(), 1, false, false), new XLAddress(row.RowNumber(), XLWorksheet.MaxNumberOfColumns, false, false))) + { + height = row.height; + IsReference = row.IsReference; + collapsed = row.collapsed; + isHidden = row.isHidden; + outlineLevel = row.outlineLevel; + this.Worksheet = worksheet; + style = new XLStyle(this, row.Style); + } + void Worksheet_RangeShiftedRows(XLRange range, int rowsShifted) { if (range.RangeAddress.FirstAddress.RowNumber <= this.RowNumber()) diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLProtection.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLProtection.cs index daf5965..a47da26 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLProtection.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLProtection.cs @@ -87,23 +87,16 @@ public override string ToString() { - StringBuilder sb = new StringBuilder(); - - if (this.Locked) - sb.Append("Locked"); - - if (this.Hidden) - { - if (this.Locked) - sb.Append("-"); - - sb.Append("Hidden"); - } - - if (sb.Length < 0) - sb.Append("None"); - - return sb.ToString(); + if (Locked) + if (Hidden) + return "Locked-Hidden"; + else + return "Locked"; + else + if (Hidden) + return "Hidden"; + else + return "None"; } public IXLStyle SetLocked() { Locked = true; return container.Style; } public IXLStyle SetLocked(Boolean value) { Locked = value; return container.Style; } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/XLTable.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/XLTable.cs index 717a466..ffb7505 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/XLTable.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/XLTable.cs @@ -65,7 +65,7 @@ while (true) { String tableName = String.Format("Table{0}", id); - if (!Worksheet.Tables.Where(t=>t.Name == tableName).Any()) + if (!Worksheet.Tables.Where(t => t.Name == tableName).Any()) { Name = tableName; AddToTables(range, addToTables); @@ -108,7 +108,7 @@ } } - HashSet uniqueNames; + internal HashSet uniqueNames; private String GetUniqueName(String originalName) { String name = originalName; @@ -266,7 +266,7 @@ return Field(GetFieldIndex(fieldName)); } - private Dictionary fields = new Dictionary(); + internal Dictionary fields = new Dictionary(); public IXLTableField Field(Int32 fieldIndex) { if (!fields.ContainsKey(fieldIndex)) diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/XLTableField.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/XLTableField.cs index db7e7fd..2d4cdfa 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/XLTableField.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Tables/XLTableField.cs @@ -16,7 +16,7 @@ public Int32 Index { get; set; } public String Name { get; set; } - private String totalsRowLabel; + internal String totalsRowLabel; public String TotalsRowLabel { get { return totalsRowLabel; } @@ -47,7 +47,7 @@ } } - private XLTotalsRowFunction totalsRowFunction; + internal XLTotalsRowFunction totalsRowFunction; public XLTotalsRowFunction TotalsRowFunction { get { return totalsRowFunction; } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs index 4376fba..0c3e486 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs @@ -109,6 +109,7 @@ var ws = (XLWorksheet)Worksheets.Add(sheetName); ws.RelId = dSheet.Id; + ws.SheetId = (Int32)dSheet.SheetId.Value; if (dSheet.State != null) ws.Visibility = sheetStateValues.Single(p => p.Value == dSheet.State).Key; @@ -218,6 +219,9 @@ Int32 styleIndex = dCell.StyleIndex != null ? Int32.Parse(dCell.StyleIndex.InnerText) : 0; var xlCell = (XLCell)ws.Cell(dCell.CellReference); + //if (dCell.CellReference.Value == "A56") + // dCell.CellReference = dCell.CellReference.Value; + if (styleIndex > 0) { //styleIndex = Int32.Parse(dCell.StyleIndex.InnerText); @@ -315,7 +319,7 @@ else if ((numberFormatId >= 14 && numberFormatId <= 22) || (numberFormatId >= 45 && numberFormatId <= 47)) xlCell.DataType = XLCellValues.DateTime; - else if (numberFormatId == 0 || numberFormatId == 49) + else if (numberFormatId == 49) xlCell.DataType = XLCellValues.Text; else xlCell.DataType = XLCellValues.Number; @@ -485,19 +489,19 @@ var sp = (SheetProtection)sheetProtectionQuery.First(); if (sp.Sheet != null) ws.Protection.Protected = sp.Sheet.Value; if (sp.Password != null) (ws.Protection as XLSheetProtection).PasswordHash = sp.Password.Value; - if (sp.FormatCells != null) ws.Protection.Protected = sp.FormatCells.Value; - if (sp.FormatColumns != null) ws.Protection.Protected = sp.FormatColumns.Value; - if (sp.FormatRows != null) ws.Protection.Protected = sp.FormatRows.Value; - if (sp.InsertColumns != null) ws.Protection.Protected = sp.InsertColumns.Value; - if (sp.InsertHyperlinks != null) ws.Protection.Protected = sp.InsertHyperlinks.Value; - if (sp.InsertRows != null) ws.Protection.Protected = sp.InsertRows.Value; - if (sp.DeleteColumns != null) ws.Protection.Protected = sp.DeleteColumns.Value; - if (sp.DeleteRows != null) ws.Protection.Protected = sp.DeleteRows.Value; - if (sp.AutoFilter != null) ws.Protection.Protected = sp.AutoFilter.Value; - if (sp.PivotTables != null) ws.Protection.Protected = sp.PivotTables.Value; - if (sp.Sort != null) ws.Protection.Protected = sp.Sort.Value; - if (sp.SelectLockedCells != null) ws.Protection.Protected = !sp.SelectLockedCells.Value; - if (sp.SelectUnlockedCells != null) ws.Protection.Protected = !sp.SelectUnlockedCells.Value; + if (sp.FormatCells != null) ws.Protection.FormatCells = sp.FormatCells.Value; + if (sp.FormatColumns != null) ws.Protection.FormatColumns = sp.FormatColumns.Value; + if (sp.FormatRows != null) ws.Protection.FormatRows = sp.FormatRows.Value; + if (sp.InsertColumns != null) ws.Protection.InsertColumns = sp.InsertColumns.Value; + if (sp.InsertHyperlinks != null) ws.Protection.InsertHyperlinks = sp.InsertHyperlinks.Value; + if (sp.InsertRows != null) ws.Protection.InsertRows = sp.InsertRows.Value; + if (sp.DeleteColumns != null) ws.Protection.DeleteColumns = sp.DeleteColumns.Value; + if (sp.DeleteRows != null) ws.Protection.DeleteRows = sp.DeleteRows.Value; + if (sp.AutoFilter != null) ws.Protection.AutoFilter = sp.AutoFilter.Value; + if (sp.PivotTables != null) ws.Protection.PivotTables = sp.PivotTables.Value; + if (sp.Sort != null) ws.Protection.Sort = sp.Sort.Value; + if (sp.SelectLockedCells != null) ws.Protection.SelectLockedCells = !sp.SelectLockedCells.Value; + if (sp.SelectUnlockedCells != null) ws.Protection.SelectUnlockedCells = !sp.SelectUnlockedCells.Value; } } @@ -807,7 +811,7 @@ else { xlStylized.InnerStyle.Protection.Hidden = protection.Hidden != null && protection.Hidden.HasValue && protection.Hidden.Value; - xlStylized.InnerStyle.Protection.Locked = protection.Locked != null && protection.Locked.HasValue && protection.Locked.Value; + xlStylized.InnerStyle.Protection.Locked = protection.Locked == null || (protection.Locked != null && protection.Locked.HasValue && protection.Locked.Value); } } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs index f071385..fd89dbd 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs @@ -637,11 +637,13 @@ foreach (var sheet in workbook.Sheets.Elements()) { var sName = sheet.Name.Value; - if (Worksheets.Where(w => w.Name.ToLower() == sName.ToLower()).Any()) + //if (Worksheets.Where(w => w.Name.ToLower() == sName.ToLower()).Any()) + if (Worksheets.Where(w=>(w as XLWorksheet).SheetId == (Int32)sheet.SheetId.Value).Any()) { - var wks = (XLWorksheet)Worksheets.Where(w => w.Name.ToLower() == sName.ToLower()).Single(); - wks.SheetId = (Int32)sheet.SheetId.Value; + var wks = (XLWorksheet)Worksheets.Where(w => (w as XLWorksheet).SheetId == (Int32)sheet.SheetId.Value).Single(); + //wks.SheetId = (Int32)sheet.SheetId.Value; wks.RelId = sheet.Id; + sheet.Name = wks.Name; } } @@ -672,13 +674,17 @@ select sheet; UInt32 firstSheetVisible = 0; - + Boolean foundVisible = false; foreach (var sheet in sheetElements) { workbook.Sheets.RemoveChild(sheet); workbook.Sheets.Append(sheet); - if (firstSheetVisible == 0 && sheet.State != null && sheet.State != SheetStateValues.Visible) - firstSheetVisible++; + + if (!foundVisible) + if (sheet.State == null || sheet.State == SheetStateValues.Visible) + foundVisible = true; + else + firstSheetVisible++; } WorkbookView workbookView = workbook.BookViews.Elements().FirstOrDefault(); diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs index 08c1f91..6ff35f2 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs @@ -154,7 +154,20 @@ public Double ColumnWidth { get; set; } public Double RowHeight { get; set; } - public String Name { get; set; } + private String name; + public String Name + { + get + { + return name; + } + set + { + name = value; + (workbook.Worksheets as XLWorksheets).Rename(name, value); + } + } + public Int32 SheetId { get; set; } public String RelId { get; set; } @@ -541,15 +554,54 @@ { var ws = (XLWorksheet)workbook.Worksheets.Add(newSheetName, position); - this.Internals.CellsCollection.ForEach(kp => (ws.Cell(kp.Value.Address) as XLCell).CopyValues(kp.Value)); - this.Internals.ColumnsCollection.ForEach(kp => ws.Internals.ColumnsCollection.Add(kp)); - this.Internals.MergedRanges.ForEach(kp => ws.Internals.MergedRanges.Add(kp)); - this.Internals.RowsCollection.ForEach(kp => ws.Internals.RowsCollection.Add(kp)); - this.PageSetup = new XLPageSetup(this.PageSetup, ws); - this.Outline = new XLOutline(this.Outline); - this.SheetView = new XLSheetView(this.SheetView); - this.NamedRanges.ForEach(r => ws.NamedRanges.Add(r.Name, r.Ranges)); - this.Tables.ForEach(t => ws.Tables.Add(t)); + this.Internals.CellsCollection.ForEach(kp => (ws.Cell(kp.Value.Address) as XLCell).CopyFrom(kp.Value)); + this.DataValidations.ForEach(dv => ws.DataValidations.Add(new XLDataValidation(dv, ws))); + this.Internals.ColumnsCollection.ForEach(kp => ws.Internals.ColumnsCollection.Add(kp.Key, new XLColumn(kp.Value, ws))); + this.Internals.RowsCollection.ForEach(kp => ws.Internals.RowsCollection.Add(kp.Key, new XLRow(kp.Value, ws))); + ws.Visibility = this.Visibility; + ws.ColumnWidth = this.ColumnWidth; + ws.RowHeight = this.RowHeight; + ws.style = new XLStyle(ws, this.style); + ws.PageSetup = new XLPageSetup(this.PageSetup, ws); + ws.Outline = new XLOutline(this.Outline); + ws.SheetView = new XLSheetView(this.SheetView); + this.Internals.MergedRanges.ForEach(kp => ws.Internals.MergedRanges.Add(ws.Range(kp.RangeAddress.ToString()))); + + foreach (var r in this.NamedRanges) + { + var ranges = new XLRanges(workbook, this.style); + r.Ranges.ForEach(rr => ranges.Add(rr)); + ws.NamedRanges.Add(r.Name, ranges); + } + + foreach(var t in this.Tables) + { + XLTable table; + if (ws.Tables.Where(tt => tt.Name == t.Name).Any()) + table = new XLTable((XLRange)ws.Range(t.RangeAddress.ToString()), true); + else + table = new XLTable((XLRange)ws.Range(t.RangeAddress.ToString()), t.Name, true); + + table.RelId = (t as XLTable).RelId; + table.EmphasizeFirstColumn = t.EmphasizeFirstColumn; + table.EmphasizeLastColumn = t.EmphasizeLastColumn; + table.ShowRowStripes = t.ShowRowStripes; + table.ShowColumnStripes = t.ShowColumnStripes; + table.ShowAutoFilter = t.ShowAutoFilter; + table.Theme = t.Theme; + table.showTotalsRow = t.ShowTotalsRow; + + (t as XLTable).uniqueNames.ForEach(n => table.uniqueNames.Add(n)); + Int32 fieldCount = t.ColumnCount(); + for (Int32 f = 0; f < fieldCount; f++) + { + table.Field(f).Index = t.Field(f).Index; + table.Field(f).Name = t.Field(f).Name; + (table.Field(f) as XLTableField).totalsRowLabel = (t.Field(f) as XLTableField).totalsRowLabel; + (table.Field(f) as XLTableField).totalsRowFunction = (t.Field(f) as XLTableField).totalsRowFunction; + } + } + return ws; } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheets.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheets.cs index 041efab..48dbb6b 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheets.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheets.cs @@ -34,6 +34,16 @@ return worksheets.Values.Where(w => w.Position == position).Single(); } + public void Rename(String oldSheetName, String newSheetName) + { + if (!StringExtensions.IsNullOrWhiteSpace(oldSheetName) && worksheets.ContainsKey(oldSheetName)) + { + var ws = worksheets[oldSheetName]; + worksheets.Remove(oldSheetName); + worksheets.Add(newSheetName, ws); + } + } + public IXLWorksheet Add(String sheetName) { var sheet = new XLWorksheet(sheetName, workbook); diff --git a/ClosedXML/ClosedXML/ClosedXML_Examples/Ranges/UsingTables.cs b/ClosedXML/ClosedXML/ClosedXML_Examples/Ranges/UsingTables.cs index 3373640..b2c6693 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Examples/Ranges/UsingTables.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Examples/Ranges/UsingTables.cs @@ -17,6 +17,7 @@ { var wb = new XLWorkbook(@"C:\Excel Files\Created\BasicTable.xlsx"); var ws = wb.Worksheet(1); + ws.Name = "Contacts Table"; var firstCell = ws.FirstCellUsed(); var lastCell = ws.LastCellUsed(); var range = ws.Range(firstCell.Address, lastCell.Address); diff --git a/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs b/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs index 62c5f75..32e70df 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs @@ -12,20 +12,41 @@ { class Program { - static void Main(string[] args) { //var fileName = "DifferentKinds"; - var fileName = "Sandbox"; - //var fileName = "Issue_6405"; - //var wb = new XLWorkbook(String.Format(@"c:\Excel Files\ForTesting\{0}.xlsx", fileName)); - var wb = new XLWorkbook(); - var ws = wb.Worksheets.Add("Sheet1"); - ws.Range("A1:C3").Style.Border.OutsideBorder = XLBorderStyleValues.Thick; + //var fileName = "Sandbox"; + var fileName = "Issue_6375"; + var wb = new XLWorkbook(String.Format(@"c:\Excel Files\ForTesting\{0}.xlsx", fileName)); + //var wb = new XLWorkbook(); + //wbSource.Worksheet(1).CopyTo(wb, "Sheet1"); + //String source = @"c:\Excel Files\Created\{0}.xlsx"; + + + //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.xlsx", fileName)); } + static void CopyWorksheets(String source, XLWorkbook target) + { + var wb = new XLWorkbook(source); + foreach (var ws in wb.Worksheets) + { + ws.CopyTo(target, ws.Name); + } + } + static void xMain(string[] args) { FillStyles();