尋找預設的段落樣式
管理 WordprocessingML 文件中的資訊教學課程中的第一個工作是,尋找文件中的預設段落樣式。
範例
描述
下列範例會開啟 Office Open XML WordprocessingML 文件、尋找文件和封裝的樣式部分,然後執行尋找預設樣式名稱的查詢。如需有關 Office Open XML 文件封裝及其組成部分的詳細資訊,請參閱 Office Open XML WordprocessingML 文件的詳細資料。
查詢會尋找名稱為 w:style 的節點,其中擁有名稱為 w:type 且值為 "paragraph" 的屬性,同時也擁有名稱為 w:default 且值為 "1" 的屬性。由於這些屬性只有一個 XML 節點,查詢會使用 Enumerable.First 運算子,將集合轉換為單一子句。接著,它會取得名稱為 w:styleId 之屬性的值。
這個範例會使用 WindowsBase 組件的類別。它會使用 System.IO.Packaging 命名空間中的型別。
程式碼
const string fileName = "SampleDoc.docx";
const string documentRelationshipType =
"https://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
const string stylesRelationshipType =
"https://schemas.openxmlformats.org/officeDocument/2006/relationships/styles";
const string wordmlNamespace =
"https://schemas.openxmlformats.org/wordprocessingml/2006/main";
XNamespace w = wordmlNamespace;
XDocument xDoc = null;
XDocument styleDoc = null;
using (Package wdPackage = Package.Open(fileName, FileMode.Open, FileAccess.Read))
{
PackageRelationship docPackageRelationship =
wdPackage.GetRelationshipsByType(documentRelationshipType).FirstOrDefault();
if (docPackageRelationship != null)
{
Uri documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative),
docPackageRelationship.TargetUri);
PackagePart documentPart = wdPackage.GetPart(documentUri);
// Load the document XML in the part into an XDocument instance.
xDoc = XDocument.Load(XmlReader.Create(documentPart.GetStream()));
// Find the styles part. There will only be one.
PackageRelationship styleRelation =
documentPart.GetRelationshipsByType(stylesRelationshipType).FirstOrDefault();
if (styleRelation != null)
{
Uri styleUri = PackUriHelper.ResolvePartUri(documentUri, styleRelation.TargetUri);
PackagePart stylePart = wdPackage.GetPart(styleUri);
// Load the style XML in the part into an XDocument instance.
styleDoc = XDocument.Load(XmlReader.Create(stylePart.GetStream()));
}
}
}
// The following query finds all the paragraphs that have the default style.
string defaultStyle =
(string)(
from style in styleDoc.Root.Elements(w + "style")
where (string)style.Attribute(w + "type") == "paragraph"&&
(string)style.Attribute(w + "default") == "1"
select style
).First().Attribute(w + "styleId");
Console.WriteLine("The default style is: {0}", defaultStyle);
Imports <xmlns:w="https://schemas.openxmlformats.org/wordprocessingml/2006/main">
Module Module1
Sub Main()
Const fileName As String = "SampleDoc.docx"
Const documentRelationshipType As String = _
"https://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
Const stylesRelationshipType As String = _
"https://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
Dim xDoc As XDocument = Nothing
Dim styleDoc As XDocument = Nothing
Using wdPackage As Package = Package.Open(fileName, FileMode.Open, FileAccess.Read)
Dim docPackageRelationship As PackageRelationship = _
wdPackage.GetRelationshipsByType(documentRelationshipType).FirstOrDefault()
If docPackageRelationship IsNot Nothing Then
Dim documentUri As Uri = PackUriHelper.ResolvePartUri(New Uri("/", UriKind.Relative), _
docPackageRelationship.TargetUri)
Dim documentPart As PackagePart = wdPackage.GetPart(documentUri)
' Load the document XML in the part into an XDocument instance.
xDoc = XDocument.Load(XmlReader.Create(documentPart.GetStream()))
' Find the styles part. There will only be one.
Dim styleRelation As PackageRelationship = _
documentPart.GetRelationshipsByType(stylesRelationshipType).FirstOrDefault()
If styleRelation IsNot Nothing Then
Dim styleUri As Uri = _
PackUriHelper.ResolvePartUri(documentUri, styleRelation.TargetUri)
Dim stylePart As PackagePart = wdPackage.GetPart(styleUri)
' Load the style XML in the part into an XDocument instance.
styleDoc = XDocument.Load(XmlReader.Create(stylePart.GetStream()))
End If
End If
End Using
' The following query finds all the paragraphs that have the default style.
Dim defParas As IEnumerable(Of XElement) = _
From style In styleDoc.Root.<w:style> _
Where style.@w:type.Equals("paragraph") And _
style.@w:default.Equals("1") _
Select style
' Then find the style of the first.
Dim defaultStyle As String = defParas.First().@w:styleId
Console.WriteLine("The default style is: " & defaultStyle)
End Sub
End Module
註解
這個範例會產生下列輸出:
The default style is: Normal
後續步驟
在下一個範例中,我們將建立可在文件中尋找所有段落及其樣式的類似查詢: