如何使用 NPOI 获取 HSSFRichTextString 中第一个字符的正确 HSSFFont .IsBold 值?

发布于 2025-01-15 07:21:58 字数 3534 浏览 3 评论 0原文

我正在编写一个函数,该函数以 HSSFCell 作为输入,并返回使用 .xml 标签(等)格式化的单元格的输入,为此,我迭代 HSSFRichTextString 并检查每个字符的字体。只要单元格中的第一个字符不是粗体,一切都会按预期进行。不知何故,即使第一个字符是粗体,HSSFFont.IsBold 总是发送错误。 .IsItalic 的行为相同,

我测试以下情况:

情况 1:

  • 单元格值:“My name is Flo”
  • 返回:“My name is Flo"
  • 这是正确的输出。

情况 2:

  • 单元格值:“我的名字e 是 Fl​​o”
  • 返回:“我的名字是 Flo”
  • Font.IsBold 不会将第一个和后面的字母识别为粗体。

情况 3:

  • 单元格值:“我的名字e 是 Fl​​o”
  • 返回:“我的名字;e is Flo"
  • 如果第一个字符是“ ”,则该方法有效。

情况 4:

  • 单元格值:“我的名字Flo
  • 返回:“我的名字是Flo"
  • 它不适用于包含第一个字符的部分,但适用于其余部分。

我不知道是否有人遇到过这个问题或者是否愿意帮助我,但这会很棒。非常感谢你们。

这是我创建的函数:

        private string cellFormatterHSSF (HSSFCell hssfCell, HSSFWorkbook hssfwb)
    {
        try
        {
            HSSFRichTextString rts = (HSSFRichTextString)hssfCell.RichStringCellValue;
            string value = rts.ToString();
            StringBuilder sb = new StringBuilder();
            Boolean bold = false;
            Boolean italic = false;
            for (int i = 1; i < rts.Length; i++)
            {
                HSSFFont font = (HSSFFont)hssfwb.GetFontAt(rts.GetFontAtIndex(i));

                if (font.IsBold && font.IsItalic && !bold && !italic)
                {
                    sb.Append("<BOLD>");
                    sb.Append("<ITALIC>");
                    sb.Append(value[i]);
                    bold = true;
                    italic = true;
                    continue;
                }
                else if (font.IsItalic && !italic)
                {
                    sb.Append("<ITALIC>");
                    sb.Append(value[i]);
                    italic = true;
                }
                else if (font.IsBold && !bold)
                {
                    sb.Append("<BOLD>");
                    sb.Append(value[i]);
                    bold = true;
                }
                else if (!font.IsItalic && italic && !font.IsBold && bold)
                {
                    sb.Append("</ITALIC>");
                    sb.Append("</BOLD>");
                    sb.Append(value[i]);
                    bold = false;
                    italic = false;
                    continue;
                }
                else if (!font.IsBold && bold)
                {
                    sb.Append("</BOLD>");
                    sb.Append(value[i]);
                    bold = false;
                }
                else if (!font.IsItalic && italic)
                {
                    sb.Append("</ITALIC>");
                    sb.Append(value[i]);
                    italic = false;
                }
                else
                    sb.Append(value[i]);
            }
            if (bold)
            {
                sb.Append("</BOLD>");
            }
            if (italic)
            {
                sb.Append("</ITALIC>");
            }
            return rankFormatter(sb.ToString());
        }
        catch (Exception)
        {
            return string.Empty;
        }
    }

I was coding a function that has a HSSFCell as input and returns the input of the cell formated with .xml tags ( etc.) in order to do so I iterate trough the HSSFRichTextString and check the font for each character. Everything works as expected as long as the first character in the cell is not bold. Somehow HSSFFont.IsBold is always sending a false even if the first char is bold. Same behavior for .IsItalic

I testet the following cases:

Case 1:

  • Cell Value:"My name is Flo"
  • Return:"M<Bold>y nam</Bold>e is Flo"
  • This is the correct Output.

Case 2:

  • Cell Value:"My name is Flo"
  • Return:"My name is Flo"
  • Font.IsBold doesnt recognice the first and the following letters as bold.

Case 3:

  • Cell Value:" My name is Flo"
  • Return:" <Bold>My nam</Bold>e is Flo"
  • It works if the first Character is a " ".

Case 4:

  • Cell Value:" My name is Flo"
  • Return:" My name is <Bold>Flo</Bold>"
  • It doesn not work for the section including the first char, but for the rest.

I dont know if someone came across this issue or is aber to help me but this would be great. Thanks alot guys.

Here is the funktion I created:

        private string cellFormatterHSSF (HSSFCell hssfCell, HSSFWorkbook hssfwb)
    {
        try
        {
            HSSFRichTextString rts = (HSSFRichTextString)hssfCell.RichStringCellValue;
            string value = rts.ToString();
            StringBuilder sb = new StringBuilder();
            Boolean bold = false;
            Boolean italic = false;
            for (int i = 1; i < rts.Length; i++)
            {
                HSSFFont font = (HSSFFont)hssfwb.GetFontAt(rts.GetFontAtIndex(i));

                if (font.IsBold && font.IsItalic && !bold && !italic)
                {
                    sb.Append("<BOLD>");
                    sb.Append("<ITALIC>");
                    sb.Append(value[i]);
                    bold = true;
                    italic = true;
                    continue;
                }
                else if (font.IsItalic && !italic)
                {
                    sb.Append("<ITALIC>");
                    sb.Append(value[i]);
                    italic = true;
                }
                else if (font.IsBold && !bold)
                {
                    sb.Append("<BOLD>");
                    sb.Append(value[i]);
                    bold = true;
                }
                else if (!font.IsItalic && italic && !font.IsBold && bold)
                {
                    sb.Append("</ITALIC>");
                    sb.Append("</BOLD>");
                    sb.Append(value[i]);
                    bold = false;
                    italic = false;
                    continue;
                }
                else if (!font.IsBold && bold)
                {
                    sb.Append("</BOLD>");
                    sb.Append(value[i]);
                    bold = false;
                }
                else if (!font.IsItalic && italic)
                {
                    sb.Append("</ITALIC>");
                    sb.Append(value[i]);
                    italic = false;
                }
                else
                    sb.Append(value[i]);
            }
            if (bold)
            {
                sb.Append("</BOLD>");
            }
            if (italic)
            {
                sb.Append("</ITALIC>");
            }
            return rankFormatter(sb.ToString());
        }
        catch (Exception)
        {
            return string.Empty;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文