簡體   English   中英

如何使用C#在特定標題旁邊的Word文檔中獲取MS Word表?

[英]How to get MS Word table in a word document next to specific heading using C#?

我正在嘗試使用c#在單詞表的相應列下標識特定值。 嘗試:1-如果表索引已知-將表范圍獲取為文本並對其進行迭代以到達特定的行和列。

問題是-表位於文檔中任何位置的特定標題下。 索引是可變的,因此不能依賴索引。

正如辛迪指出的那樣,當前形式的問題過於廣泛。 我不確定您如何閱讀文件。 另外,我不確定您所說的“某些標題”是什么意思,您知道目標表之前的文字是什么嗎?

不過,您可以查看以下示例,該代碼使用GemBox.Document

string cellValue = null;

string headingText = "My Heading Text";
int rowIndex = 0;
int columnIndex = 0;

// Load Word document.
DocumentModel document = DocumentModel.Load("Sample.docx");

// Iterate through all paragraphs.
foreach (Paragraph paragraph in document.GetChildElements(true, ElementType.Paragraph))
{
    // Check that paragraph is of heading style.
    ParagraphStyle style = paragraph.ParagraphFormat.Style;
    if (style == null || !style.Name.Contains("heading"))
        continue;

    // Check that heading paragraph contains our text.
    if (!paragraph.Content.ToString().Contains(headingText))
        continue;

    // Get first table after the heading paragraph.
    Table table = new ContentRange(paragraph.Content.End, document.Content.End)
        .GetChildElements(ElementType.Table)
        .Cast<Table>()
        .First();

    // Get cell value.
    cellValue = table.Rows[rowIndex].Cells[columnIndex].Content.ToString();
    break;
}

Console.WriteLine(cellValue);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM