简体   繁体   中英

Programmatically convert Excel to XPS

I am looking for a way to programmatically convert Excel reports to XPS format. Is this supported anywhere in the Microsoft framework, or should we look for a third party tool?

Yes currently we are programmatically creating Excel reports using ExcelWriter and need to produce XPS reports for a client. So we either go direct to XPS which seems to be a larger learning curve, or convert the Excel report to XPS.

There is an add on for Office 2007 that gives you the ability to export to XPS or PDF. Invoke Excel via Microsoft.Office.interop.Excel and export to XPS.

From my own code (Workbook is an instance but providing full namespace instead):

Microsoft.Office.Interop.Excel.Workbook.ExportAsFixedFormat(
    Excel.XlFixedFormatType.xlTypeXPS,
    pdfpath, Excel.XlFixedFormatQuality.xlQualityStandard,
    true, true,
    fpage, tpage,
    false,
    oMissing
);

There is an MSDN article on how to do this.

You can also print from Excel to XPS. Not technically converting but may be just what you need.

Dim ws As Worksheet
Set ws = ActiveSheet
Call ws.PrintOut(ActivePrinter:="Microsoft XPS Document Writer", _
                PrintToFile:=True, PrToFileName:="S:\Temp\Test3.xps")

I would prefer the method suggested by Colin, but you could also use SaveAs on the workbook with the FileFormat constant of 18 for XPS as described in this related question:

What is the FileType number for PDF in Excel 2007 that is needed to save a file as PDF through the API?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM