OOXML - Linq 提取带有特定标题的 Word 表
OOXML - Linq to Extract Word Tables With a Certain Caption
我正在使用 Open XML SDK 2.6 尝试更新 Word 文档。我可以使用 元素来识别 table。我的问题是,给定具有特定 w:val 属性的 table,我需要使用什么 Linq 查询来获取对整个 结构的引用?
DocumentFormat.OpenXml.Packaging.WordprocessingDocument doc = WordprocessingDocument.Open(@"D:\dev\openxml\table.docx", false);
var tables = doc.MainDocumentPart.Document.Descendants<Table>().ToList();
//WHAT LINQ STATEMENT DO I USE?
DOCX TBL XML
<w:tbl>
<w:tblPr>
<w:tblW w:w="4814" w:type="dxa"/>
<w:tblInd w:w="247" w:type="dxa"/>
<w:tblBorders>
<w:top w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:left w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:bottom w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:right w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:insideH w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:insideV w:val="single" w:sz="4" w:space="0" w:color="auto"/>
</w:tblBorders>
<w:tblLayout w:type="fixed"/>
<w:tblLook w:val="0000" w:firstRow="0" w:lastRow="0" w:firstColumn="0" w:lastColumn="0" w:noHBand="0" w:noVBand="0"/>
<w:tblCaption w:val="TBL_TEST"/>
</w:tblPr>
<w:tblGrid>
<w:gridCol w:w="1468"/>
<w:gridCol w:w="1444"/>
<w:gridCol w:w="1902"/>
</w:tblGrid>
<w:tr>
</w:tr>
</w:tbl>
您可以尝试这样的操作:
XDocument doc;
XNamespace ns =
XNamespace.Get(@"http://schemas.openxmlformats.org/wordprocessingml/2006/main");
using (WordprocessingDocument doc = WordprocessingDocument.Open(stream, true))
{
using (StreamReader sr = new StreamReader(doc.MainDocumentPart.GetStream()))
using (XmlReader xr = XmlReader.Create(sr))
xdoc = XDocument.Load(xr);
XElement table;
//Descendants() gets all children,grandchildren etc.
//First get document -> body ->
XElement tablecapt = xdoc.Elements().First().Elements().First()
.Descendants().Where(d => d.Name == XName.Get("tblCaption", ns)
&& d.Value == "TBL_TEST").FirstOrDefault();
if (tablecapt != null)
table = tablecapt.Parent.Parent;
}
暂时无法测试,但我想应该不会太远。
我正在使用 Open XML SDK 2.6 尝试更新 Word 文档。我可以使用
DocumentFormat.OpenXml.Packaging.WordprocessingDocument doc = WordprocessingDocument.Open(@"D:\dev\openxml\table.docx", false);
var tables = doc.MainDocumentPart.Document.Descendants<Table>().ToList();
//WHAT LINQ STATEMENT DO I USE?
DOCX TBL XML
<w:tbl>
<w:tblPr>
<w:tblW w:w="4814" w:type="dxa"/>
<w:tblInd w:w="247" w:type="dxa"/>
<w:tblBorders>
<w:top w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:left w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:bottom w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:right w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:insideH w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:insideV w:val="single" w:sz="4" w:space="0" w:color="auto"/>
</w:tblBorders>
<w:tblLayout w:type="fixed"/>
<w:tblLook w:val="0000" w:firstRow="0" w:lastRow="0" w:firstColumn="0" w:lastColumn="0" w:noHBand="0" w:noVBand="0"/>
<w:tblCaption w:val="TBL_TEST"/>
</w:tblPr>
<w:tblGrid>
<w:gridCol w:w="1468"/>
<w:gridCol w:w="1444"/>
<w:gridCol w:w="1902"/>
</w:tblGrid>
<w:tr>
</w:tr>
</w:tbl>
您可以尝试这样的操作:
XDocument doc;
XNamespace ns =
XNamespace.Get(@"http://schemas.openxmlformats.org/wordprocessingml/2006/main");
using (WordprocessingDocument doc = WordprocessingDocument.Open(stream, true))
{
using (StreamReader sr = new StreamReader(doc.MainDocumentPart.GetStream()))
using (XmlReader xr = XmlReader.Create(sr))
xdoc = XDocument.Load(xr);
XElement table;
//Descendants() gets all children,grandchildren etc.
//First get document -> body ->
XElement tablecapt = xdoc.Elements().First().Elements().First()
.Descendants().Where(d => d.Name == XName.Get("tblCaption", ns)
&& d.Value == "TBL_TEST").FirstOrDefault();
if (tablecapt != null)
table = tablecapt.Parent.Parent;
}
暂时无法测试,但我想应该不会太远。