npoi
npoi copied to clipboard
ExcelToHtmlConverter.ProcessWorkbook throws Illegal IndexedColor index: 0 error on .NET Core 3.1
以下代码在.net framework中可顺利执行,但在core 3.1下2.5.4版本报错:Illegal IndexedColor index: 0;
` ///
ExcelToHtmlConverter eth = new ExcelToHtmlConverter();
// 设置输出参数
eth.OutputColumnHeaders = true; //是否输出ColumnHeader
eth.OutputHiddenColumns = false;//是否在表头插入一行序列号
eth.OutputHiddenRows = false;//是否输出HiddenRow
eth.OutputLeadingSpacesAsNonBreaking = true; //不清楚
eth.OutputRowNumbers = false;//是否在列头插入一行序列号
eth.UseDivsToSpan = false; //不清楚
// 加载Excel文件
eth.ProcessWorkbook(workbook); //***此处报错:Illegal IndexedColor index: 0****
// 保存文件值当前路径
return eth.Document.InnerXml;
// string htmlFile = Application.StartupPath + "\\" + HtmlName + ".html";
// eth.Document.Save(htmlFile);
// return htmlFile;
}`
以下是Excel样例。 simple.xlsx
Can you provide the Excel file?
Can you provide the Excel file?
The sample has been uploaded.
The exception thrown by IndexedColor.ValueOf() method. In some cases, the color value=0. But there is no pre-defined color whose index equals 0.
Hi,
Is there any way to avoid this exception?
Here is a simple workaround :
(typeof(IndexedColors).GetField("mappingIndex", BindingFlags.Static | BindingFlags.NonPublic)
.GetValue(null) as Dictionary<int, IndexedColors>)[0] = IndexedColors.Black;
But a real fix is still very welcome
Thanks for the "fix", @PhenX. In my case I needed white instead of black, so it seems the color is not fixed, but you lead me on the right track. :)