Awesome
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);
}
}
}