From 0aa080ff1754c699e5cc9e37f4b2815fbf09a7d3 Mon Sep 17 00:00:00 2001 From: ABykiev Date: Fri, 8 Sep 2023 23:30:58 +0300 Subject: [PATCH] Change check logic --- OpenXmlFormats/Spreadsheet/Sheet.cs | 2 +- ooxml/XSSF/UserModel/XSSFSheet.cs | 39 +++++++++++++++++++++++------ openxml4Net/Util/XmlHelper.cs | 13 +++------- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/OpenXmlFormats/Spreadsheet/Sheet.cs b/OpenXmlFormats/Spreadsheet/Sheet.cs index 9407accd6..50a1e5310 100644 --- a/OpenXmlFormats/Spreadsheet/Sheet.cs +++ b/OpenXmlFormats/Spreadsheet/Sheet.cs @@ -2600,7 +2600,7 @@ public static CT_SheetFormatPr Parse(XmlNode node, XmlNamespaceManager namespace return null; CT_SheetFormatPr ctObj = new CT_SheetFormatPr(); ctObj.baseColWidth = XmlHelper.ReadUInt(node.Attributes["baseColWidth"]); - ctObj.defaultColWidth = XmlHelper.ReadDouble(node.Attributes["defaultColWidth"], 8.43); + ctObj.defaultColWidth = XmlHelper.ReadDouble(node.Attributes["defaultColWidth"]); ctObj.defaultRowHeight = XmlHelper.ReadDouble(node.Attributes["defaultRowHeight"]); ctObj.customHeight = XmlHelper.ReadBool(node.Attributes["customHeight"]); ctObj.zeroHeight = XmlHelper.ReadBool(node.Attributes["zeroHeight"]); diff --git a/ooxml/XSSF/UserModel/XSSFSheet.cs b/ooxml/XSSF/UserModel/XSSFSheet.cs index 45143fcc1..f35bbbdb2 100644 --- a/ooxml/XSSF/UserModel/XSSFSheet.cs +++ b/ooxml/XSSF/UserModel/XSSFSheet.cs @@ -152,12 +152,13 @@ public double DefaultColumnWidth get { CT_SheetFormatPr pr = worksheet.sheetFormatPr; - return pr == null ? 8.43 : pr.defaultColWidth; + return (pr == null || pr.defaultColWidth == 0.0) ? 8.43 : pr.defaultColWidth; } set { var pr = GetSheetTypeSheetFormatPr(); pr.defaultColWidth = value; + pr.baseColWidth = 0; } } @@ -2029,9 +2030,26 @@ public List GetHyperlinkList() public double GetColumnWidth(int columnIndex) { CT_Col col = columnHelper.GetColumn(columnIndex, false); - double width = (col == null || !col.IsSetWidth()) - ? DefaultColumnWidth - : col.width; + + double width = 0; + + if (col != null && col.IsSetWidth()) + width = col.width; + else + { + CT_SheetFormatPr pr = worksheet.sheetFormatPr; + if (pr != null) + { + if (pr.defaultColWidth != 0.0) + width = pr.defaultColWidth; + else if (pr.baseColWidth != 0) + width = pr.baseColWidth; + } + + if (width == 0) + width = DefaultColumnWidth; + } + return Math.Round(width * 256, 2); } @@ -5960,7 +5978,13 @@ public double GetDefaultColWidthInPixel() double fontwidth; double width_px; - if (worksheet.sheetFormatPr.baseColWidth != 0.0) + var width = worksheet.sheetFormatPr.defaultColWidth; //string length with padding + if (width != 0.0) + { + double widthInPx = width * MaximumDigitWidth; + width_px = widthInPx + (8 - widthInPx % 8); // round up to the nearest multiple of 8 pixels + } + else if (worksheet.sheetFormatPr.baseColWidth != 0) { double MDW = MaximumDigitWidth; var length = worksheet.sheetFormatPr.baseColWidth; //string length with out padding @@ -5970,8 +5994,7 @@ public double GetDefaultColWidthInPixel() } else { - var width = worksheet.sheetFormatPr.defaultColWidth; //string length with padding - double widthInPx = width * MaximumDigitWidth; + double widthInPx = DefaultColumnWidth * MaximumDigitWidth; width_px = widthInPx + (8 - widthInPx % 8); // round up to the nearest multiple of 8 pixels } @@ -6057,7 +6080,7 @@ int x } } } - lblforbreak: + lblforbreak: int EMUwidth = Units.PixelToEMU((int)Math.Round(width_px, 1)); if (x >= EMUwidth) { diff --git a/openxml4Net/Util/XmlHelper.cs b/openxml4Net/Util/XmlHelper.cs index 37b03bea8..9564dbd0e 100644 --- a/openxml4Net/Util/XmlHelper.cs +++ b/openxml4Net/Util/XmlHelper.cs @@ -170,14 +170,14 @@ public static bool ReadBool(XmlAttribute attr) { return ReadBool(attr, false); } - public static double ReadDouble(XmlAttribute attr, double defaultValue) + public static double ReadDouble(XmlAttribute attr) { if (attr == null) - return defaultValue; + return 0.0; string s = attr.Value; if (s == "") { - return defaultValue; + return 0.0; } else { @@ -188,16 +188,11 @@ public static double ReadDouble(XmlAttribute attr, double defaultValue) } else { - return defaultValue; + return 0.0; } } } - public static double ReadDouble(XmlAttribute attr) - { - return ReadDouble(attr, 0); - } - public static double? ReadDoubleNull(XmlAttribute attr) { if (attr == null)