diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCells.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCells.cs index 2651162..c554986 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCells.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCells.cs @@ -58,9 +58,9 @@ range.LastAddress.RowNumber, range.LastAddress.ColumnNumber) .Where(c => - !c.IsEmpty(_includeFormats) - || (_includeFormats && c.HasComment) - || (_predicate != null && _predicate(c)) + (!c.IsEmpty(_includeFormats) + || (_includeFormats && c.HasComment) ) + && (_predicate == null || _predicate(c)) ); foreach(var cell in cellRange) @@ -124,9 +124,9 @@ cir => cir.Value.Select(a => cir.Key.Internals.CellsCollection.GetCell(a)).Where( cell => cell != null && ( - !cell.IsEmpty(_includeFormats) - || (_includeFormats && cell.HasComment) - || (_predicate != null && _predicate(cell)) + (!cell.IsEmpty(_includeFormats) + || (_includeFormats && cell.HasComment)) + && (_predicate == null || _predicate(cell)) ))); foreach (var cell in cellRange) diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Comments/IXLComment.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Comments/IXLComment.cs index a4fdb63..cddc435 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Comments/IXLComment.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Comments/IXLComment.cs @@ -19,10 +19,6 @@ /// Adds a bolded line with the author's name /// IXLRichString AddSignature(); - /// - /// Adds a carriage return to the comment - /// - IXLRichString AddNewLine(); void Delete(); } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Comments/XLComment.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Comments/XLComment.cs index ce56266..edead12 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Comments/XLComment.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Comments/XLComment.cs @@ -97,11 +97,7 @@ return AddText(Environment.NewLine); } - public IXLRichString AddNewLine() - { - return AddText(Environment.NewLine); - } - + #region IXLDrawing public Int32 ShapeId { get; internal set; } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/IXLHFItem.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/IXLHFItem.cs index 9883434..d743b8f 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/IXLHFItem.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/IXLHFItem.cs @@ -11,7 +11,7 @@ AllPages, OddPages, EvenPages, FirstPage } - public interface IXLHFItem + public interface IXLHFItem: IXLWithRichString { /// /// Gets the text of the specified header/footer occurrence. @@ -20,12 +20,6 @@ String GetText(XLHFOccurrence occurrence); /// - /// Adds the given text to this header/footer item. - /// - /// The text to add to this header/footer item. - IXLRichString AddText(String text); - - /// /// Adds the given predefined text to this header/footer item. /// /// The predefined text to add to this header/footer item. diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/XLHFItem.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/XLHFItem.cs index 5c9f504..67075e0 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/XLHFItem.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/PageSetup/XLHFItem.cs @@ -36,7 +36,7 @@ public IXLRichString AddText(String text, XLHFOccurrence occurrence) { - IXLRichString richText = new XLRichString(text, XLWorkbook.DefaultStyle.Font); + IXLRichString richText = new XLRichString(text, XLWorkbook.DefaultStyle.Font, this); var hfText = new XLHFText(richText); if (occurrence == XLHFOccurrence.AllPages) @@ -53,6 +53,11 @@ return richText; } + public IXLRichString AddNewLine() + { + return AddText(Environment.NewLine); + } + private void AddTextToOccurrence(XLHFText hfText, XLHFOccurrence occurrence) { if (texts.ContainsKey(occurrence)) diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/IXLFormattedText.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/IXLFormattedText.cs index 70f1c93..0a6fe6d 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/IXLFormattedText.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/IXLFormattedText.cs @@ -3,7 +3,7 @@ namespace ClosedXML.Excel { - public interface IXLFormattedText : IEnumerable, IEquatable> + public interface IXLFormattedText : IEnumerable, IEquatable>, IXLWithRichString { Boolean Bold { set; } Boolean Italic { set; } @@ -27,7 +27,6 @@ IXLFormattedText SetFontName(String value); IXLFormattedText SetFontFamilyNumbering(XLFontFamilyNumberingValues value); - IXLRichString AddText(String text); IXLRichString AddText(String text, IXLFontBase font); IXLFormattedText ClearText(); IXLFormattedText ClearFont(); diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/IXLRichString.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/IXLRichString.cs index 5d62dae..349dbc1 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/IXLRichString.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/IXLRichString.cs @@ -2,9 +2,15 @@ namespace ClosedXML.Excel { - public interface IXLRichString: IXLFontBase, IEquatable + public interface IXLWithRichString + { + IXLRichString AddText(String text); + IXLRichString AddNewLine(); + } + public interface IXLRichString: IXLFontBase, IEquatable, IXLWithRichString { String Text { get; } + IXLRichString SetBold(); IXLRichString SetBold(Boolean value); IXLRichString SetItalic(); IXLRichString SetItalic(Boolean value); diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/XLFormattedText.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/XLFormattedText.cs index 6c879d1..a5afa9b 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/XLFormattedText.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/XLFormattedText.cs @@ -43,7 +43,7 @@ } public IXLRichString AddText(String text, IXLFontBase font) { - var richText = new XLRichString(text, font); + var richText = new XLRichString(text, font, this); return AddText(richText); } @@ -53,6 +53,12 @@ Length += richText.Text.Length; return richText; } + + public IXLRichString AddNewLine() + { + return AddText(Environment.NewLine); + } + public IXLFormattedText ClearText() { _richTexts.Clear(); @@ -98,20 +104,20 @@ Int32 startIndex = index - lastPosition; if (startIndex > 0) - newRichTexts.Add(new XLRichString(rt.Text.Substring(0, startIndex), rt)); + newRichTexts.Add(new XLRichString(rt.Text.Substring(0, startIndex), rt, this)); else if (startIndex < 0) startIndex = 0; Int32 leftToTake = length - retVal.Length; if (leftToTake > rt.Text.Length - startIndex) leftToTake = rt.Text.Length - startIndex; - - XLRichString newRt = new XLRichString(rt.Text.Substring(startIndex, leftToTake), rt); + + XLRichString newRt = new XLRichString(rt.Text.Substring(startIndex, leftToTake), rt, this); newRichTexts.Add(newRt); retVal.AddText(newRt); if (startIndex + leftToTake < rt.Text.Length) - newRichTexts.Add(new XLRichString(rt.Text.Substring(startIndex + leftToTake), rt)); + newRichTexts.Add(new XLRichString(rt.Text.Substring(startIndex + leftToTake), rt, this)); } else // We haven't reached the desired position yet { diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/XLRichString.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/XLRichString.cs index 2615dff..8d21b74 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/XLRichString.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/RichText/XLRichString.cs @@ -4,15 +4,24 @@ { internal class XLRichString: IXLRichString { - - public XLRichString(String text, IXLFontBase font) + IXLWithRichString _withRichString; + public XLRichString(String text, IXLFontBase font, IXLWithRichString withRichString) { Text = text; this.CopyFont(font); + _withRichString = withRichString; } public String Text { get; private set; } + public IXLRichString AddText(String text) + { + return _withRichString.AddText(text); + } + public IXLRichString AddNewLine() + { + return AddText(Environment.NewLine); + } public Boolean Bold { get; set; } public Boolean Italic { get; set; } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs index fa6cf34..fb2936e 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs @@ -383,7 +383,7 @@ var dashStyle = stroke.Attribute("dashstyle"); if (dashStyle != null) { - String dashStyleVal = dashStyle.Value; + String dashStyleVal = dashStyle.Value.ToLower(); if (dashStyleVal == "1 1" || dashStyleVal == "shortdot") { var endCap = stroke.Attribute("endcap"); @@ -397,10 +397,10 @@ switch (dashStyleVal) { case "dash": drawing.Style.ColorsAndLines.LineDash = XLDashStyle.Dash; break; - case "dashDot": drawing.Style.ColorsAndLines.LineDash = XLDashStyle.DashDot; break; - case "longDash": drawing.Style.ColorsAndLines.LineDash = XLDashStyle.LongDash; break; - case "longDashDot": drawing.Style.ColorsAndLines.LineDash = XLDashStyle.LongDashDot; break; - case "longDashDotDot": drawing.Style.ColorsAndLines.LineDash = XLDashStyle.LongDashDotDot; break; + case "dashdot": drawing.Style.ColorsAndLines.LineDash = XLDashStyle.DashDot; break; + case "longdash": drawing.Style.ColorsAndLines.LineDash = XLDashStyle.LongDash; break; + case "longdashdot": drawing.Style.ColorsAndLines.LineDash = XLDashStyle.LongDashDot; break; + case "longdashdotdot": drawing.Style.ColorsAndLines.LineDash = XLDashStyle.LongDashDotDot; break; } } } @@ -476,6 +476,23 @@ var visible = clientData.Elements().FirstOrDefault(e => e.Name.LocalName == "Visible"); drawing.Visible = !(visible != null && visible.Value.ToLower() == "false"); + + LoadDrawingHAlignment(drawing, clientData); + LoadDrawingVAlignment(drawing, clientData); + } + + private void LoadDrawingHAlignment(IXLDrawing drawing, XElement clientData) + { + var textHAlign = clientData.Elements().FirstOrDefault(e => e.Name.LocalName == "TextHAlign"); + if (textHAlign != null) + drawing.Style.Alignment.Horizontal = (XLDrawingHorizontalAlignment)Enum.Parse(typeof(XLDrawingHorizontalAlignment), textHAlign.Value.ToProper()); + } + + private void LoadDrawingVAlignment(IXLDrawing drawing, XElement clientData) + { + var textVAlign = clientData.Elements().FirstOrDefault(e => e.Name.LocalName == "TextVAlign"); + if (textVAlign != null) + drawing.Style.Alignment.Vertical = (XLDrawingVerticalAlignment)Enum.Parse(typeof(XLDrawingVerticalAlignment), textVAlign.Value.ToProper()); } private void LoadDrawingProtection(IXLDrawing drawing, XElement clientData) @@ -493,14 +510,14 @@ { var moveWithCellsElement = clientData.Elements().FirstOrDefault(e => e.Name.LocalName == "MoveWithCells"); var sizeWithCellsElement = clientData.Elements().FirstOrDefault(e => e.Name.LocalName == "SizeWithCells"); - Boolean moveWithCells = moveWithCellsElement != null && moveWithCellsElement.Value.ToLower() == "true"; - Boolean sizeWithCells = sizeWithCellsElement != null && sizeWithCellsElement.Value.ToLower() == "true"; + Boolean moveWithCells = !(moveWithCellsElement != null && moveWithCellsElement.Value.ToLower() == "true"); + Boolean sizeWithCells = !(sizeWithCellsElement != null && sizeWithCellsElement.Value.ToLower() == "true"); if (moveWithCells && !sizeWithCells) drawing.Style.Properties.Positioning = XLDrawingAnchor.MoveWithCells; else if (moveWithCells && sizeWithCells) drawing.Style.Properties.Positioning = XLDrawingAnchor.MoveAndSizeWithCells; else - drawing.Style.Properties.Positioning = XLDrawingAnchor.MoveWithCells; + drawing.Style.Properties.Positioning = XLDrawingAnchor.Absolute; } private static void LoadClientDataAnchor(IXLDrawing drawing, XElement anchor) diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs index d1b4844..7824682 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs @@ -164,7 +164,11 @@ if (vmlDrawingPart == null) { if (StringExtensions.IsNullOrWhiteSpace(worksheet.LegacyDrawingId)) + { worksheet.LegacyDrawingId = context.RelIdGenerator.GetNext(RelType.Worksheet); + worksheet.LegacyDrawingIsNew = true; + } + vmlDrawingPart = worksheetPart.AddNewPart(worksheet.LegacyDrawingId); } GenerateVmlDrawingPartContent(vmlDrawingPart, worksheet, context); @@ -218,28 +222,31 @@ if (vmlDrawingPart != null) { XDocument xdoc = XDocumentExtensions.Load(vmlDrawingPart.GetStream(FileMode.Open)); - xdoc.Root.Elements().Where(e => e.Name.LocalName == "shapelayout").Remove(); + //xdoc.Root.Elements().Where(e => e.Name.LocalName == "shapelayout").Remove(); xdoc.Root.Elements().Where(e=>e.Name.LocalName == "shapetype" && (string)e.Attribute("id") == @"_x0000_t202").Remove(); xdoc.Root.Elements().Where(e => e.Name.LocalName == "shape" && (string)e.Attribute("type") == @"#_x0000_t202").Remove(); var imageParts = vmlDrawingPart.ImageParts.ToList(); var legacyParts = vmlDrawingPart.LegacyDiagramTextParts.ToList(); + var rId = worksheetPart.GetIdOfPart(vmlDrawingPart); + worksheet.LegacyDrawingId = rId; worksheetPart.ChangeIdOfPart(vmlDrawingPart, "xxRRxx"); // Anything will do for the new relationship id - // we just want it alive enough to create the copy - - Boolean hasShapes = xdoc.Root.Elements().Where(e => e.Name.LocalName == "shape").Any(); - if (imageParts.Count > 0 || legacyParts.Count > 0 || hasShapes) + // we just want it alive enough to create the copy + + Boolean hasShapes = xdoc.Root.Elements().Where(e => + e.Name.LocalName == "shape" + || e.Name.LocalName == "group" + ).Any(); + + VmlDrawingPart vmlDrawingPartNew = null; + Boolean hasNewPart = (imageParts.Count > 0 || legacyParts.Count > 0 || hasShapes); + if (hasNewPart) { - if (StringExtensions.IsNullOrWhiteSpace(worksheet.LegacyDrawingId)) - worksheet.LegacyDrawingId = context.RelIdGenerator.GetNext(RelType.Worksheet); - VmlDrawingPart vmlDrawingPartNew = worksheetPart.AddNewPart(worksheet.LegacyDrawingId); + vmlDrawingPartNew = worksheetPart.AddNewPart(rId); - if (hasShapes) + using (XmlTextWriter writer = new XmlTextWriter(vmlDrawingPartNew.GetStream(FileMode.Create), Encoding.UTF8)) { - using (XmlTextWriter writer = new XmlTextWriter(vmlDrawingPartNew.GetStream(FileMode.Create), Encoding.UTF8)) - { - writer.WriteRaw(xdoc.ToString()); - } + writer.WriteRaw(xdoc.ToString()); } imageParts.ForEach(p => vmlDrawingPartNew.AddPart(p, vmlDrawingPart.GetIdOfPart(p))); @@ -247,6 +254,9 @@ } worksheetPart.DeletePart(vmlDrawingPart); + + if (hasNewPart && rId != worksheetPart.GetIdOfPart(vmlDrawingPartNew)) + worksheetPart.ChangeIdOfPart(vmlDrawingPartNew, rId); } } @@ -534,10 +544,10 @@ current + ("'" + worksheetName + "'!" + printArea.RangeAddress. - FirstAddress.ToStringFixed() + + FirstAddress.ToStringFixed(XLReferenceStyle.A1) + ":" + printArea.RangeAddress. - LastAddress.ToStringFixed() + + LastAddress.ToStringFixed(XLReferenceStyle.A1) + ",")); definedName.Text = definedNameText.Substring(0, definedNameText.Length - 1); definedNames.AppendChild(definedName); @@ -546,8 +556,8 @@ if (worksheet.AutoFilter.Enabled) { var definedName = new DefinedName { Name = "_xlnm._FilterDatabase", LocalSheetId = sheetId }; - definedName.Text = "'" + worksheet.Name + "'!" + worksheet.AutoFilter.Range.RangeAddress.FirstAddress.ToStringFixed() + - ":" + worksheet.AutoFilter.Range.RangeAddress.LastAddress.ToStringFixed(); + definedName.Text = "'" + worksheet.Name + "'!" + worksheet.AutoFilter.Range.RangeAddress.FirstAddress.ToStringFixed(XLReferenceStyle.A1) + + ":" + worksheet.AutoFilter.Range.RangeAddress.LastAddress.ToStringFixed(XLReferenceStyle.A1); definedName.Hidden = BooleanValue.FromBoolean(true); definedNames.AppendChild(definedName); } @@ -3373,16 +3383,33 @@ #endregion #region LegacyDrawing - worksheetPart.Worksheet.RemoveAllChildren(); + if (xlWorksheet.LegacyDrawingIsNew) { - if (!StringExtensions.IsNullOrWhiteSpace(xlWorksheet.LegacyDrawingId)) + worksheetPart.Worksheet.RemoveAllChildren(); { - var previousElement = cm.GetPreviousElementFor(XLWSContentManager.XLWSContents.LegacyDrawing); - worksheetPart.Worksheet.InsertAfter(new LegacyDrawing { Id = xlWorksheet.LegacyDrawingId }, - previousElement); + if (!StringExtensions.IsNullOrWhiteSpace(xlWorksheet.LegacyDrawingId)) + { + var previousElement = cm.GetPreviousElementFor(XLWSContentManager.XLWSContents.LegacyDrawing); + worksheetPart.Worksheet.InsertAfter(new LegacyDrawing { Id = xlWorksheet.LegacyDrawingId }, + previousElement); + } } } #endregion + + + #region LegacyDrawingHeaderFooter + //LegacyDrawingHeaderFooter legacyHeaderFooter = worksheetPart.Worksheet.Elements().FirstOrDefault(); + //if (legacyHeaderFooter != null) + //{ + // worksheetPart.Worksheet.RemoveAllChildren(); + // { + // var previousElement = cm.GetPreviousElementFor(XLWSContentManager.XLWSContents.LegacyDrawingHeaderFooter); + // worksheetPart.Worksheet.InsertAfter(new LegacyDrawingHeaderFooter { Id = xlWorksheet.LegacyDrawingId }, + // previousElement); + // } + //} + #endregion } private static void PopulateAutoFilter(XLAutoFilter xlAutoFilter, AutoFilter autoFilter) @@ -4449,11 +4476,11 @@ new Vml.Path() { ConnectionPointType = Vml.Office.ConnectValues.None }, textBox, new Vml.Spreadsheet.ClientData( - new Vml.Spreadsheet.MoveWithCells(c.Comment.Style.Properties.Positioning == XLDrawingAnchor.Absolute ? "True": "False"), // counterintuitive - new Vml.Spreadsheet.ResizeWithCells(c.Comment.Style.Properties.Positioning == XLDrawingAnchor.MoveAndSizeWithCells ? "False": "True"), // counterintuitive + new Vml.Spreadsheet.MoveWithCells(c.Comment.Style.Properties.Positioning == XLDrawingAnchor.Absolute ? "True": "False"), // Counterintuitive + new Vml.Spreadsheet.ResizeWithCells(c.Comment.Style.Properties.Positioning == XLDrawingAnchor.MoveAndSizeWithCells ? "False" : "True"), // Counterintuitive anchor, - new Vml.Spreadsheet.HorizontalTextAlignment(c.Comment.Style.Alignment.Horizontal.ToString()), - new Vml.Spreadsheet.VerticalTextAlignment(c.Comment.Style.Alignment.Vertical.ToString()), + new Vml.Spreadsheet.HorizontalTextAlignment(c.Comment.Style.Alignment.Horizontal.ToString().ToCamel()), + new Vml.Spreadsheet.VerticalTextAlignment(c.Comment.Style.Alignment.Vertical.ToString().ToCamel()), new Vml.Spreadsheet.AutoFill("False"), new Vml.Spreadsheet.CommentRowTarget() { Text = (rowNumber - 1).ToString() }, new Vml.Spreadsheet.CommentColumnTarget() { Text = (columnNumber - 1).ToString() }, @@ -4482,7 +4509,7 @@ { var lineDash = c.Comment.Style.ColorsAndLines.LineDash; var stroke = new Vml.Stroke() { LineStyle = c.Comment.Style.ColorsAndLines.LineStyle.ToOpenXml(), - DashStyle= lineDash == XLDashStyle.RoundDot || lineDash == XLDashStyle.SquareDot ? "shortdot" : lineDash.ToString().ToLower() + DashStyle = lineDash == XLDashStyle.RoundDot || lineDash == XLDashStyle.SquareDot ? "shortDot" : lineDash.ToString().ToCamel() }; if (lineDash == XLDashStyle.RoundDot) stroke.EndCap = Vml.StrokeEndCapValues.Round; diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs index ae4bf40..0f298b4 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs @@ -158,6 +158,7 @@ private Double _columnWidth; public string LegacyDrawingId; + public Boolean LegacyDrawingIsNew; public Double ColumnWidth { diff --git a/ClosedXML/ClosedXML/ClosedXML/Extensions.cs b/ClosedXML/ClosedXML/ClosedXML/Extensions.cs index 951b4ae..5285eae 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Extensions.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Extensions.cs @@ -134,6 +134,28 @@ { return value.StartsWith(" ") || value.EndsWith(" ") || value.Contains(Environment.NewLine); } + + public static String ToCamel(this String value) + { + if (value.Length == 0) + return value; + + if (value.Length == 1) + return value.ToLower(); + + return value.Substring(0, 1).ToLower() + value.Substring(1); + } + + public static String ToProper(this String value) + { + if (value.Length == 0) + return value; + + if (value.Length == 1) + return value.ToUpper(); + + return value.Substring(0, 1).ToUpper() + value.Substring(1); + } } public static class DateTimeExtensions diff --git a/ClosedXML/ClosedXML/ClosedXML_Examples/Comments/AddingComments.cs b/ClosedXML/ClosedXML/ClosedXML_Examples/Comments/AddingComments.cs index bce0c0e..dba1b3f 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Examples/Comments/AddingComments.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Examples/Comments/AddingComments.cs @@ -15,21 +15,140 @@ var wb = new XLWorkbook(); AddMiscComments(wb); AddVisibilityComments(wb); + AddPosition(wb); + AddSignatures(wb); AddStyleAlignment(wb); + AddColorsAndLines(wb); + AddMagins(wb); + AddProperties(wb); + AddProtection(wb); + AddSize(wb); + AddWeb(wb); + wb.SaveAs(filePath); } + private void AddWeb(XLWorkbook wb) + { + var ws = wb.Worksheets.Add("Web"); + ws.Cell("A1").Comment.Style.Web.AlternateText = "The alternate text in case you need it."; + } + + private void AddSize(XLWorkbook wb) + { + var ws = wb.Worksheets.Add("Size"); + + // Automatic size is a copy of the property comment.Style.Alignment.AutomaticSize + // I created the duplicate because it makes more sense for it to be in Size + // but Excel has it under the Alignment tab. + ws.Cell("A2").Comment.AddText("Things are very tight around here."); + ws.Cell("A2").Comment.Style.Size.SetAutomaticSize(); + + ws.Cell("A4").Comment.AddText("Different size"); + ws.Cell("A4").Comment.Style + .Size.SetHeight(30) // The height is set in the same units as row.Height + .Size.SetWidth(30); // The width is set in the same units as row.Width + + // Set all comments to visible + ws.CellsUsed(true, c => c.HasComment).ForEach(c => c.Comment.SetVisible()); + } + + private void AddProtection(XLWorkbook wb) + { + var ws = wb.Worksheets.Add("Protection"); + + ws.Cell("A1").Comment.Style + .Protection.SetLocked(false) + .Protection.SetLockText(false); + } + + private void AddProperties(XLWorkbook wb) + { + var ws = wb.Worksheets.Add("Properties"); + + ws.Cell("A1").Comment.Style.Properties.Positioning = XLDrawingAnchor.Absolute; + ws.Cell("A2").Comment.Style.Properties.Positioning = XLDrawingAnchor.MoveAndSizeWithCells; + ws.Cell("A3").Comment.Style.Properties.Positioning = XLDrawingAnchor.MoveWithCells; + } + + private void AddMagins(XLWorkbook wb) + { + var ws = wb.Worksheets.Add("Margins"); + + ws.Cell("A2").Comment + .SetVisible() + .AddText("Lorem ipsum dolor sit amet, adipiscing elit. ").AddNewLine() + .AddText("Nunc elementum, sapien a ultrices, commodo nisl. ").AddNewLine() + .AddText("Consequat erat lectus a nisi. Aliquam facilisis."); + + ws.Cell("A2").Comment.Style + .Margins.SetAll(0.25) + .Size.SetAutomaticSize(); + } + + private void AddColorsAndLines(XLWorkbook wb) + { + var ws = wb.Worksheets.Add("Colors and Lines"); + + ws.Cell("A2").Comment + .AddText("Now ") + .AddText("THIS").SetBold().SetFontColor(XLColor.Red) + .AddText(" is colorful!"); + ws.Cell("A2").Comment.Style + .ColorsAndLines.SetFillColor(XLColor.RichCarmine) + .ColorsAndLines.SetFillTransparency(0.25) // 25% opaque + .ColorsAndLines.SetLineColor(XLColor.Blue) + .ColorsAndLines.SetLineTransparency(0.75) // 75% opaque + .ColorsAndLines.SetLineDash(XLDashStyle.LongDash) + .ColorsAndLines.SetLineStyle(XLLineStyle.ThickBetweenThin) + .ColorsAndLines.SetLineWeight(7.5); + + // Set all comments to visible + ws.CellsUsed(true, c => c.HasComment).ForEach(c => c.Comment.SetVisible()); + } + private void AddStyleAlignment(XLWorkbook wb) { - var ws = wb.Worksheets.Add("Style Alignment"); + var ws = wb.Worksheets.Add("Alignment"); // Automagically adjust the size of the comment to fit the contents ws.Cell("A1").Comment.Style.Alignment.SetAutomaticSize(); ws.Cell("A1").Comment.AddText("Things are pretty tight around here"); + // Default values + ws.Cell("A3").Comment + .AddText("Default Alignments:").AddNewLine() + .AddText("Vertical = Top").AddNewLine() + .AddText("Horizontal = Left").AddNewLine() + .AddText("Orientation = Left to Right"); + + // Let's change the alignments + ws.Cell("A8").Comment + .AddText("Vertical = Bottom").AddNewLine() + .AddText("Horizontal = Right"); + ws.Cell("A8").Comment.Style + .Alignment.SetVertical(XLDrawingVerticalAlignment.Bottom) + .Alignment.SetHorizontal(XLDrawingHorizontalAlignment.Right); + + // And now the orientation... + ws.Cell("D3").Comment.AddText("Orientation = Bottom to Top"); + ws.Cell("D3").Comment.Style + .Alignment.SetOrientation(XLDrawingTextOrientation.BottomToTop) + .Alignment.SetAutomaticSize(); + + ws.Cell("E3").Comment.AddText("Orientation = Top to Bottom"); + ws.Cell("E3").Comment.Style + .Alignment.SetOrientation(XLDrawingTextOrientation.TopToBottom) + .Alignment.SetAutomaticSize(); + + ws.Cell("F3").Comment.AddText("Orientation = Vertical"); + ws.Cell("F3").Comment.Style + .Alignment.SetOrientation(XLDrawingTextOrientation.Vertical) + .Alignment.SetAutomaticSize(); + // Set all comments to visible - ws.CellsUsed(c => c.HasComment).ForEach(c => c.Comment.SetVisible()); + ws.CellsUsed(true, c => c.HasComment).ForEach(c => c.Comment.SetVisible()); } private static void AddMiscComments(XLWorkbook wb) @@ -96,8 +215,10 @@ .Protection.SetLockText(false) .Web.SetAlternateText("This won't be released to the web"); + ws.Cell("A9").Comment.SetAuthor("MDeLeon").AddSignature().AddText("Something"); + ws.Cell("A9").Comment.SetBold().SetFontColor(XLColor.DarkBlue); - ws.CellsUsed(c => !c.Address.ToStringRelative().Equals("A1") && c.HasComment).ForEach(c => c.Comment.SetVisible()); + ws.CellsUsed(true, c => !c.Address.ToStringRelative().Equals("A1") && c.HasComment).ForEach(c => c.Comment.SetVisible()); } private static void AddVisibilityComments(XLWorkbook wb) @@ -105,7 +226,7 @@ var ws = wb.Worksheets.Add("Visibility"); // By default comments are hidden - ws.Cell("A1").SetValue("I have a comment").Comment.AddText("Hidden"); + ws.Cell("A1").SetValue("I have a hidden comment").Comment.AddText("Hidden"); // Set the comment as visible ws.Cell("A2").Comment.SetVisible().AddText("Visible"); @@ -120,9 +241,44 @@ ws.Cell("A4").Comment.Style.Alignment.SetVertical(XLDrawingVerticalAlignment.Bottom); // Alternatively you could set all comments to visible with the following line: - // ws.CellsUsed(c => c.HasComment).ForEach(c => c.Comment.SetVisible()); + // ws.CellsUsed(true, c => c.HasComment).ForEach(c => c.Comment.SetVisible()); ws.Columns().AdjustToContents(); } + + private void AddPosition(XLWorkbook wb) + { + var ws = wb.Worksheets.Add("Position"); + + ws.Columns().Width = 10; + + ws.Cell("A1").Comment.AddText("This is an unusual place for a comment..."); + ws.Cell("A1").Comment.Position + .SetColumn(3) // Starting from the third column + .SetColumnOffset(5) // The comment will start in the middle of the third column + .SetRow(5) // Starting from the fifth row + .SetRowOffset(7.5); // The comment will start in the middle of the fifth row + + // Set all comments to visible + ws.CellsUsed(true, c => c.HasComment).ForEach(c => c.Comment.SetVisible()); + } + + private void AddSignatures(XLWorkbook wb) + { + var ws = wb.Worksheets.Add("Signatures"); + + // By default the signature will be with the logged user + // ws.Cell("A2").Comment.AddSignature().AddText("Hello World!"); + + // You can override this by specifying the comment's author: + ws.Cell("A2").Comment + .SetAuthor("MDeLeon") + .AddSignature() + .AddText("Hello World!"); + + + // Set all comments to visible + ws.CellsUsed(true, c => c.HasComment).ForEach(c => c.Comment.SetVisible()); + } } } diff --git a/ClosedXML/ClosedXML/ClosedXML_Examples/Creating/CreateFiles.cs b/ClosedXML/ClosedXML/ClosedXML_Examples/Creating/CreateFiles.cs index b94fea7..c39608a 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Examples/Creating/CreateFiles.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Examples/Creating/CreateFiles.cs @@ -11,76 +11,76 @@ { public static void CreateAllFiles() { - //new HelloWorld().Create(@"C:\Excel Files\Created\HelloWorld.xlsx"); - //new BasicTable().Create(@"C:\Excel Files\Created\BasicTable.xlsx"); + new HelloWorld().Create(@"C:\Excel Files\Created\HelloWorld.xlsx"); + new BasicTable().Create(@"C:\Excel Files\Created\BasicTable.xlsx"); - //new StyleExamples().Create(); - //new ChangingBasicTable().Create(@"C:\Excel Files\Created\BasicTable_Modified.xlsx"); - //new ShiftingRanges().Create(@"C:\Excel Files\Created\ShiftingRanges.xlsx"); - //new ColumnSettings().Create(@"C:\Excel Files\Created\ColumnSettings.xlsx"); - //new RowSettings().Create(@"C:\Excel Files\Created\RowSettings.xlsx"); - //new MergeCells().Create(@"C:\Excel Files\Created\MergedCells.xlsx"); - //new InsertRows().Create(@"C:\Excel Files\Created\InsertRows.xlsx"); - //new InsertColumns().Create(@"C:\Excel Files\Created\InsertColumns.xlsx"); - //new ColumnCollection().Create(@"C:\Excel Files\Created\ColumnCollection.xlsx"); - //new DataTypes().Create(@"C:\Excel Files\Created\DataTypes.xlsx"); - //new MultipleSheets().Create(@"C:\Excel Files\Created\MultipleSheets.xlsx"); - //new RowCollection().Create(@"C:\Excel Files\Created\RowCollection.xlsx"); - //new DefiningRanges().Create(@"C:\Excel Files\Created\DefiningRanges.xlsx"); - //new ClearingRanges().Create(@"C:\Excel Files\Created\ClearingRanges.xlsx"); - //new DeletingRanges().Create(@"C:\Excel Files\Created\DeletingRanges.xlsx"); - //new Margins().Create(@"C:\Excel Files\Created\Margins.xlsx"); - //new Page().Create(@"C:\Excel Files\Created\Page.xlsx"); - //new HeaderFooters().Create(@"C:\Excel Files\Created\HeaderFooters.xlsx"); - //new Sheets().Create(@"C:\Excel Files\Created\Sheets.xlsx"); - //new SheetTab().Create(@"C:\Excel Files\Created\SheetTab.xlsx"); - //new MultipleRanges().Create(@"C:\Excel Files\Created\MultipleRanges.xlsx"); - //new StyleWorksheet().Create(@"C:\Excel Files\Created\StyleWorksheet.xlsx"); - //new StyleRowsColumns().Create(@"C:\Excel Files\Created\StyleRowsColumns.xlsx"); - //new InsertingDeletingRows().Create(@"C:\Excel Files\Created\InsertingDeletingRows.xlsx"); - //new InsertingDeletingColumns().Create(@"C:\Excel Files\Created\InsertingDeletingColumns.xlsx"); - //new DeletingColumns().Create(@"C:\Excel Files\Created\DeletingColumns.xlsx"); - //new CellValues().Create(@"C:\Excel Files\Created\CellValues.xlsx"); - //new LambdaExpressions().Create(@"C:\Excel Files\Created\LambdaExpressions.xlsx"); - //new DefaultStyles().Create(@"C:\Excel Files\Created\DefaultStyles.xlsx"); - //new TransposeRanges().Create(@"C:\Excel Files\Created\TransposeRanges.xlsx"); - //new TransposeRangesPlus().Create(@"C:\Excel Files\Created\TransposeRangesPlus.xlsx"); - //new MergeMoves().Create(@"C:\Excel Files\Created\MergedMoves.xlsx"); - //new WorkbookProperties().Create(@"C:\Excel Files\Created\WorkbookProperties.xlsx"); - //new AdjustToContents().Create(@"C:\Excel Files\Created\AdjustToContents.xlsx"); - //new HideUnhide().Create(@"C:\Excel Files\Created\HideUnhide.xlsx"); - //new Outline().Create(@"C:\Excel Files\Created\Outline.xlsx"); - //new Formulas().Create(@"C:\Excel Files\Created\Formulas.xlsx"); - //new Collections().Create(@"C:\Excel Files\Created\Collections.xlsx"); - //new NamedRanges().Create(@"C:\Excel Files\Created\NamedRanges.xlsx"); - //new CopyingRanges().Create(@"C:\Excel Files\Created\CopyingRanges.xlsx"); - //new BlankCells().Create(@"C:\Excel Files\Created\BlankCells.xlsx"); - //new TwoPages().Create(@"C:\Excel Files\Created\TwoPages.xlsx"); - //new UsingColors().Create(@"C:\Excel Files\Created\UsingColors.xlsx"); + new StyleExamples().Create(); + new ChangingBasicTable().Create(@"C:\Excel Files\Created\BasicTable_Modified.xlsx"); + new ShiftingRanges().Create(@"C:\Excel Files\Created\ShiftingRanges.xlsx"); + new ColumnSettings().Create(@"C:\Excel Files\Created\ColumnSettings.xlsx"); + new RowSettings().Create(@"C:\Excel Files\Created\RowSettings.xlsx"); + new MergeCells().Create(@"C:\Excel Files\Created\MergedCells.xlsx"); + new InsertRows().Create(@"C:\Excel Files\Created\InsertRows.xlsx"); + new InsertColumns().Create(@"C:\Excel Files\Created\InsertColumns.xlsx"); + new ColumnCollection().Create(@"C:\Excel Files\Created\ColumnCollection.xlsx"); + new DataTypes().Create(@"C:\Excel Files\Created\DataTypes.xlsx"); + new MultipleSheets().Create(@"C:\Excel Files\Created\MultipleSheets.xlsx"); + new RowCollection().Create(@"C:\Excel Files\Created\RowCollection.xlsx"); + new DefiningRanges().Create(@"C:\Excel Files\Created\DefiningRanges.xlsx"); + new ClearingRanges().Create(@"C:\Excel Files\Created\ClearingRanges.xlsx"); + new DeletingRanges().Create(@"C:\Excel Files\Created\DeletingRanges.xlsx"); + new Margins().Create(@"C:\Excel Files\Created\Margins.xlsx"); + new Page().Create(@"C:\Excel Files\Created\Page.xlsx"); + new HeaderFooters().Create(@"C:\Excel Files\Created\HeaderFooters.xlsx"); + new Sheets().Create(@"C:\Excel Files\Created\Sheets.xlsx"); + new SheetTab().Create(@"C:\Excel Files\Created\SheetTab.xlsx"); + new MultipleRanges().Create(@"C:\Excel Files\Created\MultipleRanges.xlsx"); + new StyleWorksheet().Create(@"C:\Excel Files\Created\StyleWorksheet.xlsx"); + new StyleRowsColumns().Create(@"C:\Excel Files\Created\StyleRowsColumns.xlsx"); + new InsertingDeletingRows().Create(@"C:\Excel Files\Created\InsertingDeletingRows.xlsx"); + new InsertingDeletingColumns().Create(@"C:\Excel Files\Created\InsertingDeletingColumns.xlsx"); + new DeletingColumns().Create(@"C:\Excel Files\Created\DeletingColumns.xlsx"); + new CellValues().Create(@"C:\Excel Files\Created\CellValues.xlsx"); + new LambdaExpressions().Create(@"C:\Excel Files\Created\LambdaExpressions.xlsx"); + new DefaultStyles().Create(@"C:\Excel Files\Created\DefaultStyles.xlsx"); + new TransposeRanges().Create(@"C:\Excel Files\Created\TransposeRanges.xlsx"); + new TransposeRangesPlus().Create(@"C:\Excel Files\Created\TransposeRangesPlus.xlsx"); + new MergeMoves().Create(@"C:\Excel Files\Created\MergedMoves.xlsx"); + new WorkbookProperties().Create(@"C:\Excel Files\Created\WorkbookProperties.xlsx"); + new AdjustToContents().Create(@"C:\Excel Files\Created\AdjustToContents.xlsx"); + new HideUnhide().Create(@"C:\Excel Files\Created\HideUnhide.xlsx"); + new Outline().Create(@"C:\Excel Files\Created\Outline.xlsx"); + new Formulas().Create(@"C:\Excel Files\Created\Formulas.xlsx"); + new Collections().Create(@"C:\Excel Files\Created\Collections.xlsx"); + new NamedRanges().Create(@"C:\Excel Files\Created\NamedRanges.xlsx"); + new CopyingRanges().Create(@"C:\Excel Files\Created\CopyingRanges.xlsx"); + new BlankCells().Create(@"C:\Excel Files\Created\BlankCells.xlsx"); + new TwoPages().Create(@"C:\Excel Files\Created\TwoPages.xlsx"); + new UsingColors().Create(@"C:\Excel Files\Created\UsingColors.xlsx"); - //new ColumnCells().Create(@"C:\Excel Files\Created\ColumnCells.xlsx"); - //new RowCells().Create(@"C:\Excel Files\Created\RowCells.xlsx"); - //new FreezePanes().Create(@"C:\Excel Files\Created\FreezePanes.xlsx"); - //new UsingTables().Create(@"C:\Excel Files\Created\UsingTables.xlsx"); - //new ShowCase().Create(@"C:\Excel Files\Created\ShowCase.xlsx"); - //new CopyingWorksheets().Create(@"C:\Excel Files\Created\CopyingWorksheets.xlsx"); - //new InsertingTables().Create(@"C:\Excel Files\Created\InsertingTables.xlsx"); - //new InsertingData().Create(@"C:\Excel Files\Created\InsertingData.xlsx"); - //new Hyperlinks().Create(@"C:\Excel Files\Created\Hyperlinks.xlsx"); - //new DataValidation().Create(@"C:\Excel Files\Created\DataValidation.xlsx"); - //new HideSheets().Create(@"C:\Excel Files\Created\HideSheets.xlsx"); - //new SheetProtection().Create(@"C:\Excel Files\Created\SheetProtection.xlsx"); - //new AutoFilter().Create(@"C:\Excel Files\Created\AutoFilter.xlsx"); - //new Sorting().Create(@"C:\Excel Files\Created\Sorting.xlsx"); - //new SortExample().Create(@"C:\Excel Files\Created\SortExample.xlsx"); - //new AddingDataSet().Create(@"C:\Excel Files\Created\AddingDataSet.xlsx"); - //new AddingDataTableAsWorksheet().Create(@"C:\Excel Files\Created\AddingDataTableAsWorksheet.xlsx"); - //new TabColors().Create(@"C:\Excel Files\Created\TabColors.xlsx"); - //new ShiftingFormulas().Create(@"C:\Excel Files\Created\ShiftingFormulas.xlsx"); - //new CopyingRowsAndColumns().Create(@"C:\Excel Files\Created\CopyingRowsAndColumns.xlsx"); - //new UsingRichText().Create(@"C:\Excel Files\Created\UsingRichText.xlsx"); - //new UsingPhonetics().Create(@"C:\Excel Files\Created\UsingPhonetics.xlsx"); - //new WalkingRanges().Create(@"C:\Excel Files\Created\CellMoves.xlsx"); + new ColumnCells().Create(@"C:\Excel Files\Created\ColumnCells.xlsx"); + new RowCells().Create(@"C:\Excel Files\Created\RowCells.xlsx"); + new FreezePanes().Create(@"C:\Excel Files\Created\FreezePanes.xlsx"); + new UsingTables().Create(@"C:\Excel Files\Created\UsingTables.xlsx"); + new ShowCase().Create(@"C:\Excel Files\Created\ShowCase.xlsx"); + new CopyingWorksheets().Create(@"C:\Excel Files\Created\CopyingWorksheets.xlsx"); + new InsertingTables().Create(@"C:\Excel Files\Created\InsertingTables.xlsx"); + new InsertingData().Create(@"C:\Excel Files\Created\InsertingData.xlsx"); + new Hyperlinks().Create(@"C:\Excel Files\Created\Hyperlinks.xlsx"); + new DataValidation().Create(@"C:\Excel Files\Created\DataValidation.xlsx"); + new HideSheets().Create(@"C:\Excel Files\Created\HideSheets.xlsx"); + new SheetProtection().Create(@"C:\Excel Files\Created\SheetProtection.xlsx"); + new AutoFilter().Create(@"C:\Excel Files\Created\AutoFilter.xlsx"); + new Sorting().Create(@"C:\Excel Files\Created\Sorting.xlsx"); + new SortExample().Create(@"C:\Excel Files\Created\SortExample.xlsx"); + new AddingDataSet().Create(@"C:\Excel Files\Created\AddingDataSet.xlsx"); + new AddingDataTableAsWorksheet().Create(@"C:\Excel Files\Created\AddingDataTableAsWorksheet.xlsx"); + new TabColors().Create(@"C:\Excel Files\Created\TabColors.xlsx"); + new ShiftingFormulas().Create(@"C:\Excel Files\Created\ShiftingFormulas.xlsx"); + new CopyingRowsAndColumns().Create(@"C:\Excel Files\Created\CopyingRowsAndColumns.xlsx"); + new UsingRichText().Create(@"C:\Excel Files\Created\UsingRichText.xlsx"); + new UsingPhonetics().Create(@"C:\Excel Files\Created\UsingPhonetics.xlsx"); + new WalkingRanges().Create(@"C:\Excel Files\Created\CellMoves.xlsx"); new AddingComments().Create(@"C:\Excel Files\Created\AddingComments.xlsx"); } } diff --git a/ClosedXML/ClosedXML/ClosedXML_Sandbox/ClosedXML_Sandbox.csproj b/ClosedXML/ClosedXML/ClosedXML_Sandbox/ClosedXML_Sandbox.csproj index 83f2958..860a343 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Sandbox/ClosedXML_Sandbox.csproj +++ b/ClosedXML/ClosedXML/ClosedXML_Sandbox/ClosedXML_Sandbox.csproj @@ -129,6 +129,10 @@ {BD5E6BFE-E837-4A35-BCA9-39667D873A20} ClosedXML + + {03A518D0-1CB7-488E-861C-C4E782B27A46} + ClosedXML_Examples +