How to Programmatically Create OOXML Strict Documents

Viewed 83

It is possible to manually save an Excel spreadsheet as a "Strict Open XML" file in Excel filtype drop-down when using "Save as" instead of saving it as the default transitional OOXML file. The file format is the same .xlsx for both Strict and Transitional file format variants of the OOXML standard.

How can I do the same thing programmatically through automated workflows in e.g. C#? The purpose is to bulk convert Transitional Excel files to Strict Excel files.

I have found these code snippets part of the Office XML SDK:

Can I use them or other ways of doing bulk conversion from Transitional to Strict?

UPDATE: I am in dialogue with the developers of Open XML SDK in this issue.

1 Answers

The only way possible, that I have found after extensive research is to use Excel.Interop and have your Excel installation handle the conversion in the background. It is not a pretty solution, because it is Excel dependent, but it is a programmatic approach.

You can find the Excel.Interop package here: https://www.nuget.org/packages/Microsoft.Office.Interop.Excel

I had an issue, where the package would not find my Excel installation, so I had to make a new reference to the Excel Interop DLL on your computer. Add reference > Browse > C:\Windows\assembly\GAC\Microsoft.Office.Interop.Excel and pick the DLL from the folder in here. I also added the DLL from the subfolder in this path C:\Windows\assembly\GAC_MSIL\office

Conversion code

using Excel = Microsoft.Office.Interop.Excel;

void Convert_Transitional_to_Strict(string input_filepath, string output_filepath)
{
    Excel.Application app = new Excel.Application(); // Create Excel instance
    app.DisplayAlerts = false; // Don't display any Excel prompts
    Excel.Workbook excelWorkbook = app.Workbooks.Open(input_filepath); // Create workbook instance and open Excel Workbook for conversion
    excelWorkbook.SaveAs(output_filepath, 61); // Save file as .xlsx Strict
    excelWorkbook.Close(); // Close the Workbook
    app.Quit(); // Quit Excel Application
}

PS. If anyone finds a programmatic approach using Open XML SDK or another C# framework, do still post your answer.

Related