Serialize.OpenXml.CodeGen
0.4.2-beta
See the version list below for details.
dotnet add package Serialize.OpenXml.CodeGen --version 0.4.2-beta
NuGet\Install-Package Serialize.OpenXml.CodeGen -Version 0.4.2-beta
<PackageReference Include="Serialize.OpenXml.CodeGen" Version="0.4.2-beta" />
paket add Serialize.OpenXml.CodeGen --version 0.4.2-beta
#r "nuget: Serialize.OpenXml.CodeGen, 0.4.2-beta"
// Install Serialize.OpenXml.CodeGen as a Cake Addin #addin nuget:?package=Serialize.OpenXml.CodeGen&version=0.4.2-beta&prerelease // Install Serialize.OpenXml.CodeGen as a Cake Tool #tool nuget:?package=Serialize.OpenXml.CodeGen&version=0.4.2-beta&prerelease
Open XML Document Code Generator
The Open XML Document Code Generator is a dotnet standard library that contains processes that convert OpenXML documents (such as .docx, .pptx, and .xlsx) into CodeCompileUnit objects that can be transformed into source code using any class that inherits from the CodeDomProvider class. This allows source code generation into other .NET languages, such as Visual Basic.net, for greater learning possibilities.
Please be aware that while this project is producing working code, I would consider this is in beta status and not yet ready for production. More testing should be done before it will be production ready.
As of version 0.4.0-beta, this project is now generating code that can reproduce entire basic OpenXml documents. More testing is required of advanced document types before this project can move beyond beta status.
Looking for a front end interface for this project? Check out DocxToSource. There is still much work to be done but it is becoming a nice replacement for the old OpenXml SDK Productivity Tool.
Examples
Generate the CodeCompileUnit to process manually:
using System;
using System.IO;
using System.Linq;
using System.Text;
using Serialize.OpenXml.CodeGen;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
namespace CodeGenSample
{
class Program
{
private static readonly CodeGeneratorOptions Cgo = new CodeGeneratorOptions()
{
BracingStyle = "C"
};
static void Main(string[] args)
{
var sourceFile = new FileInfo(@"C:\Temp\Sample1.xlsx");
var targetFile = new FileInfo(@"C:\Temp\Sample1.cs");
using (var source = sourceFile.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (var xlsx = SpreadsheetDocument.Open(source, false))
{
if (xlsx != null)
{
var codeString = new StringBuilder();
var cs = new CSharpCodeProvider();
// This will build the CodeCompileUnit object containing all of
// the commands that would create the source code to rebuild Sample1.xlsx
var code = xlsx.GenerateSourceCode();
// This will convert the CodeCompileUnit into C# source code
using (var sw = new StringWriter(codeString))
{
cs.GenerateCodeFromCompileUnit(code, sw, Cgo);
}
// Save the source code to the target file
using (var target = targetFile.Open(FileMode.Create, FileAccess.ReadWrite))
{
using (var tw = new StreamWriter(target))
{
tw.Write(codeString.ToString().Trim());
}
target.Close();
}
}
}
source.Close();
}
Console.WriteLine("Press any key to quit");
Console.ReadKey(true);
}
}
}
Generate the actual source code as a string value:
using System;
using System.IO;
using System.Linq;
using System.Text;
using Serialize.OpenXml.CodeGen;
using System.CodeDom.Compiler;
using Microsoft.VisualBasic;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
namespace CodeGenSample
{
class Program
{
static void Main(string[] args)
{
var sourceFile = new FileInfo(@"./Sample1.xlsx");
var targetFile = new FileInfo(@"./Sample1.vb");
using (var source = sourceFile.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (var xlsx = SpreadsheetDocument.Open(source, false))
{
if (xlsx != null)
{
// Generate VB.NET source code
var vb = new VBCodeProvider();
// Save the source code to the target file
using (var target = targetFile.Open(FileMode.Create, FileAccess.ReadWrite))
{
using (var tw = new StreamWriter(target))
{
// Providing the CodeDomProvider object as a parameter will
// cause the method to return the source code as a string
tw.Write(xlsx.GenerateSourceCode(vb).Trim());
}
target.Close();
}
}
}
source.Close();
}
Console.WriteLine("Press any key to quit");
Console.ReadKey(true);
}
}
}
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. |
.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 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. 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. |
-
.NETStandard 2.0
- DocumentFormat.OpenXml (>= 2.14.0)
- System.CodeDom (>= 5.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.5.0-beta | 609 | 5/30/2022 |
0.4.2-beta | 676 | 11/22/2021 |
0.4.1-beta | 259 | 8/8/2021 |
0.4.0-beta | 195 | 8/2/2021 |
0.3.2-alpha | 361 | 7/30/2020 |
0.3.1-alpha | 380 | 7/25/2020 |
0.3.0-alpha | 245 | 7/20/2020 |
0.2.1-alpha | 305 | 7/3/2020 |
0.2.0-alpha | 287 | 6/27/2020 |
0.1.0-alpha | 272 | 6/24/2020 |
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
## [0.4.2-beta] - 2021-11-22
## Changed
- Refactored the variable name generation process to reuse existing variable names
when they become available.
- *\[Breaking Change\]:* Added `UseUniqueVariableNames` property to `ISerializeSettings`
interface. This allows to switch between unique and reused variable names.
- *\[Breaking Change\]:* Changed `typeCounts` parameter to `types` in the
`IOpenXmlElementHandler.BuildCodeStatements(...)` method to account for the repurposing
of existing variable name.
- Update DocumentFormat.OpenXml reference to 2.14.0.
## Fixed
- Use the correct `CodeExpression` classes for the `XmlNodeType` parameter of the
`OpenXmlMiscNode` constructor.
## [0.4.1-beta] - 2021-08-08
### Changed
- Refactored the using directive generation logic to better match how the OpenXML SDK
Productivity Tool used to create them. Using directive aliases are now dynamically
generated based on the OpenXml object that the code output is based on.
- *\[Breaking Change\]:* Updated the `namespace` parameter type in the `IOpenXmlElementHandler`
and `IOpenXmlPartHandler` interface methods from `ISet<string>` to `IDictionary<string, string>`
to account for the new namespace/using directive generation logic. The following
interface methods are impacted:
- `IOpenXmlElementHandler.BuildCodeStatements(...)`
- `IOpenXmlPartHandler.BuildEntryMethodCodeStatements(...)`
- `IOpenXmlPartHandler.BuildHelperMethod(...)`
### Fixed
- Issue related to Hyperlink and external relationship references were not being added properly
in all `OpenXmlPart` code creation scenarios.
- Use the right constructor parameters for `OpenXmlMiscNode` objects.
## [0.4.0-beta] - 2021-08-02
### Added
- New `ISerializeSettings` interface to allows greater flexibility in the source code generation.
- New `IOpenXmlHandler`, `IOpenXmlElementHandler`, and `IOpenXmlPartHandler` interfaces that will
allow developers to control how source code is created.
### Changed
- Change visibility of many of the static method helpers so developers can use them in their custom
code generation.
- Update DocumentFormat.OpenXml reference to 2.13.0.
### Fixed
- Make sure that the return type of generated element methods include the namespace alias if
needed.
- Choose between the default method or contentType parameter method for the custom OpenXmlPart.AddNewPart
methods (ex: pkg.AddExtendedFilePropertiesPart() or mainDocumentPart.AddImagePart("image/x-emf"))
## [0.3.2-alpha] - 2020-07-30
### Changed
- Updated process to account for more OpenXmlPart classes that may require custom AddNewPart methods
to initialize.
- Changed the `CreatePackage` method to take in a `String` parameter for the full file path of the target file
instead of a `Stream` when generating code for `OpenXmlPackage` objects. This was to avoid using a C# `ref`
parameter that made using the generated code in a C# project more difficult to use.
### Fixed
- TargetInvocationException/FormatException when trying to parse a value that is not valid for
`OpenXmlSimpleType` derived types being evaluated. [See this](https://github.com/OfficeDev/Open-XML-SDK/issues/780)
for more details.
- When encountering OpenXmlUnknownElement objects, make sure to initialize them with the appropriate `ctor` method.
- Correct the initialization parameters for the generated `AddExternalRelationship` method.
- Issue where AddPart methods for OpenXmlPart paths that have already been visited are generated on variables
that do not exist.
## [0.3.1-alpha] - 2020-07-25
### Fixed
- TargetInvocationException/FormatException when trying to parse a value that is not valid for
the `EnumValue` type being evaluated. [See this](https://github.com/OfficeDev/Open-XML-SDK/issues/780)
for more details.
## [0.3.0-alpha] - 2020-07-20
### Changed
- Update DocumentFormat.OpenXml reference to 2.11.3.
### Fixed
- Ambiguous Match Exception occuring when trying to identify parts that need to use the
`AddImagePart` initialization method.
## [0.2.1-alpha] - 2020-07-03
### Changed
- Change the parameters for all of the methods to `ref` parameters. This changes the generated
VB code to create `byref` parameters instead of `byval` ones.
## [0.2.0-alpha] - 2020-06-27
### Added
- Added documentation output
### Changed
- Use the alias `AP` for DocumentFormat.OpenXml.ExtendedProperties namespace objects
- Use the `AddImagePart` method for initializing `ImagePart` objects.
- Included the content type parameter for the `AddNewPart` method for `EmbeddedPackagePart` objects.
## [0.1.0-alpha] - 2020-06-24
### Added
- Added initial project to convert OpenXml SDK based documents to source code files.