From c563624795e6186ab46e8e45ddecf3e7726bfad2 Mon Sep 17 00:00:00 2001 From: xl-wenqiang Date: Tue, 1 Nov 2022 23:05:48 +0800 Subject: [PATCH] Revert "use skiasharp font instead of SixLabor fonts" --- src/EPPlus/EPPlus/ExcelRangeBase.cs | 683 +++++------------- src/EPPlus/EPPlus/ExcelWorkbook.cs | 6 +- src/EPPlus/EPPlus/Magicodes.IE.EPPlus.csproj | 2 +- src/EPPlus/EPPlus/Style/ExcelFont.cs | 17 +- src/EPPlus/EPPlus/Style/ExcelTextFont.cs | 95 ++- .../EPPlus/Style/XmlAccess/ExcelFontXml.cs | 15 +- src/EPPlus/EPPlusTest/DrawingTest.cs | 19 +- src/EPPlus/EPPlusTest/WorkSheetTests.cs | 18 +- .../ExcelExporter.cs | 21 +- .../Images/ImageExtensions.cs | 9 +- .../Magicodes.IE.Excel.csproj | 1 - 11 files changed, 295 insertions(+), 591 deletions(-) diff --git a/src/EPPlus/EPPlus/ExcelRangeBase.cs b/src/EPPlus/EPPlus/ExcelRangeBase.cs index d138e197..11a6dfb0 100644 --- a/src/EPPlus/EPPlus/ExcelRangeBase.cs +++ b/src/EPPlus/EPPlus/ExcelRangeBase.cs @@ -30,7 +30,6 @@ * Jan Källman License changed GPL-->LGPL 2011-12-27 * Eyal Seagull Conditional Formatting 2012-04-03 *******************************************************************************/ - using OfficeOpenXml.Compatibility; using OfficeOpenXml.ConditionalFormatting; using OfficeOpenXml.DataValidation; @@ -39,12 +38,12 @@ using OfficeOpenXml.Style.XmlAccess; using OfficeOpenXml.Table; using OfficeOpenXml.Utils; -using SkiaSharp; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; +using SixLabors.ImageSharp; using System.Globalization; using System.IO; using System.Linq; @@ -52,29 +51,24 @@ using System.Text; using System.Text.RegularExpressions; using System.Xml; +using SixLabors.Fonts; namespace OfficeOpenXml { /// /// A range of cells /// - public partial class ExcelRangeBase : ExcelAddress, IExcelCell, IDisposable, IEnumerable, - IEnumerator + public partial class ExcelRangeBase : ExcelAddress, IExcelCell, IDisposable, IEnumerable, IEnumerator { /// /// Reference to the worksheet /// protected ExcelWorksheet _worksheet; - internal ExcelWorkbook _workbook = null; - private delegate void _changeProp(ExcelRangeBase range, _setValue method, object value); - private delegate void _setValue(ExcelRangeBase range, object value, int row, int col); - private _changeProp _changePropMethod; private int _styleID; - private class CopiedCell { internal int Row { get; set; } @@ -87,9 +81,7 @@ private class CopiedCell internal ExcelComment Comment { get; set; } internal Byte Flag { get; set; } } - #region Constructors - internal ExcelRangeBase(ExcelWorksheet xlWorksheet) { _worksheet = xlWorksheet; @@ -97,7 +89,6 @@ internal ExcelRangeBase(ExcelWorksheet xlWorksheet) _workbook = _worksheet.Workbook; SetDelegate(); } - /// /// On change address handler /// @@ -107,10 +98,8 @@ protected internal override void ChangeAddress() { SetRCFromTable(_workbook._package, null); } - SetDelegate(); } - internal ExcelRangeBase(ExcelWorksheet xlWorksheet, string address) : base(xlWorksheet == null ? "" : xlWorksheet.Name, address) { @@ -120,7 +109,6 @@ internal ExcelRangeBase(ExcelWorksheet xlWorksheet, string address) : if (string.IsNullOrEmpty(_ws)) _ws = _worksheet == null ? "" : _worksheet.Name; SetDelegate(); } - internal ExcelRangeBase(ExcelWorkbook wb, ExcelWorksheet xlWorksheet, string address, bool isName) : base(xlWorksheet == null ? "" : xlWorksheet.Name, address, isName) { @@ -130,16 +118,12 @@ internal ExcelRangeBase(ExcelWorkbook wb, ExcelWorksheet xlWorksheet, string add if (string.IsNullOrEmpty(_ws)) _ws = (xlWorksheet == null ? null : xlWorksheet.Name); SetDelegate(); } - #endregion - - #region Set Value Delegates - + #region Set Value Delegates private static _changeProp _setUnknownProp = SetUnknown; private static _changeProp _setSingleProp = SetSingle; private static _changeProp _setRangeProp = SetRange; private static _changeProp _setMultiProp = SetMultiRange; - private void SetDelegate() { if (_fromRow == -1) @@ -162,7 +146,6 @@ private void SetDelegate() _changePropMethod = SetMultiRange; } } - /// /// We dont know the address yet. Set the delegate first time a property is set. /// @@ -176,11 +159,9 @@ private static void SetUnknown(ExcelRangeBase range, _setValue valueMethod, obje { range.SetToSelectedRange(); } - range.SetDelegate(); range._changePropMethod(range, valueMethod, value); } - /// /// Set a single cell /// @@ -191,7 +172,6 @@ private static void SetSingle(ExcelRangeBase range, _setValue valueMethod, objec { valueMethod(range, value, range._fromRow, range._fromCol); } - /// /// Set a range /// @@ -202,7 +182,6 @@ private static void SetRange(ExcelRangeBase range, _setValue valueMethod, object { range.SetValueAddress(range, valueMethod, value); } - /// /// Set a multirange (A1:A2,C1:C2) /// @@ -217,7 +196,6 @@ private static void SetMultiRange(ExcelRangeBase range, _setValue valueMethod, o range.SetValueAddress(address, valueMethod, value); } } - /// /// Set the property for an address /// @@ -227,9 +205,7 @@ private static void SetMultiRange(ExcelRangeBase range, _setValue valueMethod, o private void SetValueAddress(ExcelAddress address, _setValue valueMethod, object value) { IsRangeValid(""); - if (_fromRow == 1 && _fromCol == 1 && _toRow == ExcelPackage.MaxRows && - _toCol == ExcelPackage - .MaxColumns) //Full sheet (ex ws.Cells.Value=0). Set value for A1 only to avoid hanging + if (_fromRow == 1 && _fromCol == 1 && _toRow == ExcelPackage.MaxRows && _toCol == ExcelPackage.MaxColumns) //Full sheet (ex ws.Cells.Value=0). Set value for A1 only to avoid hanging { throw (new ArgumentException("Can't reference all cells. Please use the indexer to set the range")); } @@ -238,8 +214,7 @@ private void SetValueAddress(ExcelAddress address, _setValue valueMethod, object if (value is object[,] && valueMethod == Set_Value) { // only simple set value is supported for bulk copy - _worksheet.SetRangeValueInner(address.Start.Row, address.Start.Column, address.End.Row, - address.End.Column, (object[,])value); + _worksheet.SetRangeValueInner(address.Start.Row, address.Start.Column, address.End.Row, address.End.Column, (object[,])value); } else { @@ -253,11 +228,8 @@ private void SetValueAddress(ExcelAddress address, _setValue valueMethod, object } } } - #endregion - #region Set property methods - private static _setValue _setStyleIdDelegate = Set_StyleID; private static _setValue _setValueDelegate = Set_Value; private static _setValue _setHyperLinkDelegate = Set_HyperLink; @@ -269,12 +241,10 @@ private static void Set_StyleID(ExcelRangeBase range, object value, int row, int { range._worksheet.SetStyleInner(row, col, (int)value); } - private static void Set_StyleName(ExcelRangeBase range, object value, int row, int col) { range._worksheet.SetStyleInner(row, col, range._styleID); } - private static void Set_Value(ExcelRangeBase range, object value, int row, int col) { var sfi = range._worksheet._formulas.GetValue(row, col); @@ -282,11 +252,9 @@ private static void Set_Value(ExcelRangeBase range, object value, int row, int c { range.SplitFormulas(range._worksheet.Cells[row, col]); } - if (sfi != null) range._worksheet._formulas.SetValue(row, col, string.Empty); range._worksheet.SetValueInner(row, col, value); } - private static void Set_Formula(ExcelRangeBase range, object value, int row, int col) { var f = range._worksheet._formulas.GetValue(row, col); @@ -299,13 +267,11 @@ private static void Set_Formula(ExcelRangeBase range, object value, int row, int } else { - if (formula[0] == '=') - value = formula.Substring(1, formula.Length - 1); // remove any starting equalsign. + if (formula[0] == '=') value = formula.Substring(1, formula.Length - 1); // remove any starting equalsign. range._worksheet._formulas.SetValue(row, col, formula); range._worksheet.SetValueInner(row, col, null); } } - /// /// Handles shared formulas /// @@ -315,20 +281,16 @@ private static void Set_Formula(ExcelRangeBase range, object value, int row, int /// If the forumla is an array formula. private static void Set_SharedFormula(ExcelRangeBase range, string value, ExcelAddress address, bool IsArray) { - if (range._fromRow == 1 && range._fromCol == 1 && range._toRow == ExcelPackage.MaxRows && - range._toCol == - ExcelPackage.MaxColumns) //Full sheet (ex ws.Cells.Value=0). Set value for A1 only to avoid hanging + if (range._fromRow == 1 && range._fromCol == 1 && range._toRow == ExcelPackage.MaxRows && range._toCol == ExcelPackage.MaxColumns) //Full sheet (ex ws.Cells.Value=0). Set value for A1 only to avoid hanging { throw (new InvalidOperationException("Can't set a formula for the entire worksheet")); } - else if (address.Start.Row == address.End.Row && address.Start.Column == address.End.Column && - !IsArray) //is it really a shared formula? Arrayformulas can be one cell only + else if (address.Start.Row == address.End.Row && address.Start.Column == address.End.Column && !IsArray) //is it really a shared formula? Arrayformulas can be one cell only { //Nope, single cell. Set the formula Set_Formula(range, value, address.Start.Row, address.Start.Column); return; } - range.CheckAndSplitSharedFormula(address); ExcelWorksheet.Formulas f = new ExcelWorksheet.Formulas(SourceCodeTokenizer.Default); f.Formula = value; @@ -350,7 +312,6 @@ private static void Set_SharedFormula(ExcelRangeBase range, string value, ExcelA } } } - private static void Set_HyperLink(ExcelRangeBase range, object value, int row, int col) { if (value is Uri) @@ -376,30 +337,24 @@ private static void Set_HyperLink(ExcelRangeBase range, object value, int row, i range._worksheet.SetValueInner(row, col, (Uri)null); } } - private static void Set_IsRichText(ExcelRangeBase range, object value, int row, int col) { range._worksheet._flags.SetFlagValue(row, col, (bool)value, CellFlags.RichText); } - private static void Exists_Comment(ExcelRangeBase range, object value, int row, int col) { if (range._worksheet._commentsStore.Exists(row, col)) { - throw (new InvalidOperationException(string.Format("Cell {0} already contain a comment.", - new ExcelCellAddress(row, col).Address))); + throw (new InvalidOperationException(string.Format("Cell {0} already contain a comment.", new ExcelCellAddress(row, col).Address))); } - } + } private static void Set_Comment(ExcelRangeBase range, object value, int row, int col) { string[] v = (string[])value; - range._worksheet.Comments.Add( - new ExcelRangeBase(range._worksheet, GetAddress(range._fromRow, range._fromCol)), v[0], v[1]); + range._worksheet.Comments.Add(new ExcelRangeBase(range._worksheet, GetAddress(range._fromRow, range._fromCol)), v[0], v[1]); } - #endregion - private void SetToSelectedRange() { if (_worksheet.View.SelectedRange == "") @@ -411,7 +366,6 @@ private void SetToSelectedRange() Address = _worksheet.View.SelectedRange; } } - private void IsRangeValid(string type) { if (_fromRow <= 0) @@ -424,25 +378,21 @@ private void IsRangeValid(string type) { if (type == "") { - throw (new InvalidOperationException(string.Format("Range is not valid for this operation: {0}", - _address))); + throw (new InvalidOperationException(string.Format("Range is not valid for this operation: {0}", _address))); } else { - throw (new InvalidOperationException(string.Format("Range is not valid for {0} : {1}", type, - _address))); + throw (new InvalidOperationException(string.Format("Range is not valid for {0} : {1}", type, _address))); } } } } - internal void UpdateAddress(string address) { throw new NotImplementedException(); } #region Public Properties - /// /// The styleobject for the range. /// @@ -467,11 +417,9 @@ public ExcelStyle Style } } } - return _worksheet.Workbook.Styles.GetStyleObject(s, _worksheet.PositionID, Address); } } - /// /// The named style /// @@ -504,7 +452,6 @@ public string StyleName } } } - int nsID; if (xfId <= 0) { @@ -514,7 +461,6 @@ public string StyleName { nsID = Style.Styles.CellXfs[xfId].XfId; } - foreach (var ns in Style.Styles.NamedStyles) { if (ns.StyleXfId == nsID) @@ -529,7 +475,7 @@ public string StyleName { _styleID = _worksheet.Workbook.Styles.GetStyleIdFromName(value); int col = _fromCol; - if (_fromRow == 1 && _toRow == ExcelPackage.MaxRows) //Full column + if (_fromRow == 1 && _toRow == ExcelPackage.MaxRows) //Full column { ExcelColumn column; var c = _worksheet.GetValue(0, _fromCol); @@ -571,13 +517,11 @@ public string StyleName { column.ColumnMax = nextCol.ColumnMax - 1; } - column = nextCol; cols.Next(); } } } - if (column.ColumnMax < _toCol) { column.ColumnMax = _toCol; @@ -585,8 +529,7 @@ public string StyleName if (_fromCol == 1 && _toCol == ExcelPackage.MaxColumns) //FullRow { - var rows = new CellsStoreEnumerator(_worksheet._values, 1, 0, - ExcelPackage.MaxRows, 0); + var rows = new CellsStoreEnumerator(_worksheet._values, 1, 0, ExcelPackage.MaxRows, 0); rows.Next(); while (rows.Value._value != null) { @@ -607,8 +550,7 @@ public string StyleName } } - if (!((_fromRow == 1 && _toRow == ExcelPackage.MaxRows) || - (_fromCol == 1 && _toCol == ExcelPackage.MaxColumns))) //Cell specific + if (!((_fromRow == 1 && _toRow == ExcelPackage.MaxRows) || (_fromCol == 1 && _toCol == ExcelPackage.MaxColumns))) //Cell specific { for (int c = _fromCol; c <= _toCol; c++) { @@ -620,8 +562,7 @@ public string StyleName } else //Only set name on created cells. (uncreated cells is set on full row or full column). { - var cells = new CellsStoreEnumerator(_worksheet._values, _fromRow, _fromCol, _toRow, - _toCol); + var cells = new CellsStoreEnumerator(_worksheet._values, _fromRow, _fromCol, _toRow, _toCol); while (cells.Next()) { _worksheet.SetStyleInner(cells.Row, cells.Column, _styleID); @@ -649,10 +590,8 @@ private int GetColumnStyle(int col) } } } - return 0; } - /// /// The style ID. /// It is not recomended to use this one. Use Named styles as an alternative. @@ -670,12 +609,13 @@ public int StyleID s = _worksheet.GetStyleInner(0, _fromCol); } } - return s; } - set { _changePropMethod(this, _setStyleIdDelegate, value); } + set + { + _changePropMethod(this, _setStyleIdDelegate, value); + } } - /// /// Set the range to a specific value /// @@ -751,7 +691,6 @@ private object GetValueArray() { addr = this; } - object[,] v = new object[addr._toRow - addr._fromRow + 1, addr._toCol - addr._fromCol + 1]; for (int col = addr._fromCol; col <= addr._toCol; col++) @@ -772,10 +711,8 @@ private object GetValueArray() } } } - return v; } - private ExcelAddressBase GetAddressDim(ExcelRangeBase addr) { int fromRow, fromCol, toRow, toCol; @@ -814,15 +751,16 @@ private object GetSingleValue() return _worksheet.GetValueInner(_fromRow, _fromCol); } } - /// /// Returns the formatted value. /// public string Text { - get { return GetFormattedText(false); } + get + { + return GetFormattedText(false); + } } - /// /// Set the column width from the content of the range. The minimum width is the value of the ExcelWorksheet.defaultColumnWidth property. /// Note: Cells containing formulas must be calculated before autofit is called. @@ -851,19 +789,19 @@ public void AutoFitColumns(double MinimumWidth) /// Wrapped and merged cells are also ignored. /// Hidden columns are left hidden. /// - /// Minimum column width - /// Maximum column width - public void AutoFitColumns(double minimumWidth, double maximumWidth) + /// Minimum column width + /// Maximum column width + public void AutoFitColumns(double MinimumWidth, double MaximumWidth) { if (_worksheet.Dimension == null) { return; } - if (_fromCol < 1 || _fromRow < 1) { SetToSelectedRange(); } + var fontCache = new Dictionary(); bool doAdjust = _worksheet._package.DoAdjustDrawings; _worksheet._package.DoAdjustDrawings = false; @@ -872,24 +810,19 @@ public void AutoFitColumns(double minimumWidth, double maximumWidth) var fromCol = _fromCol > _worksheet.Dimension._fromCol ? _fromCol : _worksheet.Dimension._fromCol; var toCol = _toCol < _worksheet.Dimension._toCol ? _toCol : _worksheet.Dimension._toCol; - if (fromCol > toCol) - { - return; //Issue 15383 - } + if (fromCol > toCol) return; //Issue 15383 if (Addresses == null) { - SetMinWidth(minimumWidth, fromCol, toCol); + SetMinWidth(MinimumWidth, fromCol, toCol); } else { foreach (var addr in Addresses) { - fromCol = addr._fromCol > _worksheet.Dimension._fromCol - ? addr._fromCol - : _worksheet.Dimension._fromCol; + fromCol = addr._fromCol > _worksheet.Dimension._fromCol ? addr._fromCol : _worksheet.Dimension._fromCol; toCol = addr._toCol < _worksheet.Dimension._toCol ? addr._toCol : _worksheet.Dimension._toCol; - SetMinWidth(minimumWidth, fromCol, toCol); + SetMinWidth(MinimumWidth, fromCol, toCol); } } @@ -898,120 +831,92 @@ public void AutoFitColumns(double minimumWidth, double maximumWidth) if (_worksheet.AutoFilterAddress != null) { afAddr.Add(new ExcelAddressBase(_worksheet.AutoFilterAddress._fromRow, - _worksheet.AutoFilterAddress._fromCol, - _worksheet.AutoFilterAddress._fromRow, - _worksheet.AutoFilterAddress._toCol)); + _worksheet.AutoFilterAddress._fromCol, + _worksheet.AutoFilterAddress._fromRow, + _worksheet.AutoFilterAddress._toCol)); afAddr[afAddr.Count - 1]._ws = WorkSheet; } - foreach (var tbl in _worksheet.Tables) { if (tbl.AutoFilterAddress != null) { afAddr.Add(new ExcelAddressBase(tbl.AutoFilterAddress._fromRow, - tbl.AutoFilterAddress._fromCol, - tbl.AutoFilterAddress._fromRow, - tbl.AutoFilterAddress._toCol)); + tbl.AutoFilterAddress._fromCol, + tbl.AutoFilterAddress._fromRow, + tbl.AutoFilterAddress._toCol)); afAddr[afAddr.Count - 1]._ws = WorkSheet; } } var styles = _worksheet.Workbook.Styles; + var nf = styles.Fonts[styles.CellXfs[0].FontId]; + var fs = FontStyle.Regular; + if (nf.Bold) fs |= FontStyle.Bold; + if (nf.Italic) fs |= FontStyle.Italic; + var td = TextDecorations.None; + if (nf.UnderLine) td |= TextDecorations.Underline; + if (nf.Strike) td |= TextDecorations.Strikeout; + + var normalSize = Convert.ToSingle(ExcelWorkbook.GetWidthPixels(nf.Name, nf.Size)); + foreach (var cell in this) { - if (_worksheet.Column(cell.Start.Column).Hidden) //Issue 15338 - { + if (_worksheet.Column(cell.Start.Column).Hidden) //Issue 15338 continue; - } - if (cell.Merge || cell.Style.WrapText) + if (cell.Merge == true || cell.Style.WrapText) continue; + var fntId = styles.CellXfs[cell.StyleID].FontId; + Font f; + if (fontCache.ContainsKey(fntId)) { - continue; + f = fontCache[fntId]; } - - if (cell.Address.StartsWith("I")) + else { + var fnt = styles.Fonts[fntId]; + fs = FontStyle.Regular; + if (fnt.Bold) fs |= FontStyle.Bold; + if (fnt.Italic) fs |= FontStyle.Italic; + td = TextDecorations.None; + if (fnt.UnderLine) td |= TextDecorations.Underline; + if (fnt.Strike) td |= TextDecorations.Strikeout; + f = SystemFonts.CreateFont(fnt.Name, CultureInfo.CurrentCulture, fnt.Size, fs); + fontCache.Add(fntId, f); } - var ind = styles.CellXfs[cell.StyleID].Indent; var textForWidth = cell.TextForWidth; var t = textForWidth + (ind > 0 && !string.IsNullOrEmpty(textForWidth) ? new string('_', ind) : ""); if (t.Length > 32000) t = t.Substring(0, 32000); //Issue - using (var font = GetAvailableFont(t)) - { - var normalSize = - Convert.ToSingle(ExcelWorkbook.GetWidthPixels(font.Typeface.FamilyName, font.Size)); - using (var paint = new SKPaint(font)) - { - paint.TextSize = font.Size; - SKRect rect = SKRect.Empty; - var measureWidthInPoints = paint.MeasureText(t, ref rect); - double widthInPixels = measureWidthInPoints * 4 / 3; - double r = styles.CellXfs[cell.StyleID].TextRotation; - if (r <= 0) - { - widthInPixels = (widthInPixels + 5) / normalSize; - } - else - { - r = r <= 90 ? r : r - 90; - widthInPixels = ((widthInPixels - rect.Size.Height) * - Math.Abs(Math.Cos(Math.PI * r / 180.0)) + - rect.Size.Height + 5) / normalSize; - } + var size = TextMeasurer.Measure(t, new TextOptions(f)); - foreach (var a in afAddr) - { - if (a.Collide(cell) != eAddressCollition.No) - { - widthInPixels += 2.25; - break; - } - } - - if (widthInPixels > _worksheet.Column(cell._fromCol).Width) - { - _worksheet.Column(cell._fromCol).Width = - widthInPixels > maximumWidth ? maximumWidth : widthInPixels; - } - } - } - } - - _worksheet.Drawings.AdjustWidth(drawWidths); - _worksheet._package.DoAdjustDrawings = doAdjust; - } - - private SKFont GetAvailableFont(string text) - { - var isAllAsciiSymbolOrChars = - !text.Any(c => c < 20 || c > 126); // use FangSong to measure the text that is not in english - var styles = _worksheet.Workbook.Styles; - for (int i = isAllAsciiSymbolOrChars ? 0 : 1; i < styles.Fonts.Count; i++) - { - var skFontStyle = SKFontStyle.Normal; - var excelFontXml = styles.Fonts[i]; - if (excelFontXml.Bold && excelFontXml.Italic) + double width; + double r = styles.CellXfs[cell.StyleID].TextRotation; + if (r <= 0) { - skFontStyle = SKFontStyle.BoldItalic; + width = (size.Width + 5) / normalSize; } - else if (excelFontXml.Italic) + else { - skFontStyle = SKFontStyle.Italic; + r = (r <= 90 ? r : r - 90); + width = (((size.Width - size.Height) * Math.Abs(System.Math.Cos(System.Math.PI * r / 180.0)) + size.Height) + 5) / normalSize; } - else if (excelFontXml.Bold) + + foreach (var a in afAddr) { - skFontStyle = SKFontStyle.Bold; + if (a.Collide(cell) != eAddressCollition.No) + { + width += 2.25; + break; + } } - var fontTypeface = SKTypeface.FromFamilyName(excelFontXml.Name, skFontStyle); - if (fontTypeface.FamilyName == excelFontXml.Name) + if (width > _worksheet.Column(cell._fromCol).Width) { - return new SKFont(fontTypeface, excelFontXml.Size); + _worksheet.Column(cell._fromCol).Width = width > MaximumWidth ? MaximumWidth : width; } } - - return new SKFont(SKTypeface.Default); + _worksheet.Drawings.AdjustWidth(drawWidths); + _worksheet._package.DoAdjustDrawings = doAdjust; } private void SetMinWidth(double minimumWidth, int fromCol, int toCol) @@ -1028,10 +933,8 @@ private void SetMinWidth(double minimumWidth, int fromCol, int toCol) newCol.ColumnMax = col.ColumnMin - 1; newCol.Width = minimumWidth; } - prevCol = col.ColumnMax + 1; } - if (_worksheet.DefaultColWidth > minimumWidth && prevCol < toCol) { var newCol = _worksheet.Column(prevCol); @@ -1042,9 +945,11 @@ private void SetMinWidth(double minimumWidth, int fromCol, int toCol) internal string TextForWidth { - get { return GetFormattedText(true); } + get + { + return GetFormattedText(true); + } } - private string GetFormattedText(bool forWidthCalc) { object v = Value; @@ -1060,11 +965,9 @@ private string GetFormattedText(bool forWidthCalc) break; } } - if (nf == null) { - nf = styles.NumberFormats[0] - .FormatTranslator; //nf should never be null. If so set to General, Issue 173 + nf = styles.NumberFormats[0].FormatTranslator; //nf should never be null. If so set to General, Issue 173 } string format, textFormat; @@ -1082,8 +985,7 @@ private string GetFormattedText(bool forWidthCalc) return FormatValue(v, nf, format, textFormat); } - internal static string FormatValue(object v, ExcelNumberFormatXml.ExcelFormatTranslator nf, string format, - string textFormat) + internal static string FormatValue(object v, ExcelNumberFormatXml.ExcelFormatTranslator nf, string format, string textFormat) { if (v is decimal || TypeCompat.IsPrimitive(v)) { @@ -1163,7 +1065,6 @@ internal static string FormatValue(object v, ExcelNumberFormatXml.ExcelFormatTra return string.Format(textFormat, v); } } - return v.ToString(); } @@ -1173,17 +1074,14 @@ private static string GetDateText(DateTime d, string format, ExcelNumberFormatXm { return d.ToLongDateString(); } - else if (nf.SpecialDateFormat == - ExcelNumberFormatXml.ExcelFormatTranslator.eSystemDateFormat.SystemLongTime) + else if (nf.SpecialDateFormat == ExcelNumberFormatXml.ExcelFormatTranslator.eSystemDateFormat.SystemLongTime) { return d.ToLongTimeString(); } - else if (nf.SpecialDateFormat == - ExcelNumberFormatXml.ExcelFormatTranslator.eSystemDateFormat.SystemShortDate) + else if (nf.SpecialDateFormat == ExcelNumberFormatXml.ExcelFormatTranslator.eSystemDateFormat.SystemShortDate) { return d.ToShortDateString(); } - if (format == "d" || format == "D") { return d.Day.ToString(); @@ -1208,6 +1106,7 @@ private static string GetDateText(DateTime d, string format, ExcelNumberFormatXm { return d.ToString(format, nf.Culture); } + } /// @@ -1271,7 +1170,6 @@ public string Formula } } } - /// /// Gets or Set a formula in R1C1 format. /// @@ -1285,8 +1183,7 @@ public string FormulaR1C1 set { IsRangeValid("FormulaR1C1"); - if (value.Length > 0 && value[0] == '=') - value = value.Substring(1, value.Length - 1); // remove any starting equalsign. + if (value.Length > 0 && value[0] == '=') value = value.Substring(1, value.Length - 1); // remove any starting equalsign. if (value == null || value.Trim() == "") { @@ -1299,18 +1196,14 @@ public string FormulaR1C1 } else { - Set_SharedFormula(this, ExcelCellBase.TranslateFromR1C1(value, _fromRow, _fromCol), - new ExcelAddress(WorkSheet, FirstAddress), false); + Set_SharedFormula(this, ExcelCellBase.TranslateFromR1C1(value, _fromRow, _fromCol), new ExcelAddress(WorkSheet, FirstAddress), false); foreach (var address in Addresses) { - Set_SharedFormula(this, - ExcelCellBase.TranslateFromR1C1(value, address.Start.Row, address.Start.Column), address, - false); + Set_SharedFormula(this, ExcelCellBase.TranslateFromR1C1(value, address.Start.Row, address.Start.Column), address, false); } } } } - /// /// Set the hyperlink property for a range of cells /// @@ -1321,9 +1214,11 @@ public Uri Hyperlink IsRangeValid("formulaR1C1"); return _worksheet._hyperLinks.GetValue(_fromRow, _fromCol); } - set { _changePropMethod(this, _setHyperLinkDelegate, value); } + set + { + _changePropMethod(this, _setHyperLinkDelegate, value); + } } - /// /// If the cells in the range are merged. /// @@ -1342,7 +1237,6 @@ public bool Merge } } } - return true; } set @@ -1367,14 +1261,13 @@ public bool Merge { foreach (var address in Addresses) { - _worksheet.MergedCells.Clear(address); - ; + _worksheet.MergedCells.Clear(address); ; } } + } } } - /// /// Set an autofilter for the range /// @@ -1386,16 +1279,15 @@ public bool AutoFilter ExcelAddressBase address = _worksheet.AutoFilterAddress; if (address == null) return false; if (_fromRow >= address.Start.Row - && - _toRow <= address.End.Row - && - _fromCol >= address.Start.Column - && - _toCol <= address.End.Column) + && + _toRow <= address.End.Row + && + _fromCol >= address.Start.Column + && + _toCol <= address.End.Column) { return true; } - return false; } set @@ -1406,16 +1298,13 @@ public bool AutoFilter var c = this.Collide(_worksheet.AutoFilterAddress); if (value == false && (c == eAddressCollition.Partly || c == eAddressCollition.No)) { - throw (new InvalidOperationException( - "Can't remote Autofilter. Current autofilter does not match selected range.")); + throw (new InvalidOperationException("Can't remote Autofilter. Current autofilter does not match selected range.")); } } - if (_worksheet.Names.ContainsKey("_xlnm._FilterDatabase")) { _worksheet.Names.Remove("_xlnm._FilterDatabase"); } - if (value) { _worksheet.AutoFilterAddress = this; @@ -1428,7 +1317,6 @@ public bool AutoFilter } } } - /// /// If the value is in richtext format. /// @@ -1439,9 +1327,11 @@ public bool IsRichText IsRangeValid("richtext"); return _worksheet._flags.GetFlagValue(_fromRow, _fromCol, CellFlags.RichText); } - set { _changePropMethod(this, _setIsRichTextDelegate, value); } + set + { + _changePropMethod(this, _setIsRichTextDelegate, value); + } } - /// /// Is the range a part of an Arrayformula /// @@ -1453,9 +1343,7 @@ public bool IsArrayFormula return _worksheet._flags.GetFlagValue(_fromRow, _fromCol, CellFlags.ArrayFormula); } } - protected ExcelRichTextCollection _rtc = null; - /// /// Cell value is richtext formatted. /// Richtext-property only apply to the left-top cell of the range. @@ -1469,7 +1357,6 @@ public ExcelRichTextCollection RichText { _rtc = GetRichText(_fromRow, _fromCol); } - return _rtc; } } @@ -1483,27 +1370,20 @@ private ExcelRichTextCollection GetRichText(int row, int col) { if (isRt) { - XmlHelper.LoadXmlSafe(xml, - "" + v.ToString() + - "", Encoding.UTF8); + XmlHelper.LoadXmlSafe(xml, "" + v.ToString() + "", Encoding.UTF8); } else { - xml.LoadXml( - "" + - OfficeOpenXml.Utils.ConvertUtil.ExcelEscapeString(v.ToString()) + ""); + xml.LoadXml("" + OfficeOpenXml.Utils.ConvertUtil.ExcelEscapeString(v.ToString()) + ""); } } else { xml.LoadXml(""); } - - var rtc = new ExcelRichTextCollection(_worksheet.NameSpaceManager, - xml.SelectSingleNode("d:si", _worksheet.NameSpaceManager), this); + var rtc = new ExcelRichTextCollection(_worksheet.NameSpaceManager, xml.SelectSingleNode("d:si", _worksheet.NameSpaceManager), this); return rtc; } - /// /// returns the comment object of the first cell in the range /// @@ -1520,19 +1400,19 @@ public ExcelComment Comment return _worksheet._comments[i] as ExcelComment; } } - return null; } } - /// /// WorkSheet object /// public ExcelWorksheet Worksheet { - get { return _worksheet; } + get + { + return _worksheet; + } } - /// /// Address including sheetname /// @@ -1545,15 +1425,12 @@ public ExcelWorksheet Worksheet { foreach (var a in Addresses) { - fullAddress += "," + GetFullAddress(_worksheet.Name, a.Address); - ; + fullAddress += "," + GetFullAddress(_worksheet.Name, a.Address); ; } } - return fullAddress; } } - /// /// Address including sheetname /// @@ -1561,9 +1438,7 @@ public string FullAddressAbsolute { get { - string wbwsRef = string.IsNullOrEmpty(base._wb) - ? base._ws - : "[" + base._wb.Replace("'", "''") + "]" + _ws; + string wbwsRef = string.IsNullOrEmpty(base._wb) ? base._ws : "[" + base._wb.Replace("'", "''") + "]" + _ws; string fullAddress; if (Addresses == null) { @@ -1581,16 +1456,13 @@ public string FullAddressAbsolute } else { - fullAddress += GetFullAddress(wbwsRef, - GetAddress(a.Start.Row, a.Start.Column, a.End.Row, a.End.Column, true)); + fullAddress += GetFullAddress(wbwsRef, GetAddress(a.Start.Row, a.Start.Column, a.End.Row, a.End.Column, true)); } } } - return fullAddress; } } - /// /// Address including sheetname /// @@ -1598,9 +1470,7 @@ internal string FullAddressAbsoluteNoFullRowCol { get { - string wbwsRef = string.IsNullOrEmpty(base._wb) - ? base._ws - : "[" + base._wb.Replace("'", "''") + "]" + _ws; + string wbwsRef = string.IsNullOrEmpty(base._wb) ? base._ws : "[" + base._wb.Replace("'", "''") + "]" + _ws; string fullAddress; if (Addresses == null) { @@ -1612,29 +1482,21 @@ internal string FullAddressAbsoluteNoFullRowCol foreach (var a in Addresses) { if (fullAddress != "") fullAddress += ","; - fullAddress += GetFullAddress(wbwsRef, - GetAddress(a.Start.Row, a.Start.Column, a.End.Row, a.End.Column, true), false); - ; + fullAddress += GetFullAddress(wbwsRef, GetAddress(a.Start.Row, a.Start.Column, a.End.Row, a.End.Column, true), false); ; } } - return fullAddress; } } - #endregion - #region Private Methods - /// /// Set the value without altering the richtext property /// /// the value internal void SetValueRichText(object value) { - if (_fromRow == 1 && _fromCol == 1 && _toRow == ExcelPackage.MaxRows && - _toCol == ExcelPackage - .MaxColumns) //Full sheet (ex ws.Cells.Value=0). Set value for A1 only to avoid hanging + if (_fromRow == 1 && _fromCol == 1 && _toRow == ExcelPackage.MaxRows && _toCol == ExcelPackage.MaxColumns) //Full sheet (ex ws.Cells.Value=0). Set value for A1 only to avoid hanging { SetValue(value, 1, 1); } @@ -1650,7 +1512,6 @@ private void SetValue(object value, int row, int col) // if (value is string) _worksheet._types.SetValue(row, col, "S"); else _worksheet._types.SetValue(row, col, ""); _worksheet._formulas.SetValue(row, col, ""); } - internal void SetSharedFormulaID(int id) { for (int col = _fromCol; col <= _toCol; col++) @@ -1661,7 +1522,6 @@ internal void SetSharedFormulaID(int id) } } } - private void CheckAndSplitSharedFormula(ExcelAddressBase address) { for (int col = address._fromCol; col <= address._toCol; col++) @@ -1692,13 +1552,10 @@ private void SplitFormulas(ExcelAddressBase address) if (id >= 0 && !formulas.Contains(id)) { if (_worksheet._sharedFormulas[id].IsArray && - Collide(_worksheet.Cells[_worksheet._sharedFormulas[id].Address]) == - eAddressCollition - .Partly) // If the formula is an array formula and its on the inside the overwriting range throw an exception + Collide(_worksheet.Cells[_worksheet._sharedFormulas[id].Address]) == eAddressCollition.Partly) // If the formula is an array formula and its on the inside the overwriting range throw an exception { throw (new InvalidOperationException("Can not overwrite a part of an array-formula")); } - formulas.Add(id); } } @@ -1727,14 +1584,8 @@ private void SplitFormula(ExcelAddressBase address, int ix) return; //fRange.SetSharedFormulaID(int.MinValue); } - - var firstCellCollide = - address.Collide( - new ExcelAddressBase(fRange._fromRow, fRange._fromCol, fRange._fromRow, fRange._fromCol)); - if (collide == eAddressCollition.Partly && (firstCellCollide == eAddressCollition.Inside || - firstCellCollide == - eAddressCollition - .Equal)) //Do we need to split? Only if the functions first row is inside the new range. + var firstCellCollide = address.Collide(new ExcelAddressBase(fRange._fromRow, fRange._fromCol, fRange._fromRow, fRange._fromCol)); + if (collide == eAddressCollition.Partly && (firstCellCollide == eAddressCollition.Inside || firstCellCollide == eAddressCollition.Equal)) //Do we need to split? Only if the functions first row is inside the new range. { //The formula partly collides with the current range bool fIsSet = false; @@ -1745,7 +1596,6 @@ private void SplitFormula(ExcelAddressBase address, int ix) f.Address = ExcelCellBase.GetAddress(fRange._fromRow, fRange._fromCol, _fromRow - 1, fRange._toCol); fIsSet = true; } - //Left Range if (fRange._fromCol < address._fromCol) { @@ -1761,29 +1611,25 @@ private void SplitFormula(ExcelAddressBase address, int ix) { fIsSet = true; } - if (fRange._fromRow < address._fromRow) f.StartRow = address._fromRow; else { f.StartRow = fRange._fromRow; } - if (fRange._toRow < address._toRow) { f.Address = ExcelCellBase.GetAddress(f.StartRow, f.StartCol, - fRange._toRow, address._fromCol - 1); + fRange._toRow, address._fromCol - 1); } else { f.Address = ExcelCellBase.GetAddress(f.StartRow, f.StartCol, - address._toRow, address._fromCol - 1); + address._toRow, address._fromCol - 1); } - f.Formula = TranslateFromR1C1(formulaR1C1, f.StartRow, f.StartCol); _worksheet.Cells[f.Address].SetSharedFormulaID(f.Index); } - //Right Range if (fRange._toCol > address._toCol) { @@ -1798,7 +1644,6 @@ private void SplitFormula(ExcelAddressBase address, int ix) { fIsSet = true; } - f.StartCol = address._toCol + 1; if (address._fromRow < fRange._fromRow) f.StartRow = fRange._fromRow; @@ -1810,18 +1655,16 @@ private void SplitFormula(ExcelAddressBase address, int ix) if (fRange._toRow < address._toRow) { f.Address = ExcelCellBase.GetAddress(f.StartRow, f.StartCol, - fRange._toRow, fRange._toCol); + fRange._toRow, fRange._toCol); } else { f.Address = ExcelCellBase.GetAddress(f.StartRow, f.StartCol, - address._toRow, fRange._toCol); + address._toRow, fRange._toCol); } - f.Formula = TranslateFromR1C1(formulaR1C1, f.StartRow, f.StartCol); _worksheet.Cells[f.Address].SetSharedFormulaID(f.Index); } - //Bottom Range if (fRange._toRow > address._toRow) { @@ -1839,21 +1682,19 @@ private void SplitFormula(ExcelAddressBase address, int ix) f.Formula = TranslateFromR1C1(formulaR1C1, f.StartRow, f.StartCol); f.Address = ExcelCellBase.GetAddress(f.StartRow, f.StartCol, - fRange._toRow, fRange._toCol); + fRange._toRow, fRange._toCol); _worksheet.Cells[f.Address].SetSharedFormulaID(f.Index); + } } } - private object ConvertData(ExcelTextFormat Format, string v, int col, bool isText) { - if (isText && (Format.DataTypes == null || Format.DataTypes.Length < col)) - return string.IsNullOrEmpty(v) ? null : v; + if (isText && (Format.DataTypes == null || Format.DataTypes.Length < col)) return string.IsNullOrEmpty(v) ? null : v; double d; DateTime dt; - if (Format.DataTypes == null || Format.DataTypes.Length <= col || - Format.DataTypes[col] == eDataTypes.Unknown) + if (Format.DataTypes == null || Format.DataTypes.Length <= col || Format.DataTypes[col] == eDataTypes.Unknown) { string v2 = v.EndsWith("%") ? v.Substring(0, v.Length - 1) : v; if (double.TryParse(v2, NumberStyles.Any, Format.Culture, out d)) @@ -1867,15 +1708,13 @@ private object ConvertData(ExcelTextFormat Format, string v, int col, bool isTex return d / 100; } } - if (DateTime.TryParse(v, Format.Culture, DateTimeStyles.None, out dt)) { return dt; } else { - return string.IsNullOrEmpty(v) ? null : v; - ; + return string.IsNullOrEmpty(v) ? null : v; ; } } else @@ -1914,40 +1753,37 @@ private object ConvertData(ExcelTextFormat Format, string v, int col, bool isTex return v; default: return string.IsNullOrEmpty(v) ? null : v; + } } } - #endregion - #region Public Methods - #region ConditionalFormatting - /// /// Conditional Formatting for this range. /// public IRangeConditionalFormatting ConditionalFormatting { - get { return new RangeConditionalFormatting(_worksheet, new ExcelAddress(Address)); } + get + { + return new RangeConditionalFormatting(_worksheet, new ExcelAddress(Address)); + } } - #endregion - #region DataValidation - /// /// Data validation for this range. /// public IRangeDataValidation DataValidation { - get { return new RangeDataValidation(_worksheet, Address); } + get + { + return new RangeDataValidation(_worksheet, Address); + } } - #endregion - #region LoadFromDataReader - /// /// Load the data from the datareader starting from the top left cell of the range /// @@ -1956,21 +1792,17 @@ public IRangeDataValidation DataValidation /// The name of the table /// The table style to apply to the data /// The filled range - public ExcelRangeBase LoadFromDataReader(IDataReader Reader, bool PrintHeaders, string TableName, - TableStyles TableStyle = TableStyles.None) + public ExcelRangeBase LoadFromDataReader(IDataReader Reader, bool PrintHeaders, string TableName, TableStyles TableStyle = TableStyles.None) { var r = LoadFromDataReader(Reader, PrintHeaders); int rows = r.Rows - 1; if (rows >= 0 && r.Columns > 0) { - var tbl = _worksheet.Tables.Add( - new ExcelAddressBase(_fromRow, _fromCol, _fromRow + (rows <= 0 ? 1 : rows), - _fromCol + r.Columns - 1), TableName); + var tbl = _worksheet.Tables.Add(new ExcelAddressBase(_fromRow, _fromCol, _fromRow + (rows <= 0 ? 1 : rows), _fromCol + r.Columns - 1), TableName); tbl.ShowHeader = PrintHeaders; tbl.TableStyle = TableStyle; } - return r; } @@ -1986,7 +1818,6 @@ public ExcelRangeBase LoadFromDataReader(IDataReader Reader, bool PrintHeaders) { throw (new ArgumentNullException("Reader", "Reader can't be null")); } - int fieldCount = Reader.FieldCount; int col = _fromCol, row = _fromRow; @@ -1997,29 +1828,23 @@ public ExcelRangeBase LoadFromDataReader(IDataReader Reader, bool PrintHeaders) // If no caption is set, the ColumnName property is called implicitly. _worksheet.SetValueInner(row, col++, Reader.GetName(i)); } - row++; col = _fromCol; } - while (Reader.Read()) { for (int i = 0; i < fieldCount; i++) { _worksheet.SetValueInner(row, col++, Reader.GetValue(i)); } - row++; col = _fromCol; } - return _worksheet.Cells[_fromRow, _fromCol, row - 1, _fromCol + fieldCount - 1]; } - #endregion #region LoadFromDataTable - /// /// Load the data from the datatable starting from the top left cell of the range /// @@ -2034,16 +1859,12 @@ public ExcelRangeBase LoadFromDataTable(DataTable Table, bool PrintHeaders, Tabl int rows = (Table.Rows.Count == 0 ? 1 : Table.Rows.Count) + (PrintHeaders ? 1 : 0); if (rows >= 0 && Table.Columns.Count > 0) { - var tbl = _worksheet.Tables.Add( - new ExcelAddressBase(_fromRow, _fromCol, _fromRow + rows - 1, _fromCol + Table.Columns.Count - 1), - Table.TableName); + var tbl = _worksheet.Tables.Add(new ExcelAddressBase(_fromRow, _fromCol, _fromRow + rows - 1, _fromCol + Table.Columns.Count - 1), Table.TableName); tbl.ShowHeader = PrintHeaders; tbl.TableStyle = TableStyle; } - return r; } - /// /// Load the data from the datatable starting from the top left cell of the range /// @@ -2067,14 +1888,11 @@ public ExcelRangeBase LoadFromDataTable(DataTable Table, bool PrintHeaders) { rowArray.Add(Table.Columns.Cast().Select((dc) => { return dc.Caption; }).ToArray()); } - foreach (DataRow dr in Table.Rows) { rowArray.Add(dr.ItemArray); } - - _worksheet._values.SetRangeValueSpecial(_fromRow, _fromCol, _fromRow + rowArray.Count - 1, - _fromCol + Table.Columns.Count - 1, + _worksheet._values.SetRangeValueSpecial(_fromRow, _fromCol, _fromRow + rowArray.Count - 1, _fromCol + Table.Columns.Count - 1, (List list, int index, int rowIx, int columnIx, object value) => { rowIx -= _fromRow; @@ -2087,14 +1905,11 @@ public ExcelRangeBase LoadFromDataTable(DataTable Table, bool PrintHeaders) } }, rowArray); - return _worksheet.Cells[_fromRow, _fromCol, _fromRow + rowArray.Count - 1, - _fromCol + Table.Columns.Count - 1]; + return _worksheet.Cells[_fromRow, _fromCol, _fromRow + rowArray.Count - 1, _fromCol + Table.Columns.Count - 1]; } - #endregion #region LoadFromArrays - /// /// Loads data from the collection of arrays of objects into the range, starting from /// the top-left cell. @@ -2112,10 +1927,8 @@ public ExcelRangeBase LoadFromArrays(IEnumerable Data) rowArray.Add(item); if (maxColumn < item.Length) maxColumn = item.Length; } - if (rowArray.Count == 0) return null; //Issue #57 - _worksheet._values.SetRangeValueSpecial(_fromRow, _fromCol, _fromRow + rowArray.Count - 1, - _fromCol + maxColumn - 1, + _worksheet._values.SetRangeValueSpecial(_fromRow, _fromCol, _fromRow + rowArray.Count - 1, _fromCol + maxColumn - 1, (List list, int index, int rowIx, int columnIx, object value) => { rowIx -= _fromRow; @@ -2135,11 +1948,8 @@ public ExcelRangeBase LoadFromArrays(IEnumerable Data) return _worksheet.Cells[_fromRow, _fromCol, _fromRow + rowArray.Count - 1, _fromCol + maxColumn - 1]; } - #endregion - #region LoadFromCollection - /// /// Load a collection into a the worksheet starting from the top left row of the range. /// @@ -2148,10 +1958,8 @@ public ExcelRangeBase LoadFromArrays(IEnumerable Data) /// The filled range public ExcelRangeBase LoadFromCollection(IEnumerable Collection) { - return LoadFromCollection(Collection, false, TableStyles.None, - BindingFlags.Public | BindingFlags.Instance, null); + return LoadFromCollection(Collection, false, TableStyles.None, BindingFlags.Public | BindingFlags.Instance, null); } - /// /// Load a collection of T into the worksheet starting from the top left row of the range. /// Default option will load all public instance properties of T @@ -2162,10 +1970,8 @@ public ExcelRangeBase LoadFromCollection(IEnumerable Collection) /// The filled range public ExcelRangeBase LoadFromCollection(IEnumerable Collection, bool PrintHeaders) { - return LoadFromCollection(Collection, PrintHeaders, TableStyles.None, - BindingFlags.Public | BindingFlags.Instance, null); + return LoadFromCollection(Collection, PrintHeaders, TableStyles.None, BindingFlags.Public | BindingFlags.Instance, null); } - /// /// Load a collection of T into the worksheet starting from the top left row of the range. /// Default option will load all public instance properties of T @@ -2175,13 +1981,10 @@ public ExcelRangeBase LoadFromCollection(IEnumerable Collection, bool Prin /// Print the property names on the first row. If the property is decorated with a or a that attribute will be used instead of the reflected member name. /// Will create a table with this style. If set to TableStyles.None no table will be created /// The filled range - public ExcelRangeBase LoadFromCollection(IEnumerable Collection, bool PrintHeaders, - TableStyles TableStyle) + public ExcelRangeBase LoadFromCollection(IEnumerable Collection, bool PrintHeaders, TableStyles TableStyle) { - return LoadFromCollection(Collection, PrintHeaders, TableStyle, - BindingFlags.Public | BindingFlags.Instance, null); + return LoadFromCollection(Collection, PrintHeaders, TableStyle, BindingFlags.Public | BindingFlags.Instance, null); } - /// /// Load a collection into the worksheet starting from the top left row of the range. /// @@ -2192,8 +1995,7 @@ public ExcelRangeBase LoadFromCollection(IEnumerable Collection, bool Prin /// Property flags to use /// The properties to output. Must be of type T /// The filled range - public ExcelRangeBase LoadFromCollection(IEnumerable Collection, bool PrintHeaders, - TableStyles TableStyle, BindingFlags memberFlags, MemberInfo[] Members) + public ExcelRangeBase LoadFromCollection(IEnumerable Collection, bool PrintHeaders, TableStyles TableStyle, BindingFlags memberFlags, MemberInfo[] Members) { var type = typeof(T); bool isSameType = true; @@ -2203,41 +2005,33 @@ public ExcelRangeBase LoadFromCollection(IEnumerable Collection, bool Prin } else { - if (Members.Length == 0) //Fixes issue 15555 + if (Members.Length == 0) //Fixes issue 15555 { throw (new ArgumentException("Parameter Members must have at least one property. Length is zero")); } - foreach (var t in Members) { if (t.DeclaringType != null && t.DeclaringType != type) { isSameType = false; } - //Fixing inverted check for IsSubclassOf / Pullrequest from tomdam - if (t.DeclaringType != null && t.DeclaringType != type && - !TypeCompat.IsSubclassOf(type, t.DeclaringType) && - !TypeCompat.IsSubclassOf(t.DeclaringType, type)) + if (t.DeclaringType != null && t.DeclaringType != type && !TypeCompat.IsSubclassOf(type, t.DeclaringType) && !TypeCompat.IsSubclassOf(t.DeclaringType, type)) { - throw new InvalidCastException( - "Supplied properties in parameter Properties must be of the same type as T (or an assignable type from T)"); + throw new InvalidCastException("Supplied properties in parameter Properties must be of the same type as T (or an assignable type from T)"); } } } // create buffer - object[,] values = new object[(PrintHeaders ? Collection.Count() + 1 : Collection.Count()), - Members.Count()]; + object[,] values = new object[(PrintHeaders ? Collection.Count() + 1 : Collection.Count()), Members.Count()]; int col = 0, row = 0; if (Members.Length > 0 && PrintHeaders) { foreach (var t in Members) { - var descriptionAttribute = - t.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as - DescriptionAttribute; + var descriptionAttribute = t.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as DescriptionAttribute; var header = string.Empty; if (descriptionAttribute != null) { @@ -2247,7 +2041,7 @@ public ExcelRangeBase LoadFromCollection(IEnumerable Collection, bool Prin { var displayNameAttribute = t.GetCustomAttributes(typeof(DisplayNameAttribute), false).FirstOrDefault() as - DisplayNameAttribute; + DisplayNameAttribute; if (displayNameAttribute != null) { header = displayNameAttribute.DisplayName; @@ -2261,7 +2055,6 @@ public ExcelRangeBase LoadFromCollection(IEnumerable Collection, bool Prin //_worksheet.SetValueInner(row, col++, header); values[row, col++] = header; } - row++; } @@ -2300,7 +2093,6 @@ public ExcelRangeBase LoadFromCollection(IEnumerable Collection, bool Prin } } } - row++; } @@ -2320,14 +2112,10 @@ public ExcelRangeBase LoadFromCollection(IEnumerable Collection, bool Prin tbl.ShowHeader = PrintHeaders; tbl.TableStyle = TableStyle; } - return r; } - #endregion - #region LoadFromText - /// /// Loads a CSV text into a range starting from the top left cell. /// Default settings is Comma separation @@ -2338,7 +2126,6 @@ public ExcelRangeBase LoadFromText(string Text) { return LoadFromText(Text, new ExcelTextFormat()); } - /// /// Loads a CSV text into a range starting from the top left cell. /// @@ -2394,7 +2181,6 @@ public ExcelRangeBase LoadFromText(string Text, ExcelTextFormat Format) { throw (new Exception(string.Format("Invalid Text Qualifier in line : {0}", line))); } - isQualifier = !isQualifier; QCount += 1; lineQCount++; @@ -2428,23 +2214,18 @@ public ExcelRangeBase LoadFromText(string Text, ExcelTextFormat Format) { if (QCount % 2 == 1) { - throw (new Exception(string.Format("Text delimiter is not closed in line : {0}", - line))); + throw (new Exception(string.Format("Text delimiter is not closed in line : {0}", line))); } - v += c; } } - QCount = 0; } } - if (QCount > 1 && (v != "" && QCount == 2)) { v += new string(Format.TextQualifier, QCount / 2); } - if (lineQCount % 2 == 1) throw (new Exception(string.Format("Text delimiter is not closed in line : {0}", line))); @@ -2453,10 +2234,8 @@ public ExcelRangeBase LoadFromText(string Text, ExcelTextFormat Format) if (col > maxCol) maxCol = col; row++; } - lineNo++; } - // flush _worksheet._values.SetRangeValueSpecial(_fromRow, _fromCol, _fromRow + values.Length - 1, _fromCol + maxCol, (List list, int index, int rowIx, int columnIx, object value) => @@ -2510,10 +2289,8 @@ private string[] GetLines(string text, ExcelTextFormat Format) { list.Add(text.Substring(prevLineStart)); } - return list.ToArray(); } - private bool IsEOL(string text, int ix, string eol) { for (int i = 0; i < eol.Length; i++) @@ -2521,7 +2298,6 @@ private bool IsEOL(string text, int ix, string eol) if (text[ix + i] != eol[i]) return false; } - return ix + eol.Length <= text.Length; } @@ -2533,8 +2309,7 @@ private bool IsEOL(string text, int ix, string eol) /// Create a table with this style /// Use the first row as header /// - public ExcelRangeBase LoadFromText(string Text, ExcelTextFormat Format, TableStyles TableStyle, - bool FirstRowIsHeader) + public ExcelRangeBase LoadFromText(string Text, ExcelTextFormat Format, TableStyles TableStyle, bool FirstRowIsHeader) { var r = LoadFromText(Text, Format); @@ -2544,7 +2319,6 @@ public ExcelRangeBase LoadFromText(string Text, ExcelTextFormat Format, TableSty return r; } - /// /// Loads a CSV file into a range starting from the top left cell. /// @@ -2554,7 +2328,6 @@ public ExcelRangeBase LoadFromText(FileInfo TextFile) { return LoadFromText(File.ReadAllText(TextFile.FullName, Encoding.ASCII)); } - /// /// Loads a CSV file into a range starting from the top left cell. /// @@ -2565,7 +2338,6 @@ public ExcelRangeBase LoadFromText(FileInfo TextFile, ExcelTextFormat Format) { return LoadFromText(File.ReadAllText(TextFile.FullName, Format.Encoding), Format); } - /// /// Loads a CSV file into a range starting from the top left cell. /// @@ -2574,15 +2346,11 @@ public ExcelRangeBase LoadFromText(FileInfo TextFile, ExcelTextFormat Format) /// Create a table with this style /// Use the first row as header /// - public ExcelRangeBase LoadFromText(FileInfo TextFile, ExcelTextFormat Format, TableStyles TableStyle, - bool FirstRowIsHeader) + public ExcelRangeBase LoadFromText(FileInfo TextFile, ExcelTextFormat Format, TableStyles TableStyle, bool FirstRowIsHeader) { - return LoadFromText(File.ReadAllText(TextFile.FullName, Format.Encoding), Format, TableStyle, - FirstRowIsHeader); + return LoadFromText(File.ReadAllText(TextFile.FullName, Format.Encoding), Format, TableStyle, FirstRowIsHeader); } - #endregion - #region GetValue /// @@ -2612,9 +2380,7 @@ public T GetValue() { return ConvertUtil.GetTypedCellValue(Value); } - #endregion - /// /// Get a range with an offset from the top left cell. /// The new range has the same dimensions as the current range @@ -2624,17 +2390,13 @@ public T GetValue() /// public ExcelRangeBase Offset(int RowOffset, int ColumnOffset) { - if (_fromRow + RowOffset < 1 || _fromCol + ColumnOffset < 1 || - _fromRow + RowOffset > ExcelPackage.MaxRows || _fromCol + ColumnOffset > ExcelPackage.MaxColumns) + if (_fromRow + RowOffset < 1 || _fromCol + ColumnOffset < 1 || _fromRow + RowOffset > ExcelPackage.MaxRows || _fromCol + ColumnOffset > ExcelPackage.MaxColumns) { throw (new ArgumentOutOfRangeException("Offset value out of range")); } - - string address = GetAddress(_fromRow + RowOffset, _fromCol + ColumnOffset, _toRow + RowOffset, - _toCol + ColumnOffset); + string address = GetAddress(_fromRow + RowOffset, _fromCol + ColumnOffset, _toRow + RowOffset, _toCol + ColumnOffset); return new ExcelRangeBase(_worksheet, address); } - /// /// Get a range with an offset from the top left cell. /// @@ -2649,23 +2411,16 @@ public ExcelRangeBase Offset(int RowOffset, int ColumnOffset, int NumberOfRows, { throw (new Exception("Number of rows/columns must be greater than 0")); } - NumberOfRows--; NumberOfColumns--; - if (_fromRow + RowOffset < 1 || _fromCol + ColumnOffset < 1 || - _fromRow + RowOffset > ExcelPackage.MaxRows || _fromCol + ColumnOffset > ExcelPackage.MaxColumns || - _fromRow + RowOffset + NumberOfRows < 1 || _fromCol + ColumnOffset + NumberOfColumns < 1 || - _fromRow + RowOffset + NumberOfRows > ExcelPackage.MaxRows || - _fromCol + ColumnOffset + NumberOfColumns > ExcelPackage.MaxColumns) + if (_fromRow + RowOffset < 1 || _fromCol + ColumnOffset < 1 || _fromRow + RowOffset > ExcelPackage.MaxRows || _fromCol + ColumnOffset > ExcelPackage.MaxColumns || + _fromRow + RowOffset + NumberOfRows < 1 || _fromCol + ColumnOffset + NumberOfColumns < 1 || _fromRow + RowOffset + NumberOfRows > ExcelPackage.MaxRows || _fromCol + ColumnOffset + NumberOfColumns > ExcelPackage.MaxColumns) { throw (new ArgumentOutOfRangeException("Offset value out of range")); } - - string address = GetAddress(_fromRow + RowOffset, _fromCol + ColumnOffset, - _fromRow + RowOffset + NumberOfRows, _fromCol + ColumnOffset + NumberOfColumns); + string address = GetAddress(_fromRow + RowOffset, _fromCol + ColumnOffset, _fromRow + RowOffset + NumberOfRows, _fromCol + ColumnOffset + NumberOfColumns); return new ExcelRangeBase(_worksheet, address); } - /// /// Adds a new comment for the range. /// If this range contains more than one cell, the top left comment is returned by the method. @@ -2683,7 +2438,6 @@ public ExcelComment AddComment(string Text, string Author) Author = Thread.CurrentPrincipal.Identity.Name; #endif } - //Check if any comments exists in the range and throw an exception _changePropMethod(this, _setExistsCommentDelegate, null); //Create the comments @@ -2710,7 +2464,7 @@ public void Copy(ExcelRangeBase Destination, ExcelRangeCopyOptionFlags? excelRan { bool sameWorkbook = Destination._worksheet.Workbook == _worksheet.Workbook; ExcelStyles sourceStyles = _worksheet.Workbook.Styles, - styles = Destination._worksheet.Workbook.Styles; + styles = Destination._worksheet.Workbook.Styles; Dictionary styleCashe = new Dictionary(); //Clear all existing cells; @@ -2722,16 +2476,14 @@ public void Copy(ExcelRangeBase Destination, ExcelRangeCopyOptionFlags? excelRan byte flag = 0; Uri hl = null; - var excludeFormulas = excelRangeCopyOptionFlags.HasValue && - (excelRangeCopyOptionFlags.Value & ExcelRangeCopyOptionFlags.ExcludeFormulas) == - ExcelRangeCopyOptionFlags.ExcludeFormulas; + var excludeFormulas = excelRangeCopyOptionFlags.HasValue && (excelRangeCopyOptionFlags.Value & ExcelRangeCopyOptionFlags.ExcludeFormulas) == ExcelRangeCopyOptionFlags.ExcludeFormulas; var cse = new CellsStoreEnumerator(_worksheet._values, _fromRow, _fromCol, _toRow, _toCol); var copiedValue = new List(); while (cse.Next()) { var row = cse.Row; - var col = cse.Column; //Issue 15070 + var col = cse.Column; //Issue 15070 var cell = new CopiedCell { Row = Destination._fromRow + (row - _fromRow), @@ -2746,8 +2498,7 @@ public void Copy(ExcelRangeBase Destination, ExcelRangeCopyOptionFlags? excelRan cell.Formula = _worksheet.GetFormula(cse.Row, cse.Column); if (_worksheet._flags.GetFlagValue(cse.Row, cse.Column, CellFlags.ArrayFormula)) { - Destination._worksheet._flags.SetFlagValue(cse.Row, cse.Column, true, - CellFlags.ArrayFormula); + Destination._worksheet._flags.SetFlagValue(cse.Row, cse.Column, true, CellFlags.ArrayFormula); } } else @@ -2755,7 +2506,6 @@ public void Copy(ExcelRangeBase Destination, ExcelRangeCopyOptionFlags? excelRan cell.Formula = o; } } - if (_worksheet.ExistsStyleInner(row, col, ref i)) { if (sameWorkbook) @@ -2774,7 +2524,6 @@ public void Copy(ExcelRangeBase Destination, ExcelRangeCopyOptionFlags? excelRan i = styles.CloneStyle(sourceStyles, i); styleCashe.Add(oldStyleID, i); } - cell.StyleID = i; } } @@ -2791,7 +2540,6 @@ public void Copy(ExcelRangeBase Destination, ExcelRangeCopyOptionFlags? excelRan { cell.Flag = flag; } - copiedValue.Add(cell); } @@ -2827,14 +2575,11 @@ public void Copy(ExcelRangeBase Destination, ExcelRangeCopyOptionFlags? excelRan i = styles.CloneStyle(sourceStyles, i); styleCashe.Add(oldStyleID, i); } - cell.StyleID = i; } - copiedValue.Add(cell); } } - var copiedMergedCells = new Dictionary(); //Merged cells var csem = new CellsStoreEnumerator(_worksheet.MergedCells._cells, _fromRow, _fromCol, _toRow, _toCol); @@ -2860,8 +2605,7 @@ public void Copy(ExcelRangeBase Destination, ExcelRangeCopyOptionFlags? excelRan } } - Destination._worksheet.MergedCells.Clear(new ExcelAddressBase(Destination._fromRow, Destination._fromCol, - Destination._fromRow + toRow - 1, Destination._fromCol + toCol - 1)); + Destination._worksheet.MergedCells.Clear(new ExcelAddressBase(Destination._fromRow, Destination._fromCol, Destination._fromRow + toRow - 1, Destination._fromCol + toCol - 1)); Destination._worksheet._values.Clear(Destination._fromRow, Destination._fromCol, toRow, toCol); Destination._worksheet._formulas.Clear(Destination._fromRow, Destination._fromCol, toRow, toCol); @@ -2880,11 +2624,9 @@ public void Copy(ExcelRangeBase Destination, ExcelRangeCopyOptionFlags? excelRan if (cell.Formula != null) { - cell.Formula = UpdateFormulaReferences(cell.Formula.ToString(), Destination._fromRow - _fromRow, - Destination._fromCol - _fromCol, 0, 0, Destination.WorkSheet, Destination.WorkSheet, true); + cell.Formula = UpdateFormulaReferences(cell.Formula.ToString(), Destination._fromRow - _fromRow, Destination._fromCol - _fromCol, 0, 0, Destination.WorkSheet, Destination.WorkSheet, true); Destination._worksheet._formulas.SetValue(cell.Row, cell.Column, cell.Formula); } - if (cell.HyperLink != null) { Destination._worksheet._hyperLinks.SetValue(cell.Row, cell.Column, cell.HyperLink); @@ -2892,10 +2634,8 @@ public void Copy(ExcelRangeBase Destination, ExcelRangeCopyOptionFlags? excelRan if (cell.Comment != null) { - Destination.Worksheet.Cells[cell.Row, cell.Column] - .AddComment(cell.Comment.Text, cell.Comment.Author); + Destination.Worksheet.Cells[cell.Row, cell.Column].AddComment(cell.Comment.Text, cell.Comment.Author); } - if (cell.Flag != 0) { Destination._worksheet._flags.SetValue(cell.Row, cell.Column, cell.Flag); @@ -2940,7 +2680,6 @@ public void Copy(ExcelRangeBase Destination, ExcelRangeCopyOptionFlags? excelRan destinationRow.OutlineLevel = Worksheet.Row(range._fromRow + r).OutlineLevel; } } - if (_fromRow == 1 && _toRow == ExcelPackage.MaxRows) { for (int c = 0; c < range.Columns; c++) @@ -2958,7 +2697,6 @@ public void Clear() { Delete(this, false); } - /// /// Creates an array-formula. /// @@ -2969,10 +2707,8 @@ public void CreateArrayFormula(string ArrayFormula) { throw (new Exception("An Arrayformula can not have more than one address")); } - Set_SharedFormula(this, ArrayFormula, this, true); } - //private void Clear(ExcelAddressBase Range) //{ // Clear(Range, true); @@ -2992,7 +2728,6 @@ internal void Delete(ExcelAddressBase Range, bool shift) { fromRow = Range._fromRow; } - if (d != null && Range._fromCol <= d._fromCol && Range._toCol >= d._toCol) //EntireRow? { fromCol = 0; @@ -3041,15 +2776,12 @@ private void DeleteCheckMergedCells(ExcelAddressBase Range) } } } - foreach (var item in removeItems) { Worksheet.MergedCells.Remove(item); } } - #endregion - #region IDisposable Members public void Dispose() @@ -3058,11 +2790,8 @@ public void Dispose() } #endregion - #region "Enumerator" - CellsStoreEnumerator cellEnum; - public IEnumerator GetEnumerator() { Reset(); @@ -3080,7 +2809,10 @@ IEnumerator IEnumerable.GetEnumerator() /// public ExcelRangeBase Current { - get { return new ExcelRangeBase(_worksheet, ExcelAddressBase.GetAddress(cellEnum.Row, cellEnum.Column)); } + get + { + return new ExcelRangeBase(_worksheet, ExcelAddressBase.GetAddress(cellEnum.Row, cellEnum.Column)); + } } /// @@ -3090,15 +2822,13 @@ object IEnumerator.Current { get { - return ((object)(new ExcelRangeBase(_worksheet, - ExcelAddressBase.GetAddress(cellEnum.Row, cellEnum.Column)))); + return ((object)(new ExcelRangeBase(_worksheet, ExcelAddressBase.GetAddress(cellEnum.Row, cellEnum.Column)))); } } //public object FormatedText { get; private set; } int _enumAddressIx = -1; - public bool MoveNext() { if (cellEnum.Next()) @@ -3122,7 +2852,6 @@ public bool MoveNext() return false; } } - return false; } @@ -3131,22 +2860,18 @@ public void Reset() _enumAddressIx = -1; cellEnum = new CellsStoreEnumerator(_worksheet._values, _fromRow, _fromCol, _toRow, _toCol); } - #endregion - private struct SortItem { internal int Row { get; set; } internal T[] Items { get; set; } } - private class Comp : IComparer> { public int[] columns; public bool[] descending; public CultureInfo cultureInfo = CultureInfo.CurrentCulture; public CompareOptions compareOptions = CompareOptions.None; - public int Compare(SortItem x, SortItem y) { var ret = 0; @@ -3156,7 +2881,7 @@ public int Compare(SortItem x, SortItem y) var y1 = y.Items[columns[i]]._value; var isNumX = ConvertUtil.IsNumeric(x1); var isNumY = ConvertUtil.IsNumeric(y1); - if (isNumX && isNumY) //Numeric Compare + if (isNumX && isNumY) //Numeric Compare { var d1 = ConvertUtil.GetValueDouble(x1); var d2 = ConvertUtil.GetValueDouble(y1); @@ -3164,15 +2889,13 @@ public int Compare(SortItem x, SortItem y) { d1 = double.MaxValue; } - if (double.IsNaN(d2)) { d2 = double.MaxValue; } - ret = d1 < d2 ? -1 : (d1 > d2 ? 1 : 0); } - else if (isNumX == false && isNumY == false) //String Compare + else if (isNumX == false && isNumY == false) //String Compare { var s1 = x1 == null ? "" : x1.ToString(); var s2 = y1 == null ? "" : y1.ToString(); @@ -3182,14 +2905,11 @@ public int Compare(SortItem x, SortItem y) { ret = isNumX ? -1 : 1; } - if (ret != 0) return ret * (descending[i] ? -1 : 1); } - return 0; } } - /// /// Sort the range by value of the first column, Ascending. /// @@ -3197,7 +2917,6 @@ public void Sort() { Sort(new int[] { 0 }, new bool[] { false }); } - /// /// Sort the range by value of the supplied column, Ascending. /// The column to sort by within the range. Zerobased @@ -3207,7 +2926,6 @@ public void Sort(int column, bool descending = false) { Sort(new int[] { column }, new bool[] { descending }); } - /// /// Sort the range by value /// @@ -3215,24 +2933,20 @@ public void Sort(int column, bool descending = false) /// Descending if true, otherwise Ascending. Default Ascending. Zerobased /// The CultureInfo used to compare values. A null value means CurrentCulture /// String compare option - public void Sort(int[] columns, bool[] descending = null, CultureInfo culture = null, - CompareOptions compareOptions = CompareOptions.None) + public void Sort(int[] columns, bool[] descending = null, CultureInfo culture = null, CompareOptions compareOptions = CompareOptions.None) { if (columns == null) { columns = new int[] { 0 }; } - var cols = _toCol - _fromCol + 1; foreach (var c in columns) { if (c > cols - 1 || c < 0) { - throw (new ArgumentException( - "Can not reference columns outside the boundries of the range. Note that column reference is zero-based within the range")); + throw (new ArgumentException("Can not reference columns outside the boundries of the range. Note that column reference is zero-based within the range")); } } - var e = new CellsStoreEnumerator(_worksheet._values, _fromRow, _fromCol, _toRow, _toCol); var l = new List>(); SortItem item = new SortItem(); @@ -3244,7 +2958,6 @@ public void Sort(int[] columns, bool[] descending = null, CultureInfo culture = item = new SortItem() { Row = e.Row, Items = new ExcelCoreValue[cols] }; l.Add(item); } - item.Items[e.Column - _fromCol] = e.Value; } @@ -3283,7 +2996,6 @@ public void Sort(int[] columns, bool[] descending = null, CultureInfo culture = { _worksheet._flags.SetValue(row, col, flags[addr]); } - //Move formulas if (formulas.ContainsKey(addr)) { @@ -3295,16 +3007,13 @@ public void Sort(int[] columns, bool[] descending = null, CultureInfo culture = var f = Worksheet._sharedFormulas[sfIx]; if (startAddr._fromRow > row) { - f.Formula = ExcelCellBase.TranslateFromR1C1( - ExcelCellBase.TranslateToR1C1(f.Formula, f.StartRow, f.StartCol), row, f.StartCol); + f.Formula = ExcelCellBase.TranslateFromR1C1(ExcelCellBase.TranslateToR1C1(f.Formula, f.StartRow, f.StartCol), row, f.StartCol); f.StartRow = row; - f.Address = ExcelCellBase.GetAddress(row, startAddr._fromCol, startAddr._toRow, - startAddr._toCol); + f.Address = ExcelCellBase.GetAddress(row, startAddr._fromCol, startAddr._toRow, startAddr._toCol); } else if (startAddr._toRow < row) { - f.Address = ExcelCellBase.GetAddress(startAddr._fromRow, startAddr._fromCol, row, - startAddr._toCol); + f.Address = ExcelCellBase.GetAddress(startAddr._fromRow, startAddr._fromCol, row, startAddr._toCol); } } } @@ -3327,8 +3036,7 @@ public void Sort(int[] columns, bool[] descending = null, CultureInfo culture = } } - private static Dictionary GetItems(CellStore store, int fromRow, int fromCol, int toRow, - int toCol) + private static Dictionary GetItems(CellStore store, int fromRow, int fromCol, int toRow, int toCol) { var e = new CellsStoreEnumerator(store, fromRow, fromCol, toRow, toCol); var l = new Dictionary(); @@ -3336,7 +3044,6 @@ private static Dictionary GetItems(CellStore store, int fromRow { l.Add(e.CellAddress, e.Value); } - return l; } @@ -3346,4 +3053,4 @@ private static void SortSetValue(List list, int index, object va list[index] = new ExcelCoreValue { _value = v._value, _styleId = v._styleId }; } } -} \ No newline at end of file +} diff --git a/src/EPPlus/EPPlus/ExcelWorkbook.cs b/src/EPPlus/EPPlus/ExcelWorkbook.cs index 0854f05b..6858dae6 100644 --- a/src/EPPlus/EPPlus/ExcelWorkbook.cs +++ b/src/EPPlus/EPPlus/ExcelWorkbook.cs @@ -653,11 +653,7 @@ public XmlDocument StylesXml StringBuilder xml = new StringBuilder(""); xml.Append(""); - xml.Append(""); - xml.Append(""); - xml.Append(""); - // As the System.Drawing.Graphics is a GDI+, no matter SixLabors or SkiaSharp, always give inconsistent text measuring result in Calibri font. Use a FangSong instead. - xml.Append(""); + xml.Append(""); xml.Append(""); xml.Append(""); xml.Append(""); diff --git a/src/EPPlus/EPPlus/Magicodes.IE.EPPlus.csproj b/src/EPPlus/EPPlus/Magicodes.IE.EPPlus.csproj index 2425bba6..8fd11ae4 100644 --- a/src/EPPlus/EPPlus/Magicodes.IE.EPPlus.csproj +++ b/src/EPPlus/EPPlus/Magicodes.IE.EPPlus.csproj @@ -68,6 +68,6 @@ - + \ No newline at end of file diff --git a/src/EPPlus/EPPlus/Style/ExcelFont.cs b/src/EPPlus/EPPlus/Style/ExcelFont.cs index ba54b8d2..b6e9bdc1 100644 --- a/src/EPPlus/EPPlus/Style/ExcelFont.cs +++ b/src/EPPlus/EPPlus/Style/ExcelFont.cs @@ -31,7 +31,8 @@ *******************************************************************************/ using System; -using SkiaSharp; +using SixLabors.ImageSharp; +using SixLabors.Fonts; namespace OfficeOpenXml.Style { @@ -206,15 +207,15 @@ public ExcelVerticalAlignmentFont VerticalAlign /// /// Set the font from a Font object /// - /// - public void SetFromFont(SKFont font) + /// + public void SetFromTextRun(TextRun textRun) { - Name = font.Typeface.FamilyName; + var font = textRun.Font; Size = font.Size; - Bold = font.Typeface.IsBold; - Italic = font.Typeface.IsItalic; - UnderLine = font.Metrics.UnderlineThickness != null; - Strike = font.Metrics.StrikeoutThickness != null; + Bold = font.IsBold; + Italic = font.IsItalic; + UnderLine = (textRun.TextDecorations & TextDecorations.Underline) != 0; + Strike = (textRun.TextDecorations & TextDecorations.Strikeout) != 0; } internal override string Id diff --git a/src/EPPlus/EPPlus/Style/ExcelTextFont.cs b/src/EPPlus/EPPlus/Style/ExcelTextFont.cs index fd00228b..c0933ca4 100644 --- a/src/EPPlus/EPPlus/Style/ExcelTextFont.cs +++ b/src/EPPlus/EPPlus/Style/ExcelTextFont.cs @@ -29,14 +29,13 @@ * Jan Källman Initial Release 2009-10-01 * Jan Källman License changed GPL-->LGPL 2011-12-16 *******************************************************************************/ - using System; using SixLabors.ImageSharp; using System.Globalization; using System.Xml; using Magicodes.IE.EPPlus.SixLabors; +using SixLabors.Fonts; using SixLabors.ImageSharp.PixelFormats; -using SkiaSharp; namespace OfficeOpenXml.Style { @@ -64,7 +63,6 @@ public enum eUnderLineType WavyHeavy, Words } - /// /// Type of font strike /// @@ -74,7 +72,6 @@ public enum eStrikeType No, Single } - /// /// Used by Rich-text and Paragraphs. /// @@ -82,9 +79,7 @@ public class ExcelTextFont : XmlHelper { string _path; XmlNode _rootNode; - - internal ExcelTextFont(XmlNamespaceManager namespaceManager, XmlNode rootNode, string path, - string[] schemaNodeOrder) + internal ExcelTextFont(XmlNamespaceManager namespaceManager, XmlNode rootNode, string path, string[] schemaNodeOrder) : base(namespaceManager, rootNode) { SchemaNodeOrder = schemaNodeOrder; @@ -97,15 +92,15 @@ internal ExcelTextFont(XmlNamespaceManager namespaceManager, XmlNode rootNode, s TopNode = node; } } - _path = path; } - string _fontLatinPath = "a:latin/@typeface"; - public string LatinFont { - get { return GetXmlNodeString(_fontLatinPath); } + get + { + return GetXmlNodeString(_fontLatinPath); + } set { CreateTopNode(); @@ -121,45 +116,46 @@ protected internal void CreateTopNode() TopNode = _rootNode.SelectSingleNode(_path, NameSpaceManager); } } - string _fontCsPath = "a:cs/@typeface"; - public string ComplexFont { - get { return GetXmlNodeString(_fontCsPath); } + get + { + return GetXmlNodeString(_fontCsPath); + } set { CreateTopNode(); SetXmlNodeString(_fontCsPath, value); } } - string _boldPath = "@b"; - public bool Bold { - get { return GetXmlNodeBool(_boldPath); } + get + { + return GetXmlNodeBool(_boldPath); + } set { CreateTopNode(); SetXmlNodeString(_boldPath, value ? "1" : "0"); } } - string _underLinePath = "@u"; - public eUnderLineType UnderLine { - get { return TranslateUnderline(GetXmlNodeString(_underLinePath)); } + get + { + return TranslateUnderline(GetXmlNodeString(_underLinePath)); + } set { CreateTopNode(); SetXmlNodeString(_underLinePath, TranslateUnderlineText(value)); } } - string _underLineColorPath = "a:uFill/a:solidFill/a:srgbClr/@val"; - public Color UnderLineColor { get @@ -178,48 +174,49 @@ public Color UnderLineColor set { CreateTopNode(); - SetXmlNodeString(_underLineColorPath, value.ToArgbHex() /*.Substring(2)*/); + SetXmlNodeString(_underLineColorPath, value.ToArgbHex()/*.Substring(2)*/); } } - string _italicPath = "@i"; - public bool Italic { - get { return GetXmlNodeBool(_italicPath); } + get + { + return GetXmlNodeBool(_italicPath); + } set { CreateTopNode(); SetXmlNodeString(_italicPath, value ? "1" : "0"); } } - string _strikePath = "@strike"; - public eStrikeType Strike { - get { return TranslateStrike(GetXmlNodeString(_strikePath)); } + get + { + return TranslateStrike(GetXmlNodeString(_strikePath)); + } set { CreateTopNode(); SetXmlNodeString(_strikePath, TranslateStrikeText(value)); } } - string _sizePath = "@sz"; - public float Size { - get { return GetXmlNodeInt(_sizePath) / 100; } + get + { + return GetXmlNodeInt(_sizePath) / 100; + } set { CreateTopNode(); SetXmlNodeString(_sizePath, ((int)(value * 100)).ToString()); } } - string _colorPath = "a:solidFill/a:srgbClr/@val"; - public Color Color { get @@ -238,12 +235,10 @@ public Color Color set { CreateTopNode(); - SetXmlNodeString(_colorPath, value.ToArgbHex() /*.Substring(2)*/); + SetXmlNodeString(_colorPath, value.ToArgbHex()/*.Substring(2)*/); } } - #region "Translate methods" - private eUnderLineType TranslateUnderline(string text) { switch (text) @@ -258,7 +253,6 @@ private eUnderLineType TranslateUnderline(string text) return (eUnderLineType)Enum.Parse(typeof(eUnderLineType), text); } } - private string TranslateUnderlineText(eUnderLineType value) { switch (value) @@ -272,7 +266,6 @@ private string TranslateUnderlineText(eUnderLineType value) return ret.Substring(0, 1).ToLower(CultureInfo.InvariantCulture) + ret.Substring(1, ret.Length - 1); } } - private eStrikeType TranslateStrike(string text) { switch (text) @@ -285,7 +278,6 @@ private eStrikeType TranslateStrike(string text) return eStrikeType.No; } } - private string TranslateStrikeText(eStrikeType value) { switch (value) @@ -298,22 +290,21 @@ private string TranslateStrikeText(eStrikeType value) return "noStrike"; } } - #endregion - /// - /// Set the font style from a font object + /// Set the font style from a textRun object /// - /// - public void SetFromFont(SKFont font) + /// + public void SetFromTextRun(TextRun textRun) { - LatinFont = font.Typeface.FamilyName; - ComplexFont = font.Typeface.FamilyName; + var font = textRun.Font; + LatinFont = font.Name; + ComplexFont = font.Name; Size = font.Size; - if (font.Typeface.IsBold) Bold = font.Typeface.IsBold; - if (font.Typeface.IsItalic) Italic = font.Typeface.IsItalic; - if(font.Metrics.UnderlineThickness != null) UnderLine = eUnderLineType.Single; - if(font.Metrics.StrikeoutThickness != null) Strike = eStrikeType.Single; + if (font.IsBold) Bold = font.IsBold; + if (font.IsItalic) Italic = font.IsItalic; + if ((textRun.TextDecorations & TextDecorations.Underline) != 0) UnderLine = eUnderLineType.Single; + if ((textRun.TextDecorations & TextDecorations.Strikeout) != 0) Strike = eStrikeType.Single; } } -} \ No newline at end of file +} diff --git a/src/EPPlus/EPPlus/Style/XmlAccess/ExcelFontXml.cs b/src/EPPlus/EPPlus/Style/XmlAccess/ExcelFontXml.cs index b2e28985..f5a03101 100644 --- a/src/EPPlus/EPPlus/Style/XmlAccess/ExcelFontXml.cs +++ b/src/EPPlus/EPPlus/Style/XmlAccess/ExcelFontXml.cs @@ -29,10 +29,11 @@ * Jan Källman Initial Release 2009-10-01 * Jan Källman License changed GPL-->LGPL 2011-12-16 *******************************************************************************/ -using SkiaSharp; using System; +using SixLabors.ImageSharp; using System.Globalization; using System.Xml; +using SixLabors.Fonts; namespace OfficeOpenXml.Style.XmlAccess { @@ -268,14 +269,14 @@ public string VerticalAlign _verticalAlign = value; } } - public void SetFromFont(SKFont font) + public void SetFromTextRun(TextRun textRun) { - Name = font.Typeface.FamilyName; + var font = textRun.Font; Size = font.Size; - Bold = font.Typeface.IsBold; - Italic = font.Typeface.IsItalic; - UnderLine = font.Metrics.UnderlineThickness != null; - Strike = font.Metrics.StrikeoutThickness != null; + Bold = font.IsBold; + Italic = font.IsItalic; + UnderLine = (textRun.TextDecorations & TextDecorations.Underline) != 0; + Strike = (textRun.TextDecorations & TextDecorations.Strikeout) != 0; } public static float GetFontHeight(string name, float size) { diff --git a/src/EPPlus/EPPlusTest/DrawingTest.cs b/src/EPPlus/EPPlusTest/DrawingTest.cs index b39fdff7..96df9ed6 100644 --- a/src/EPPlus/EPPlusTest/DrawingTest.cs +++ b/src/EPPlus/EPPlusTest/DrawingTest.cs @@ -8,8 +8,8 @@ using OfficeOpenXml.Drawing; using OfficeOpenXml.Drawing.Chart; using OfficeOpenXml.Style; +using SixLabors.Fonts; using SixLabors.ImageSharp; -using SkiaSharp; namespace EPPlusTest { @@ -293,9 +293,11 @@ public void Scatter() ser.DataLabel.Fill.Color = Color.BlueViolet; ser.DataLabel.Font.Color = Color.White; ser.DataLabel.Font.Italic = true; - using var skFontTypeface = SKTypeface.FromFamilyName("bookman old style"); - using var font = new SKFont(skFontTypeface, 8f); - ser.DataLabel.Font.SetFromFont(font); + ser.DataLabel.Font.SetFromTextRun( + new TextRun + { + Font = SystemFonts.CreateFont("bookman old style", CultureInfo.CurrentCulture, 8) + }); Assert.IsTrue(chrt.ChartType == eChartType.XYScatterSmoothNoMarkers, "Invalid Charttype"); chrt.Series[0].Header = "Test serie"; chrt = ws.Drawings.AddChart("ScatterChart2", eChartType.XYScatterSmooth) as ExcelScatterChart; @@ -619,9 +621,12 @@ public void Drawings() rt = (ws.Drawings["shape2"] as ExcelShape).RichText.Add("\r\nAdded formated richtext"); rt.Bold = true; rt.Color = Color.DarkGoldenrod; - using var skFontTypeface = SKTypeface.FromFamilyName("Times new roman"); - using var font = new SKFont(skFontTypeface, 18f); - rt.SetFromFont(font); + rt.SetFromTextRun( + new TextRun + { + Font = SystemFonts.CreateFont("Times new roman", CultureInfo.CurrentCulture, 18), + TextDecorations = TextDecorations.Underline + }); rt.UnderLineColor = Color.Green; diff --git a/src/EPPlus/EPPlusTest/WorkSheetTests.cs b/src/EPPlus/EPPlusTest/WorkSheetTests.cs index 12d5d779..02a20bd6 100644 --- a/src/EPPlus/EPPlusTest/WorkSheetTests.cs +++ b/src/EPPlus/EPPlusTest/WorkSheetTests.cs @@ -16,7 +16,7 @@ using System.Threading; using System.Globalization; using System.Threading.Tasks; -using SkiaSharp; +using SixLabors.Fonts; namespace EPPlusTest { @@ -1890,9 +1890,11 @@ public void CloseProblem() using (ExcelRange r = ws.Cells["A1:F1"]) { r.Merge = true; - using var skFontTypeface = SKTypeface.FromFamilyName("Arial", SKFontStyle.Italic); - using var font = new SKFont(skFontTypeface, 18f); - r.Style.Font.SetFromFont(font); + r.Style.Font.SetFromTextRun( + new TextRun + { + Font = SystemFonts.CreateFont("Arial", CultureInfo.CurrentCulture, 18, FontStyle.Italic) + }); r.Style.Font.Color.SetColor(Color.DarkRed); r.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.CenterContinuous; //r.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; @@ -2321,9 +2323,11 @@ public void NamedStyles() var secondNamedStyle = _pck.Workbook.Styles.CreateNamedStyle("first", firstNamedStyle.Style).Style; secondNamedStyle.Font.Bold = true; - using var skFontTypeface = SKTypeface.FromFamilyName("Arial Black"); - using var font = new SKFont(skFontTypeface, 8f); - secondNamedStyle.Font.SetFromFont(font); + secondNamedStyle.Font.SetFromTextRun( + new TextRun + { + Font = SystemFonts.CreateFont("Arial Black", CultureInfo.CurrentCulture, 8) + }); secondNamedStyle.Border.Bottom.Style = ExcelBorderStyle.Medium; secondNamedStyle.Border.Left.Style = ExcelBorderStyle.Medium; diff --git a/src/Magicodes.ExporterAndImporter.Excel/ExcelExporter.cs b/src/Magicodes.ExporterAndImporter.Excel/ExcelExporter.cs index deb24666..86d38f5d 100644 --- a/src/Magicodes.ExporterAndImporter.Excel/ExcelExporter.cs +++ b/src/Magicodes.ExporterAndImporter.Excel/ExcelExporter.cs @@ -269,7 +269,8 @@ public async Task ExportAppendData(string fileName) helper.Export(sheetDataItems); } - return Task.FromResult(NPOI.Extension.SaveToExcelWithXSSFWorkbook(helper.CurrentExcelPackage.GetAsByteArray())); + return Task.FromResult(helper.CurrentExcelPackage.GetAsByteArray()); + //return Task.FromResult(NPOI.Extension.SaveToExcelWithXSSFWorkbook(helper.CurrentExcelPackage.GetAsByteArray())); // todo: use interface, not import NPOI directly } } else @@ -306,7 +307,7 @@ public async Task ExportAppendData(string fileName) { fileName.CheckExcelFileName(); var bytes = await ExportAsByteArray(dataItems); - bytes = NPOI.Extension.SaveToExcelWithXSSFWorkbook(bytes); + //bytes = NPOI.Extension.SaveToExcelWithXSSFWorkbook(bytes); // todo: use interface, not import NPOI directly return bytes.ToExcelExportFileInfo(fileName); } @@ -368,7 +369,8 @@ public async Task ExportAppendData(string fileName) helper.AddExcelWorksheet(); helper.Export(sheetDataItems); } - return Task.FromResult(NPOI.Extension.SaveToExcelWithXSSFWorkbook(helper.CurrentExcelPackage.GetAsByteArray())); + return Task.FromResult(helper.CurrentExcelPackage.GetAsByteArray()); + //return Task.FromResult(NPOI.Extension.SaveToExcelWithXSSFWorkbook(helper.CurrentExcelPackage.GetAsByteArray())); // todo: use interface, not import NPOI directly } } else @@ -402,7 +404,9 @@ public Task ExportWithXSSFWorkbookAsByteArray(DataTable dataItems, Type helper.AddExcelWorksheet(); helper.Export(sheetDataItems); } - return Task.FromResult(NPOI.Extension.SaveToExcelWithXSSFWorkbook(helper.CurrentExcelPackage.GetAsByteArray())); + + return Task.FromResult(helper.CurrentExcelPackage.GetAsByteArray()); + //return Task.FromResult(NPOI.Extension.SaveToExcelWithXSSFWorkbook(helper.CurrentExcelPackage.GetAsByteArray())); // todo: use interface, not import NPOI directly } } else @@ -478,7 +482,8 @@ public Task ExportWithXSSFWorkbookHeaderAsByteArray(string[] items, stri helper.AddExporterHeaderInfoList(headerList); using (var ep = helper.ExportHeaders()) { - return Task.FromResult(NPOI.Extension.SaveToExcelWithXSSFWorkbook(ep.GetAsByteArray())); + return Task.FromResult(ep.GetAsByteArray()); + //return Task.FromResult(NPOI.Extension.SaveToExcelWithXSSFWorkbook(ep.GetAsByteArray())); // todo: use interface, not import NPOI directly } } @@ -525,7 +530,8 @@ public Task ExportHeaderAsByteArray(string[] items, string sheetName = " var helper = new ExportHelper(); using (var ep = helper.ExportHeaders()) { - return Task.FromResult(NPOI.Extension.SaveToExcelWithXSSFWorkbook(ep.GetAsByteArray())); // + return Task.FromResult(ep.GetAsByteArray()); + //return Task.FromResult(NPOI.Extension.SaveToExcelWithXSSFWorkbook(ep.GetAsByteArray())); // } } @@ -605,7 +611,8 @@ public async Task ExportWithXSSFWorkbook(string fileName, DataTa { fileName.CheckExcelFileName(); var bytes = await ExportAsByteArray(dataItems, exporterHeaderFilter, maxRowNumberOnASheet); - bytes = NPOI.Extension.SaveToExcelWithXSSFWorkbook(bytes); + //bytes = NPOI.Extension.SaveToExcelWithXSSFWorkbook(bytes); // todo: use interface, not import NPOI directly + return bytes.ToExcelExportFileInfo(fileName); } diff --git a/src/Magicodes.ExporterAndImporter.Excel/Images/ImageExtensions.cs b/src/Magicodes.ExporterAndImporter.Excel/Images/ImageExtensions.cs index 2310aa0f..524190d3 100644 --- a/src/Magicodes.ExporterAndImporter.Excel/Images/ImageExtensions.cs +++ b/src/Magicodes.ExporterAndImporter.Excel/Images/ImageExtensions.cs @@ -4,7 +4,6 @@ using Magicodes.IE.Core; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Formats; -using SixLabors.ImageSharp.Metadata; namespace Magicodes.IE.Excel.Images { @@ -49,13 +48,7 @@ public static Image GetImageByUrl(this string url, out IImageFormat format) using (var wc = new WebClient()) { wc.Proxy = null; - var image = Image.Load(wc.OpenRead(url), out format); - if (image.Metadata.HorizontalResolution == 0 && image.Metadata.VerticalResolution == 0) - { - image.Metadata.HorizontalResolution = ImageMetadata.DefaultHorizontalResolution; - image.Metadata.VerticalResolution = ImageMetadata.DefaultVerticalResolution; - } - return image; + return Image.Load(wc.OpenRead(url), out format); } } diff --git a/src/Magicodes.ExporterAndImporter.Excel/Magicodes.IE.Excel.csproj b/src/Magicodes.ExporterAndImporter.Excel/Magicodes.IE.Excel.csproj index 55576782..570f7764 100644 --- a/src/Magicodes.ExporterAndImporter.Excel/Magicodes.IE.Excel.csproj +++ b/src/Magicodes.ExporterAndImporter.Excel/Magicodes.IE.Excel.csproj @@ -12,6 +12,5 @@ - \ No newline at end of file