Converting Excel to PDF with VS2008 and Office2007 - excel

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

Related

How to Programmatically Create OOXML Strict Documents

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.

How to save .xls file using OpenXML in dot net core?

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

NodeJs construct Excel file and export in PDF

As the title said, I'm searching for a NodeJS library to construct Excel (xlsx), with cell format (color, font size, images...). Importantly, it must has the capability of exporting the resulted .xlsx file to .pdf format.
I know some libs that call Excel API but I'm running a linux server and that's impossible for me.
Thanks to jcaron comment I finally found out that I can build a PDF file directly without passing through xls.
I used pdfmaker for nodejs that support creating PDF file pretty well.

Office Invalid XML error, file still opens in Office

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.)

Convert docx to xlsx

I am writting .NET application that generates reports in docx. One of the last requirements I've got was - generate also these reports in xlsx format. So,
is there any simple way to convert docx to xlsx format? I haven't found any solution or utility/library. One of the ideas was to use Microsoft.Office.Interop Copy/Paste methods, but I don't know if it helps :)
We have used a MS tool that allows you to work with Office documents as if they were xml:
Open XML SDK 2.0 for Microsoft Office
http://www.microsoft.com/downloads/details.aspx?FamilyId=C6E744E5-36E9-45F5-8D8C-331DF206E0D0&displaylang=en
This had the following benefits compared with interop:
No need in install office
No problems with memory due to Excel not closing
Better performance, in our case it went from 40 seconds to 2 (two)

Resources