Skip to content

Commit

Permalink
Merge pull request #805 from chen1tian/master
Browse files Browse the repository at this point in the history
bugfix: ExcelToHtmlConverter throw exception when cell has background…
  • Loading branch information
tonyqus authored Sep 22, 2024
2 parents fb2775a + d055e82 commit ae979de
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
19 changes: 16 additions & 3 deletions main/SS/UserModel/IndexedColors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,18 +249,31 @@ static IndexedColors()
mappingIndex.Add(63, IndexedColors.Grey80Percent);
mappingIndex.Add(64, IndexedColors.Automatic);
}

public static IndexedColors TryValueOf(int index)
{
if (mappingIndex.ContainsKey(index))
return mappingIndex[index];

return null;
}

public static IndexedColors ValueOf(string colorName)
{
if (mappingName.ContainsKey(colorName.ToLower()))
return mappingName[colorName.ToLower()];

return null;
}

public static IndexedColors ValueOf(int index)
{
if(mappingIndex.ContainsKey(index))
return mappingIndex[index];
throw new ArgumentException("Illegal IndexedColor index: " + index);
var indexedColors = TryValueOf(index);

if(indexedColors == null)
throw new ArgumentException("Illegal IndexedColor index: " + index);

return indexedColors;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions ooxml/SS/Converter/ExcelToHtmlConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ protected String BuildStyle(IWorkbook workbook, ICellStyle cellStyle)
else if (cellStyle.FillPattern == FillPattern.SolidForeground)
{
//cellStyle
IndexedColors clr=IndexedColors.ValueOf(cellStyle.FillForegroundColor);
IndexedColors clr=IndexedColors.TryValueOf(cellStyle.FillForegroundColor);
string hexstring=null;
if(clr!=null)
{
Expand All @@ -698,7 +698,7 @@ protected String BuildStyle(IWorkbook workbook, ICellStyle cellStyle)
}
else
{
IndexedColors clr = IndexedColors.ValueOf(cellStyle.FillBackgroundColor);
IndexedColors clr = IndexedColors.TryValueOf(cellStyle.FillBackgroundColor);
string hexstring = null;
if (clr != null)
{
Expand Down Expand Up @@ -752,7 +752,7 @@ private void BuildStyle_Border(IWorkbook workbook, StringBuilder style,
}
else
{
IndexedColors clr = IndexedColors.ValueOf(borderColor);
IndexedColors clr = IndexedColors.TryValueOf(borderColor);
if (clr != null)
{
borderStyle.Append(' ');
Expand Down Expand Up @@ -803,7 +803,7 @@ void BuildStyle_Font(IWorkbook workbook, StringBuilder style,
}
else
{
IndexedColors clr = IndexedColors.ValueOf(font.Color);
IndexedColors clr = IndexedColors.TryValueOf(font.Color);
string hexstring = null;
if (clr != null)
{
Expand Down
35 changes: 31 additions & 4 deletions testcases/ooxml/SS/Converter/TestExcelToHtmlConverterSuite.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NPOI.HSSF.UserModel;
using NPOI.SS.Converter;
using NPOI.XSSF.UserModel;
using NUnit.Framework;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -63,10 +64,36 @@ public void TestExcelToHtmlConverter()
private void Test(string fileName)
{
HSSFWorkbook workbook;
workbook = ExcelToHtmlUtils.LoadXls(fileName);
ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter();
excelToHtmlConverter.ProcessWorkbook(workbook);
excelToHtmlConverter.Document.Save(Path.ChangeExtension(fileName, "html")); ;
workbook = ExcelToHtmlUtils.LoadXls(fileName);
ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter();
excelToHtmlConverter.ProcessWorkbook(workbook);
excelToHtmlConverter.Document.Save(Path.ChangeExtension(fileName, "html")); ;
}

[Test]
public void TestExcelToHtmlConverterWithBackground()
{
var fi = new FileInfo(@"..\..\..\..\test-data\spreadsheet\background_color.xlsx");
string fileName = fi.FullName;

XSSFWorkbook workbook;
FileStream inputStream = File.Open(fileName, FileMode.Open);
try
{
workbook = new XSSFWorkbook(inputStream);
}
finally
{
if (inputStream != null)
inputStream.Close();

inputStream = null;
}

ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter();
excelToHtmlConverter.ProcessWorkbook(workbook);
excelToHtmlConverter.Document.Save(Path.ChangeExtension(fileName, "html"));

}
}
}
Binary file not shown.

0 comments on commit ae979de

Please sign in to comment.