Development with Open XML
1. Prepare the development environment
1.1 Open XML SDK
Now the latest version of OpenXML SDK is 2.12.1 (2021 / 1), which needs to be downloaded through nuget. NuGet: https://www.nuget.org/packages/DocumentFormat.OpenXml/
Microsoft's official documents are some 2.5 and some 2.7, which have not been unified to the latest version. You should pay attention to when reading Microsoft documents.
This is the address of Microsoft's official docs: https://docs.microsoft.com/en-us/office/open-xml/open-xml-sdk
API documentation: Open XML SDK 2.5 class library reference
Microsoft has officially provided a series of examples to demonstrate how to use OpenXML SDK for processing in various scenarios:
How do I...(OpenXML SDK)
Usually, the official OpenXML SDK download of Microsoft is version 2.5, which is relatively old. It is not recommended.
Open XML SDK 2.5 for Microsoft Office: https://www.microsoft.com/en-us/download/details.aspx?id=30425
Microsoft has released the source code of OpenXML SDK to GitHub. GitHub: https://github.com/OfficeDev/Open-XML-SDK/
1.2 Open XML SDK Tools
Microsoft also provides a graphical interface tool of OpenXML SDK, which can directly open documents and provide C# code to generate corresponding contents.
Open XML SDK 2.5 for Microsoft Office
https://www.microsoft.com/en-us/download/details.aspx?id=30425
1.3 Open Xml Power Tools
Originally, Microsoft also maintained an OpenXML development tool set, Open XML Power Tools, which is now transferred from Microsoft's branch to an independent branch.
It uses LINQ to XML Technology to provide performance, and provides development tools and examples of various advanced scenarios. The development kit OpenXmlPowerTools is provided on NuGet, which can be installed and used directly. NuGet address: https://www.nuget.org/packages/OpenXmlPowerTools/
The source code can be found on GitHub: https://github.com/EricWhiteDev/Open-Xml-PowerTools
Eric, the maintainer of the library, also created a website Open XML Power Tools Developer Center To provide support. Here, Eric has posted blogs and videos to help you use the library.
However, there is a problem with the library NET Core. At present, it can only be in NET Framework.
After opening the document, the OpenXML SDK actually builds an XML DOM tree in memory. Instead of directly operating the DOM tree, Power Tools uses Linq to XML to rebuild the data structure for operation. After completion, it directly replaces the original XML DOM tree. because. The exception is caused by the problem of XML processing in. NET Core.
Abnormal content
Unhandled exception. System.IO.IOException: Entries cannot be opened multiple times in Update mode. at System.IO.Compression.ZipArchiveEntry.OpenInUpdateMode() at System.IO.Compression.ZipArchiveEntry.Open() at System.IO.Packaging.ZipStreamManager.Open(ZipArchiveEntry zipArchiveEntry, FileMode streamFileMode, FileAccess streamFileAccess) at System.IO.Packaging.ZipPackagePart.GetStreamCore(FileMode streamFileMode, FileAccess streamFileAccess) at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access) at System.IO.Packaging.PackagePart.GetStream(FileMode mode) at DocumentFormat.OpenXml.Packaging.OpenXmlPart.GetStream(FileMode mode) at DocumentFormat.OpenXml.OpenXmlPartRootElement.SaveToPart(OpenXmlPart openXmlPart) at DocumentFormat.OpenXml.OpenXmlPartRootElement.Save() at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.SavePartContent(OpenXmlPart part) at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.TrySavePartContent(OpenXmlPart part) at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.SavePartContents(Boolean save) at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Save() at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Clone(String path, Boolean isEditable, OpenSettings openSettings) at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Clone(String path) at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.SaveAs(String path)
Issues on this issue: