It is possible to manually save an Excel spreadsheet as a "Strict Open XML" file type in Excel when using "Save as" instead of saving it as the default workbook OOXML file, which is a "Transitional" variant of the OOXML standard. The extension is .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:
https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.conformanceclass?view=openxml-2.8.1
https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.workbook.conformance?view=openxml-2.8.1#documentformat-openxml-spreadsheet-workbook-conformance
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.
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
I want to create a .xls file using OpenXML SDK. I am able to create .xlsx and .xlsm files but when i save it as .xls and open in my system it pops an error. Then I have to do save as again for it to work properly.
Is there any way to save files directly in .xls format?
Simple answer is No, you cannot do it.
The reason being .xls format is proprietary to Microsoft. OpenXml SDK cannot understand or write to that format.
If you want to store data in .xls format, then you need to use Microsoft office excel interop objects.
Excel namespace interop
If you indeed want to take the route of adding interops to your application to save to .xls format, I would also suggest you to look into following SO answer because adding reference to interop objects in .NET core is not straight forward.
ms interop in .net core
I have recently installed visio 2010. it has VBA 7.0.
I am trying to write a code to open a file dialog, choose an excel file and open it.
I used this code
Set fd = Application.FileDialog(msoFileDialogFilePicker)
but i get the following error:
Run-time error '438':
Object doesn't support this property or method
i already have the Microsoft Office 14. Object Library as reference.
Do you have an idea? what i should do?
If you use this code in Visio
Set fd = Application.FileDialog(msoFileDialogFilePicker)
the word Application refers to Visio and as you can see in Object Browser, Visio.Application class has no such method as FileDialog. Access and Excel applications have it, but not Visio.
In order to use it in Visio you need to add reference to either Microsoft Excel Object Library or Microsoft Access Object Library. Then you need to use the code below to create an instance of FileDialog (select a proper version depending on what reference did you add to your project - Excel or Access)
'If you have reference to Microsoft Excel Object Library
Set fd = Excel.Application.FileDialog(msoFileDialogFilePicker)
or
'If you have reference to Microsoft Access Object Library
Set fd = Access.Application.FileDialog(msoFileDialogFilePicker)
Like explained by #mielk, Visio does not have the Application.FileDialog. You could use the method of Excel (in this case you'll have to have Excel installed, and started to invoke its methods) or use pure WinAPI from VBA (i.e. GetOpenFileName function). This option is explained here for example:
http://visguy.com/vgforum/index.php?topic=738.0
I have an .xlsx file that when run through the open Office SDK 2.5 generates an error that the document is invalid and contains multiple validation errors involving the slicerCache and invalid attribute values.
I can attach more information about the actual XML if needed from the xlsx file, however my question is actually this. Excel still opens the document without an error. Not even a request to "repair" the document.
I am curious why using the Microsoft open office XML SDK generates validation errors, yet office is still able to open these documents.
Does office make a best guess? Or is the SDK given by microsoft not entirely accurate??
Thanks.
This is a formatting issue as far as I can tell. When you save it in xlsx it saves it as a workbook, not a spreadsheet. I would save it in a different file format or see if there libraries that your sdk needs in order to process the xlsx. I've never worked with office sdk, but I get similar errors when I open xlsx in other programs. 99% of the time I can just change the format. (if you live dangerously you can just manual change the file extension in your folder to something itll read.)
I'm using Open XML to open an excel file as in:
using (SpreadsheetDocument myWorkbook = SpreadsheetDocument.Open("generated.xlsx", true))
...
but I cannot figure out how to actually launch excel and show the file through code.
Thanks for any help.
Take a look at either one of these answers:
how to create and download excel document using asp.net
OpenXML file download without temporary file
I am trying to use Interop.Excell to save an Excel Workbook as a PDF file. I am using VS2008 and Office2007, and have downloaded and installed the SaveAsPDFandXPS.exe from Microsoft. This enabled me to save a Word document as a pdf using the following code:
object frmt = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
wrd.ActiveDocument.SaveAs(ref dest, ref frmt, ref unknown, ref unknown,...
Pretty cool excpet for the whole Interop thing.
Anyway, I have been unsucsessful in finding a parallel in Interop.Excell for the Word.WdSaveFormat.wdFormatPDF. The Workbook.SaveAs takes a Interop.Excel.XlFileFormat, but there is no option for a pdf format. Has anyone done this or has experience in this area?
This question has been answered here:
What is the FileType number for PDF in Excel 2007 that is needed to save a file as PDF through the API?
You need to call the Workbook.ExportAsFixedFormat method:
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF
FileName:=“sales.pdf”
Quality:=xlQualityStandard
DisplayFileAfterPublish:=True
This method should be preferred over using SaveAs because it also allows specifying all PDF / XPS options.
Note: This method has been added to the Excel object model with Excel 2007 and requires the Save as PDF or XPS Add-in for 2007 Microsoft Office programs (or SP2) to be installed.
According to Microsoft, the constant to use with Workbook.SaveAs to save as a PDF is 57.
"The pdf format is not listed here. However it has number 57."
From:
http://msdn.microsoft.com/en-us/library/bb241279%28office.12%29.aspx