[英]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.