Skip to content

Commit

Permalink
Change check logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Bykiev committed Sep 8, 2023
1 parent b4b8b0b commit 0aa080f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion OpenXmlFormats/Spreadsheet/Sheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
Expand Down
39 changes: 31 additions & 8 deletions ooxml/XSSF/UserModel/XSSFSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -2029,9 +2030,26 @@ public List<IHyperlink> 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);
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -6057,7 +6080,7 @@ int x
}
}
}
lblforbreak:
lblforbreak:
int EMUwidth = Units.PixelToEMU((int)Math.Round(width_px, 1));
if (x >= EMUwidth)
{
Expand Down
13 changes: 4 additions & 9 deletions openxml4Net/Util/XmlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)
Expand Down

0 comments on commit 0aa080f

Please sign in to comment.