HOW TO:將自訂 XML 組件加入至文件而不啟動 Microsoft Office
更新:2007 年 11 月
適用於 |
---|
本主題內容僅適用特定的 Microsoft Office 版本。 Microsoft Office 版本
如需詳細資訊,請參閱依應用程式和專案類型提供的功能。 |
您可以將自訂 XML 組件加入至文件,而不需要先啟動 Microsoft Office Excel、Microsoft Office PowerPoint 或 Microsoft Office Word。如果您想要將 XML 資料以文件形式存放到尚未安裝 Microsoft Office 的電腦上,例如伺服器,這種做法會很便利。如需詳細資訊,請參閱自訂 XML 組件概觀。
文件必須是 Open XML 格式,例如 .docx、.xlsx 或 .pptx。您必須先啟動 Microsoft Office 應用程式,才有辦法存取二進位檔案 (Binary File) 格式的自訂 XML 組件。
若要在不啟動 Microsoft Office 的情況下加入自訂 XML 組件,請使用 Package 和 PackagePart 類別 (Class)。如需這些類別的詳細資訊,請參閱Windows Presentation Foundation 中的文件。
若要在不啟動 Microsoft Office 的情況下將自訂 XML 組件加入至文件
建立一個代表您要加入至自訂 XML 組件的 XmlDocument 物件。
Dim xmlString As String = _ "<?xml version=""1.0"" encoding=""utf-8"" ?>" & _ "<employees https://schemas.microsoft.com/vsto/samples"">" & _ "<employee>" & _ "<name>Karina Leal</name>" & _ "<hireDate>1999-04-01</hireDate>" & _ "<title>Manager</title>" & _ "</employee>" & _ "</employees>" Dim reader As StringReader = New StringReader(xmlString) Dim xmlDoc As XmlDocument = New XmlDocument() xmlDoc.Load(reader)
string xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "<employees xmlns=\"https://schemas.microsoft.com/vsto/samples\">" + "<employee>" + "<name>Karina Leal</name>" + "<hireDate>1999-04-01</hireDate>" + "<title>Manager</title>" + "</employee>" + "</employees>"; StringReader reader = new StringReader(xmlString); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(reader);
建立一個代表您要加入至文件中的自訂 XML 組件的新 PackagePart。然後,將您先前建立之 XmlDocument 物件的 XML 儲存到新的自訂 XML 組件中。
Using package As Package = package.Open(fullDocumentPath, FileMode.Open, _ FileAccess.ReadWrite) Dim uriPartTarget As Uri = New Uri("/customXml/employee1.xml", UriKind.Relative) If Not package.PartExists(uriPartTarget) Then Dim customXml As PackagePart = package.CreatePart(uriPartTarget, _ "application/vnd.openxmlformats-officedocument.customXmlProperties+xml") Using partStream As Stream = customXml.GetStream(FileMode.Create, _ FileAccess.ReadWrite) xmlDoc.Save(partStream) End Using End If End Using
using (Package package = Package.Open(fullDocumentPath, FileMode.Open, FileAccess.ReadWrite)) { Uri uriPartTarget = new Uri("/customXml/employee1.xml", UriKind.Relative); if (!package.PartExists(uriPartTarget)) { PackagePart customXml = package.CreatePart(uriPartTarget, "application/vnd.openxmlformats-officedocument.customXmlProperties+xml"); using (Stream partStream = customXml.GetStream(FileMode.Create, FileAccess.ReadWrite)) { xmlDoc.Save(partStream); } } }
範例
下列程式碼範例會將自訂 XML 組件加入至名為 Employees.docx 的 Word 文件,這份文件位於 %UserProfile%\My Documents 資料夾 (適用於 Windows XP 和更早版本) 或 %UserProfile%\Documents folder (適用於 Windows Vista)。
這個範例主要是用在主控台應用程式 (Console Application) 中,但是您可以加以修改以運用在任何可以和 .NET Framework 3.0 (含) 更新版本搭配使用的應用程式中,例如 Windows Presentation Foundation (WPF) 應用程式。
若要修改這個範例以與 Excel 或 PowerPoint 文件一起使用,請修改 Main 方法以將文件的完整路徑傳入 AddCustomXmlPart 方法中。
Sub Main()
AddCustomXmlPart(Environment.GetFolderPath( _
Environment.SpecialFolder.MyDocuments) & "\Employees.docx")
End Sub
Private Sub AddCustomXmlPart(ByVal fullDocumentPath As String)
Dim xmlDoc As XmlDocument = GetXmlDocumentFromString()
If xmlDoc IsNot Nothing Then
Using package As Package = package.Open(fullDocumentPath, FileMode.Open, _
FileAccess.ReadWrite)
Dim uriPartTarget As Uri = New Uri("/customXml/employee1.xml", UriKind.Relative)
If Not package.PartExists(uriPartTarget) Then
Dim customXml As PackagePart = package.CreatePart(uriPartTarget, _
"application/vnd.openxmlformats-officedocument.customXmlProperties+xml")
Using partStream As Stream = customXml.GetStream(FileMode.Create, _
FileAccess.ReadWrite)
xmlDoc.Save(partStream)
End Using
End If
End Using
End If
End Sub
Private Function GetXmlDocumentFromString() As XmlDocument
Dim xmlString As String = _
"<?xml version=""1.0"" encoding=""utf-8"" ?>" & _
"<employees https://schemas.microsoft.com/vsto/samples"">" & _
"<employee>" & _
"<name>Karina Leal</name>" & _
"<hireDate>1999-04-01</hireDate>" & _
"<title>Manager</title>" & _
"</employee>" & _
"</employees>"
Dim reader As StringReader = New StringReader(xmlString)
Dim xmlDoc As XmlDocument = New XmlDocument()
xmlDoc.Load(reader)
Return xmlDoc
End Function
static void Main(string[] args)
{
AddCustomXmlPart(Environment.GetFolderPath(
Environment.SpecialFolder.MyDocuments) + @"\Employees.docx");
}
private static void AddCustomXmlPart(string fullDocumentPath)
{
XmlDocument xmlDoc = GetXmlDocumentFromString();
if (xmlDoc != null)
{
using (Package package = Package.Open(fullDocumentPath, FileMode.Open,
FileAccess.ReadWrite))
{
Uri uriPartTarget = new Uri("/customXml/employee1.xml", UriKind.Relative);
if (!package.PartExists(uriPartTarget))
{
PackagePart customXml = package.CreatePart(uriPartTarget,
"application/vnd.openxmlformats-officedocument.customXmlProperties+xml");
using (Stream partStream = customXml.GetStream(FileMode.Create,
FileAccess.ReadWrite))
{
xmlDoc.Save(partStream);
}
}
}
}
}
private static XmlDocument GetXmlDocumentFromString()
{
string xmlString =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
"<employees xmlns=\"https://schemas.microsoft.com/vsto/samples\">" +
"<employee>" +
"<name>Karina Leal</name>" +
"<hireDate>1999-04-01</hireDate>" +
"<title>Manager</title>" +
"</employee>" +
"</employees>";
StringReader reader = new StringReader(xmlString);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(reader);
return xmlDoc;
}
編譯程式碼
這個範例需要參考 WindowsBase.dll 組件。此組件已包含在 .NET Framework 3.0 以後的版本中。
這個範例假設您已經針對下列命名空間 (Namespace) 使用 using (適用於 C#) 或 Imports (適用於 Visual Basic) 陳述式:
System.IO
System.IO.Packaging
System.Xml
請參閱
工作
HOW TO:使用應用程式層級增益集將自訂 XML 組件加入至文件