diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs index 5b1e845..b7e20fe 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs @@ -234,7 +234,7 @@ public string GetFormattedString() { - string cValue = StringExtensions.IsNullOrWhiteSpace(FormulaA1) ? _cellValue : GetString(); + string cValue = FormulaA1.Length > 0 ? String.Empty : _cellValue; if (_dataType == XLCellValues.Boolean) return (cValue != "0").ToString(); @@ -544,8 +544,8 @@ return _worksheet.Range( Address.RowNumber, Address.ColumnNumber, - Address.RowNumber + ro - 1, - Address.ColumnNumber + maxCo - 1); + ro - 1, + maxCo - 1); } return null; @@ -836,11 +836,9 @@ { if (_richText == null) { - _richText = StringExtensions.IsNullOrWhiteSpace(_cellValue) ? new XLRichText(_style.Font) : new XLRichText(GetFormattedString(), _style.Font); + _richText = _cellValue.Length == 0 ? new XLRichText(_style.Font) : new XLRichText(GetFormattedString(), _style.Font); _dataType = XLCellValues.Text; - if (!Style.Alignment.WrapText) - Style.Alignment.WrapText = true; } return _richText; @@ -1005,7 +1003,11 @@ FormulaA1 = String.Empty; string val = value.ToString(); _richText = null; - if (val.Length > 0) + if (val.Length == 0) + { + _dataType = XLCellValues.Text; + } + else { double dTest; DateTime dtTest; diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook.cs index 4627ebc..3435fe6 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook.cs @@ -19,7 +19,6 @@ Default }; - public enum XLCellSetValueBehavior { /// Analyze input string and convert value. For avoid analyzing use escape symbol ' @@ -27,7 +26,6 @@ /// Direct set value. If value has unsupported type - value will be stored as string returned by Simple = 1, } - public partial class XLWorkbook { #region Static @@ -138,7 +136,7 @@ }; } } - /// +/// /// Behavior for /// public static XLCellSetValueBehavior CellSetValueBehavior { get; set; } @@ -202,7 +200,7 @@ public XLWorkbook() { DefaultRowHeight = 15; - DefaultColumnWidth = 9.140625; + DefaultColumnWidth = 8.43; Style = new XLStyle(null, DefaultStyle); RowHeight = DefaultRowHeight; ColumnWidth = DefaultColumnWidth; diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs index c67ae8a..c41aba4 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs @@ -144,7 +144,9 @@ sheetFormatProperties.CustomHeight.Value); if (sheetFormatProperties.DefaultColumnWidth != null) + { ws.ColumnWidth = sheetFormatProperties.DefaultColumnWidth; + } } } else if (reader.ElementType == typeof (SheetViews)) diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs index e86b8df..0ed2618 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs @@ -36,7 +36,7 @@ { public partial class XLWorkbook { - private const Double ColumnWidthOffset = 0.71; + private const Double ColumnWidthOffset = 0.710625; //private Dictionary sharedStrings; //private Dictionary context.SharedStyles; @@ -360,7 +360,7 @@ foreach (Sheet sheet in workbook.Sheets.Elements()) { - Int32 sheetId = (Int32)sheet.SheetId.Value; + var sheetId = (Int32)sheet.SheetId.Value; if (WorksheetsInternal.Any(w => (w).SheetId == sheetId)) { var wks = @@ -605,7 +605,7 @@ if ( c.DataType == XLCellValues.Text && c.ShareString - && !StringExtensions.IsNullOrWhiteSpace(c.InnerText)) + && c.InnerText.Length > 0) { if (c.HasRichText) { @@ -674,7 +674,8 @@ sharedStringItem.Append(phoneticRun); } var f = new XLFont(null, c.RichText.Phonetics); - context.SharedFonts.Add(f, new FontInfo {Font = f}); + if (!context.SharedFonts.ContainsKey(f)) + context.SharedFonts.Add(f, new FontInfo {Font = f}); var phoneticProperties = new PhoneticProperties { @@ -2489,9 +2490,19 @@ worksheetPart.Worksheet.SheetFormatProperties); worksheetPart.Worksheet.SheetFormatProperties.DefaultRowHeight = xlWorksheet.RowHeight; - worksheetPart.Worksheet.SheetFormatProperties.DefaultColumnWidth = xlWorksheet.ColumnWidth; + if (xlWorksheet.RowHeightChanged) worksheetPart.Worksheet.SheetFormatProperties.CustomHeight = true; + else + worksheetPart.Worksheet.SheetFormatProperties.CustomHeight = null; + + + double worksheetColumnWidth = GetColumnWidth(xlWorksheet.ColumnWidth); + if (xlWorksheet.ColumnWidthChanged) + worksheetPart.Worksheet.SheetFormatProperties.DefaultColumnWidth = worksheetColumnWidth; + else + worksheetPart.Worksheet.SheetFormatProperties.DefaultColumnWidth = null; + if (maxOutlineColumn > 0) worksheetPart.Worksheet.SheetFormatProperties.OutlineLevelColumn = (byte)maxOutlineColumn; @@ -2512,7 +2523,7 @@ worksheetPart.Worksheet.RemoveAllChildren(); else { - double worksheetColumnWidth = GetColumnWidth(xlWorksheet.ColumnWidth); + if (!worksheetPart.Worksheet.Elements().Any()) { @@ -2539,11 +2550,12 @@ maxInColumnsCollection = 0; } + uint worksheetStyleId = context.SharedStyles[xlWorksheet.Style].StyleId; if (minInColumnsCollection > 1) { UInt32Value min = 1; UInt32Value max = (UInt32)(minInColumnsCollection - 1); - uint styleId = context.SharedStyles[xlWorksheet.Style].StyleId; + for (var co = min; co <= max; co++) { @@ -2551,7 +2563,7 @@ { Min = co, Max = co, - Style = styleId, + Style = worksheetStyleId, Width = worksheetColumnWidth, CustomWidth = true }; @@ -2589,6 +2601,7 @@ Width = columnWidth, CustomWidth = true }; + if (isHidden) column.Hidden = true; if (collapsed) @@ -2605,20 +2618,21 @@ columns.Elements().Where(c => c.Min > (UInt32)(collection)).OrderBy( c => c.Min.Value)) { - col.Style = context.SharedStyles[xlWorksheet.Style].StyleId; - col.Width = worksheetColumnWidth; - col.CustomWidth = true; + col.Style = worksheetStyleId; + col.Width = worksheetColumnWidth; + col.CustomWidth = true; + if ((Int32)col.Max.Value > maxInColumnsCollection) maxInColumnsCollection = (Int32)col.Max.Value; } - if (maxInColumnsCollection < ExcelHelper.MaxColumnNumber) + if (maxInColumnsCollection < ExcelHelper.MaxColumnNumber && !xlWorksheet.Style.Equals(DefaultStyle)) { var column = new Column { Min = (UInt32)(maxInColumnsCollection + 1), Max = (UInt32)(ExcelHelper.MaxColumnNumber), - Style = context.SharedStyles[xlWorksheet.Style].StyleId, + Style = worksheetStyleId, Width = worksheetColumnWidth, CustomWidth = true }; @@ -2626,6 +2640,12 @@ } CollapseColumns(columns, sheetColumnsByMin); + + if (!columns.Any()) + { + worksheetPart.Worksheet.RemoveAllChildren(); + cm.SetElement( XLWSContentManager.XLWSContents.Columns, null); + } } #endregion @@ -2801,7 +2821,7 @@ var cellValue = new CellValue(); if (dataType == XLCellValues.Text) { - if (StringExtensions.IsNullOrWhiteSpace(opCell.InnerText)) + if (opCell.InnerText.Length == 0 ) cell.CellValue = null; else { @@ -3388,8 +3408,10 @@ private static bool ColumnsAreEqual(Column left, Column right) { return - left.Style.Value == right.Style.Value - && left.Width.Value == right.Width.Value + ((left.Style == null && right.Style == null) + || (left.Style != null && right.Style != null && left.Style.Value == right.Style.Value)) + && ((left.Width == null && right.Width == null) + || (left.Width != null && right.Width != null && left.Width.Value == right.Width.Value)) && ((left.Hidden == null && right.Hidden == null) || (left.Hidden != null && right.Hidden != null && left.Hidden.Value == right.Hidden.Value)) && ((left.Collapsed == null && right.Collapsed == null) diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs index 9a79fe2..3464d9c 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorksheet.cs @@ -59,7 +59,7 @@ new XLRowsCollection(), new XLRanges()); PageSetup = new XLPageSetup(workbook.PageOptions, this); Outline = new XLOutline(workbook.Outline); - ColumnWidth = workbook.ColumnWidth; + _columnWidth = workbook.ColumnWidth; _rowHeight = workbook.RowHeight; RowHeightChanged = workbook.RowHeight != XLWorkbook.DefaultRowHeight; Name = sheetName; @@ -102,6 +102,7 @@ } internal Boolean RowHeightChanged { get; set; } + internal Boolean ColumnWidthChanged { get; set; } public Int32 SheetId { get; set; } public String RelId { get; set; } @@ -123,7 +124,19 @@ } } - public Double ColumnWidth { get; set; } + private Double _columnWidth; + public Double ColumnWidth + { + get + { + return _columnWidth; + } + set + { + ColumnWidthChanged = true; + _columnWidth = value; + } + } public Double RowHeight { diff --git a/ClosedXML/ClosedXML/ClosedXML_Examples/ClosedXML_Examples.csproj b/ClosedXML/ClosedXML/ClosedXML_Examples/ClosedXML_Examples.csproj index a962cac..e2aa431 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Examples/ClosedXML_Examples.csproj +++ b/ClosedXML/ClosedXML/ClosedXML_Examples/ClosedXML_Examples.csproj @@ -188,6 +188,7 @@ + diff --git a/ClosedXML/ClosedXML/ClosedXML_Examples/Styles/UsingRichText.cs b/ClosedXML/ClosedXML/ClosedXML_Examples/Styles/UsingRichText.cs new file mode 100644 index 0000000..200d1e9 --- /dev/null +++ b/ClosedXML/ClosedXML/ClosedXML_Examples/Styles/UsingRichText.cs @@ -0,0 +1,107 @@ +using System; +using ClosedXML.Excel; + +namespace ClosedXML_Examples.Styles +{ + public class UsingRichText : IXLExample + { + #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 wb = new XLWorkbook(); + var ws = wb.Worksheets.Add("Rich Text"); + + // Let's start with a plain text and then decorate it... + var cell1 = ws.Cell(1, 1).SetValue("The show must go on..."); + + // We want everything in blue except the word show + // (which we want in red and with Broadway Font) + cell1.Style.Font.FontColor = XLColor.Blue; // Set the color for the entire cell + cell1.RichText.Substring(4, 4) + .SetFontColor(XLColor.Red) + .SetFontName("Broadway"); // Set the color and font for the word "show" + + // On the next example we'll start with an empty cell and add the rich text + var cell = ws.Cell(3, 1); + + // Add the text parts + cell.RichText.AddText("Hello").SetFontColor(XLColor.Red); + cell.RichText.AddText(" BIG ").SetFontColor(XLColor.Blue).SetBold(); + cell.RichText.AddText("World").SetFontColor(XLColor.Red); + + // Here we're showing that even though we added three pieces of text + // you can treat then like a single one. + cell.RichText.Substring(4, 7).SetUnderline(); + + // Right now cell.RichText has the following 5 strings: + // + // "Hell" -> Red + // "o" -> Red, Underlined + // " BIG " -> Blue, Underlined, Bold + // "W" -> Red, Underlined + // "orld" -> Red + + // Of course you can loop through each piece of text and check its properties + foreach (var richText in cell.RichText) + { + if(richText.Bold) + ws.Cell(3, 2).Value = String.Format("\"{0}\" is Bold.", richText.Text); + } + + ws.Columns().AdjustToContents(); + + wb.SaveAs(filePath); + } + + // Private + + // Override + + + #endregion + } +} diff --git a/ClosedXML/ClosedXML/ClosedXML_Tests/ClosedXML_Tests.csproj b/ClosedXML/ClosedXML/ClosedXML_Tests/ClosedXML_Tests.csproj index 6f6c743..fc132ed 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Tests/ClosedXML_Tests.csproj +++ b/ClosedXML/ClosedXML/ClosedXML_Tests/ClosedXML_Tests.csproj @@ -166,6 +166,9 @@ + + +