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

ExcelToHtmlConverter.ProcessWorkbook throws Illegal IndexedColor index: 0 error on .NET Core 3.1 #621

Open
Evanyi0016 opened this issue Aug 18, 2021 · 6 comments
Labels
Milestone

Comments

@Evanyi0016
Copy link

Evanyi0016 commented Aug 18, 2021

以下代码在.net framework中可顺利执行,但在core 3.1下2.5.4版本报错:Illegal IndexedColor index: 0;

` ///


/// (NPOI)Excel转HTML
///

/// HTML文件的的名称(不带后缀)
/// 需要转换Excel的绝对路径
///
public string ExcelToHtml(string Filename)
{
IWorkbook workbook;
//获取后缀名称
string fileExt = Path.GetExtension(Filename).ToLower();
//判断是否可以打开该文件,如果出错则文件已经打开或者有异常
try
{
System.IO.FileStream stream = System.IO.File.OpenWrite(Filename);
stream.Close();
}
catch
{
return null;
}
using (FileStream fs = new FileStream(Filename, FileMode.Open, FileAccess.Read))
{
//如果是XLSX格式选择XSSFWorkbook ,如果是XLS格式选择HSSFWorkbook
if (fileExt == ".xlsx") { workbook = new XSSFWorkbook(fs); } else if (fileExt == ".xls") { workbook = new HSSFWorkbook(fs); } else { workbook = null; }
if (workbook == null) { return null; }
}

        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;

    }`

simple

以下是Excel样例。
simple.xlsx

@tonyqus
Copy link
Member

tonyqus commented Aug 18, 2021

Can you provide the Excel file?

@Evanyi0016
Copy link
Author

Can you provide the Excel file?

The sample has been uploaded.

@tonyqus tonyqus added this to the NPOI 2.5.6 milestone Aug 25, 2021
@tonyqus tonyqus changed the title core 3.1下2.5.4版本调用ExcelToHtmlConverter的ProcessWorkbook方法报Illegal IndexedColor index: 0错误! ExcelToHtmlConverter.ProcessWorkbook throws Illegal IndexedColor index: 0 error on .NET Core 3.1 Sep 14, 2021
@tonyqus
Copy link
Member

tonyqus commented Dec 12, 2021

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.

@isaacfi
Copy link

isaacfi commented Jan 12, 2023

Hi,

Is there any way to avoid this exception?

@PhenX
Copy link

PhenX commented Feb 13, 2023

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

@quiian
Copy link

quiian commented Nov 6, 2023

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. :)

@tonyqus tonyqus modified the milestones: NPOI vNext, NPOI 2.7.1 Nov 6, 2023
@tonyqus tonyqus modified the milestones: NPOI 2.7.1, NPOI 2.7.2 May 9, 2024
@tonyqus tonyqus modified the milestones: NPOI 2.7.2, NPOI 2.7.3, NPOI 2.8.0 Nov 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants