OpenXmlTools 2.0.0
dotnet add package OpenXmlTools --version 2.0.0
NuGet\Install-Package OpenXmlTools -Version 2.0.0
<PackageReference Include="OpenXmlTools" Version="2.0.0" />
<PackageVersion Include="OpenXmlTools" Version="2.0.0" />
<PackageReference Include="OpenXmlTools" />
paket add OpenXmlTools --version 2.0.0
#r "nuget: OpenXmlTools, 2.0.0"
#:package OpenXmlTools@2.0.0
#addin nuget:?package=OpenXmlTools&version=2.0.0
#tool nuget:?package=OpenXmlTools&version=2.0.0
OpenXmlTools
Description
OpenXmlTools is a .NET library based on OpenXML SDK that is specifically designed for processing Word documents. This tool library provides rich APIs to simplify the creation, editing and formatting of Word documents, including text replacement, bookmark processing, table operations (style setting, cell merging, content locking), font control, paragraph alignment and other functions, greatly improving the efficiency of OpenXML usage.
Software Architecture
- Built on .NET Standard 2.0/2.1 and .NET Framework 4.72/4.8
- Uses C# 8.0 language features
- Depends on DocumentFormat.OpenXml 3.3.0
- Supports cross-platform usage (Windows, Linux, macOS)
Features
- Text Processing: Supports accurate text search and replacement, including cross-Run element text replacement
- Bookmark Operations: Quickly locate and replace content through bookmark names
- Table Operations:
- Dynamic table data filling
- Cell merging
- Border style setting
- Alignment control (horizontal and vertical)
- Font format setting
- Lock table content to prevent editing
- Document Generation: Provides WordDocumentBuilder static class to support creating paragraphs, headings, tables, images, hyperlinks and other elements
- Document Comparison: Built-in WordDocumentComparer tool to compare differences between two Word documents
- Style Control: Supports setting fonts, font sizes, bold, italic and other formats
Installation
NuGet Package Manager
Install-Package OpenXmlTools
.NET CLI
dotnet add package OpenXmlTools
PackageReference
<PackageReference Include="OpenXmlTools" Version="2.0.0" />
Instructions
1. Basic Text Replacement
using OpenXmlTools.OpenXML;
using System.Collections.Generic;
using System.IO;
// Prepare replacement dictionary
var replacements = new Dictionary<string, string>
{
{ "Name", "John Doe" },
{ "Date", "2025-01-01" }
};
// Read template file
using var fileStream = File.OpenRead("template.docx");
// Perform text replacement
var resultStream = OpenXMLWordHelper.WriteData(fileStream, replacements);
// Save result
using var outputFile = File.Create("output.docx");
resultStream.CopyTo(outputFile);
2. Table Data Filling
using OpenXmlTools.OpenXML;
using OpenXmlTools.OpenXML.Word;
using System.Collections.Generic;
// Prepare table data
var tablesData = new Dictionary<string, List<List<string>>>
{
{
"0", // Table index (starting from 0)
new List<List<string>>
{
new List<string> { "Product Name", "Quantity", "Price" },
new List<string> { "Product A", "10", "$100" },
new List<string> { "Product B", "5", "$200" }
}
}
};
// Read template file
using var fileStream = File.OpenRead("template.docx");
// Fill table data
var resultStream = OpenXMLWordHelper.WriteData(fileStream, tablesData);
// Save result
using var outputFile = File.Create("output.docx");
resultStream.CopyTo(outputFile);
3. Table Cell Merging
using OpenXmlTools.OpenXML;
using OpenXmlTools.OpenXML.Word;
using System.Collections.Generic;
// Prepare table data and merge regions
var wordTables = new List<WordTable>
{
new WordTable
{
TableIdentifier = "0", // Table index
TableDatas = new List<List<string>>
{
new List<string> { "Project", "Subproject", "Description" },
new List<string> { "Project A", "Subproject 1", "Description 1" },
new List<string> { "Project A", "Subproject 2", "Description 2" },
new List<string> { "Project B", "Subproject 3", "Description 3" }
},
MergeRegions = new List<(int startRow, int startCol, int endRow, int endCol)>
{
// Merge first column of rows 2-3
(1,2,0,0)
}
}
};
// Read template file
using var fileStream = File.OpenRead("template.docx");
// Fill table data and merge cells
var resultStream = OpenXMLWordHelper.WriteData(fileStream, wordTables);
// Save result
using var outputFile = File.Create("output.docx");
resultStream.CopyTo(outputFile);
4. Bookmark Content Replacement
using OpenXmlTools.OpenXML.Word;
using System.Collections.Generic;
using System.IO;
// Prepare bookmark replacement dictionary
var bookmarks = new Dictionary<string, string>
{
{ "BookmarkName1", "Replacement Content 1" },
{ "BookmarkName2", "Replacement Content 2" }
};
// Read template file
using var fileStream = File.OpenRead("template.docx");
// Replace bookmark content
var resultStream = WordBookmarkHelper.ReplaceDocumentBookmarks(fileStream, bookmarks);
// Save result
using var outputFile = File.Create("output.docx");
resultStream.CopyTo(outputFile);
5. Creating a New Word Document
using OpenXmlTools.OpenXML.Word;
using DocumentFormat.OpenXml.Wordprocessing;
using System.IO;
// Create document stream
using var stream = new MemoryStream();
// Create Word document
var document = WordDocumentBuilder.CreateWordDocument(stream);
var body = document.MainDocumentPart.Document.Body;
// Add paragraph
WordDocumentBuilder.AddParagraph(body, "This is a paragraph", isBold: true);
// Add heading
WordDocumentBuilder.AddHeading(body, "This is a heading", 1);
// Add table
var tableData = new List<List<string>>
{
new List<string> { "Column 1", "Column 2", "Column 3" },
new List<string> { "Data 1", "Data 2", "Data 3" }
};
WordDocumentBuilder.AddTable(body, tableData);
// Save document
document.Save();
// Save to file
using var outputFile = File.Create("new_document.docx");
stream.Position = 0;
stream.CopyTo(outputFile);
6. Comparing Two Word Documents
using OpenXmlTools.OpenXML.Word;
using System.IO;
// Read two documents to compare
using var doc1Stream = File.OpenRead("document1.docx");
using var doc2Stream = File.OpenRead("document2.docx");
// Compare documents
var comparisonResult = WordDocumentComparer.CompareWordDocuments(doc1Stream, doc2Stream);
// Save comparison result
using var outputFile = File.Create("comparison_result.docx");
comparisonResult.CopyTo(outputFile);
API Reference
Main Classes and Methods
OpenXMLWordHelper
WriteData(): Fill text and table data in Word documentsWriteDataAndLockTables(): Fill data and lock table content
WordBookmarkHelper
ReplaceDocumentBookmarks(): Replace bookmark content in documents
WordTextHelper
ReplaceText(): Replace text in documentsAdvancedReplaceText(): Advanced text replacement with regex support
WordTableHelper
FillTables(): Fill table dataCreateTableWithData(): Create tables with data
WordTableMergeHelper
BatchMergeCells(): Batch merge cells
WordTableLocker
LockTable(): Lock table contentLockTables(): Batch lock table content
WordDocumentBuilder
CreateWordDocument(): Create new Word documentsAddParagraph(): Add paragraphsAddHeading(): Add headingsAddTable(): Add tablesAddImage(): Add imagesAddHyperlink(): Add hyperlinks
WordDocumentComparer
CompareWordDocuments(): Compare two Word documents
Supported Platforms
- .NET Framework 4.72
- .NET Framework 4.8
- .NET Standard 2.0
- .NET Standard 2.1
Contribution
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 is compatible. net48 is compatible. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.7.2
- DocumentFormat.OpenXml (>= 3.3.0)
-
.NETFramework 4.8
- DocumentFormat.OpenXml (>= 3.3.0)
-
.NETStandard 2.0
- DocumentFormat.OpenXml (>= 3.3.0)
-
.NETStandard 2.1
- DocumentFormat.OpenXml (>= 3.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial release of OpenXmlTools.