使用poi从.net中提取.doc中的表

时间:2017-11-14 09:51:30

标签: c# npoi

我收到以下错误

  

"表格范围超出此范围段落的编号"

当使用npoi移植到.net文件时从.doc文件中提取表格感谢tonyqus(https://github.com/tonyqus/npoi)。以下是方法

public Table GetTable(Paragraph paragraph)
{
    if (!paragraph.IsInTable())
    {
        throw new ArgumentException("This paragraph doesn't belong to a table");
    }

    Range r = paragraph;

    if (r._parent != this)
    {
        throw new ArgumentException("This paragraph is not a child of this range");
    }

    r.InitAll();
    int tableLevel = paragraph.GetTableLevel();
    int tableEndInclusive = r._parStart;

    if (r._parStart != 0)
    {
        Paragraph previous = new Paragraph(_paragraphs[r._parStart - 1], this);
        if (previous.IsInTable() && previous.GetTableLevel() == tableLevel
                                 && previous._sectionEnd >= r._sectionStart)
        {
            throw new ArgumentException("This paragraph is not the first one in the table");
        }
    }

    Range overallRange = _doc.GetOverallRange();
    int limit = _paragraphs.Count;

    for (; tableEndInclusive < limit - 1; tableEndInclusive++)
    {
        Paragraph next = new Paragraph(
        _paragraphs[tableEndInclusive + 1], overallRange);

        if (!next.IsInTable() || next.GetTableLevel() < tableLevel)
        {
            break;
        }
    }

    InitAll();

    if (tableEndInclusive > _parEnd)
    {
        throw new IndexOutOfRangeException($"The table's bounds [{_parStart};{tableEndInclusive}] fall outside of this Range paragraph's number [{_parStart};{_parEnd}]");
    }

    if (tableEndInclusive < 0)
    {
        throw new IndexOutOfRangeException("The table's end is negative, which isn't allowed!");
    }

    int endOffsetExclusive = _paragraphs[tableEndInclusive].End;

    return new Table(paragraph.StartOffset, endOffsetExclusive, this, paragraph.GetTableLevel());
}

如何解决这个错误?

0 个答案:

没有答案