Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: ExcelToHtmlConverter throw exception when cell has background… #805

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions main/SS/UserModel/IndexedColors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,18 +250,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.
Loading