Convert Excel (XLS, XLSX, XLSM, XLSB) To a PDF or XPS File

If you have trouble running these examples please see Using the Command Line and Helpful Hints For Creating a Command Line.

NOTE: To be able to convert to PDF or XPS, you will have to install a licensed version of MS Office 2007 (or greater) and the “Microsoft Save as PDF or XPS” add in. Go to Microsoft’s web site and search for “Microsoft Save as PDF or XPS add in” if you haven’t done so already.

Although the discussion below refers to PDF, everything is similarly applicable to the XPS file type.

There is a great amount of flexibility when converting files to PDF. You can specify:

·      Sheets to convert

·      Range within the sheet(s) to convert

·      Single/multiple file output

The items above are handled with the /N{sheets^range} and /U switches. Further PDF specific options are available:

·      Optimization: Use ‘Optimize for Web’ to create a smaller file.

·      Page Range: You can specify a page range using the From and To page settings. If From is left blank, it will start from the beginning. If the To is left blank it will go to the last page.

·      Ignore Print Areas: Enable this to ignore print areas.

·      Include Document Properties: Document properties can be included in the PDF, XPS file if you enable this option.

The items above are specified using the /E"{^^^^PDFOptimization^PDFFromPage^PDFToPage^PDFIgnorePrintAreas^PDFIncludeDocProperties}" switch.

The first example shows how to convert a whole workbook. Next we’ll show how to convert a specific worksheets and ranges. Finally we’ll show how to specify PDF attributes.

The examples that follow are shown using XLS files for the input file. XLSX, XLSM, and XLSB files are handled identically, simply replace the name of the input file. In fact any file MS Excel can open is valid. To convert to XPS instead of PDF in the examples below simply change the /C -1 to /C-2. Lastly, add the /U switch to any of the examples below to create multiple PDF files (one for each sheet).

Each switch shown in blue is described in detail below the examples. It is highly recommended you review each switches description before implementing the example, thereby giving you the full power of ‘Convert XLS’.

Example 1 (Whole Workbook Conversion)

Below we show how to convert the whole workbook (all sheets, and all used ranges) to a single PDF file:

ConvertXLS /S"C:\in\Y.XLS" /T"C:\out\Y.PDF" /C-1 /V

To do a whole folder is quite simple:

ConvertXLS /S"C:\in\*.XLS" /T"C:\out\*.PDF" /C-1 /V

 

Example 2 (Sheet and Range Specific Conversions)

To convert a single sheet named “Apples” within C:\in\Y.XLS to a PDF file, the following syntax would be used:

ConvertXLS /S"C:\in\Y.XLS" /F-4143 /N"Apples " /T"C:\out\Y.PDF" /C-1 /V

Specifying ranges can be very powerful. For example if you want to only extract column N you can use “N:N” for the range. To specify a specific range, say A1 to B5 simply change the /N switch slightly to:

ConvertXLS /S"C:\in\Y.XLS" /F-4143 /N"Apples^A1:B5" /T"C:\out\Y.PDF" /C-1 /V

To specify the 2nd, 3rd, 4th and 6th sheet in a workbook and use a specific range, say A1 to B5 and D5 to F6 simply change the /N switch slightly to:

ConvertXLS /S"C:\in\Y.XLS" /F-4143 /N"2-4,6^A1:B5,D5:F6" /T"C:\out\Y.PDF" /C-1 /V

To convert a sheets named “Apples”,”Oranges”,”Peaches” within C:\in\Y.XLS to a PDF file, the following syntax would be used:

ConvertXLS /S"C:\in\Y.XLS" /F-4143 /N"Apples, Oranges,Peaches" /T"C:\out\Y.PDF" /C-1 /V

Example 3 (Specify Specific PDF Attributes)

Here we use the /E switch to specify PDF attributes (Optimization, From Page, To Page, Ignore Print Areas, and Include Document Properties). The syntax for the /E switch is shown below:

/E"^^^^PDFOptimization^PDFFromPage^PDFToPage^PDFIgnorePrintAreas^PDFIncludeDocProperties}"

This is a carrot “^” separated string. The first few items in this string are omitted above for clarity, as they are not useful in the case of converting to PDF. Omitting the /E switch from the command line will set the PDF Optimization off (not minimized file size), from and to pages will be from the beginning and to the end of the document, Print Areas will be ignored and Document Properties will not be included.

To convert a whole workbook, yet specify to optimize the PDF for web use (minimize file size), only convert pages 2-5, ignore print areas and include document properties the syntax would be:

ConvertXLS /S"C:\in\Y.XLS" /T"C:\out\Y.PDF" /C-1 /V /E"^^^^1^1^5^TRUE^TRUE"

 

·      The /S and /T switches above specify Source (input) and Target (output) path respectively and are both required when converting a single file. It is always a good idea to use double quotes around the path especially if there are space characters within the path.

·      The /C-1 switch specifies that the target (output) file to be a PDF. Use -2 for XPS. See Excel Conversion File Type Constants for all possible values for /C and /F.

·      /H If converting from XLS to something else and using the ‘Convert XLS’ conversion method (/M2) you can optionally specify to include values along with formula, much like "MS Excel" conversion method does.

·      /U If converting an XLS file with multiple sheets specified, the /U switch will generate multiple files instead of a single file.

·      The /V (for Verbose) switch is used to give instant feedback by having the program report the status of the conversion with a message box. You can remove this once you have perfected your command line specification. You can also (or instead of /V) create a Log file that will contain the results of the conversion by using the /L switch.

·      /N"{Sheet^Range^DelimChar^FixedWidth^X}" Is used when converting to/from an Excel, CSV or fixed width text file. It specifies which Sheets to convert, what Range to convert and what delimitation/separation character to use, and the method to import export to a fixed width text file. The syntax for this parameter is critical, the items are separated by a carrot character "^". If you do not include Sheet, Range or DelimChar by leaving them blank they will default to all sheets, all used ranges and the comma delimitation character. Be certain to include the double quotation character around this parameter like: /N"Apples,Oranges^A2:B20^9"

Sheet: Only used when an Excel file is the input file. Use "*" or "" for all sheets. You can use names of sheets, or the numeric index. If using numeric indices, you can specify ranges of sheets (i.e. "1,4,10-20" and "4-10,9" etc.). Range and DelimChar are optional.

Range: You could optionally specify a range, say if you only wanted to convert a portion of the input file. Use the same syntax as an Excel range (e.g. A1:B10).

DelimChar: Not useful in these examples. It is for specifying the ASCII value of the delimitation character used in a CSV file. 44 for comma, 124 for Bar “|”, 9 for the tab etc.

FixedWidth: Not useful in these examples. Here you can specify how to import/export a fixed width text file. Not used in this example.

·      /E"{UseDoubleQuotes^ UseCustomXML^ RootNode^ RowNode^ PDFOptimization^ PDFFromPage^ PDFToPage^ PDFIgnorePrintAreas^ PDFIncludeDocProperties}" This parameter is used for additional conversion options and compliments /N. Several things are handled in this parameter. The syntax for this parameter is critical, the items are separated by a carrot character "^" and you may not omit any carrots. Be certain to include the double quotation character around this parameter.

UseDoubleQuotes: 0=Never, 1=Sometimes, 2=Always

UseCustomXML: TRUE to enforce custom XML attributes for the Row and Root nodes, else use FALSE

RootNode: Name of the root node. Used if bUseCustomXML is TRUE

RowNode: Name of the row node. Used if bUseCustomXML is TRUE

PDFOptimization: 0 standard, 1 = Minimize for web usage.

PDFFromPage: Page number to start the PDF or XPS output file. Leave blank or set to 0 to start at the beginning.

PDFToPage: Page to end the PDF or XPS output file. Leave blank or set to 0 to go to the end.

IgnorePrintAreas: TRUE to ignore print areas, else FALSE

PDFIncludeDocProperties: TRUE to include document properties, else FALSE

A typical string might look like the following:

/ E “0^TRUE^MyRoot^myRow^0^0^FALSE^FALSE^

Omitting the /E switch will set the PDF Optimization off (not minimized file size), from and to pages will be from the beginning and to the end, Print Areas will be ignored and Document Properties will not be included.

Note: It is highly encouraged that you use the Verbose (/V) switch initially to see what the status of your conversion is and to help you perfect your command line. When in verbose mode the program will tell you what went wrong or right with your command line using message boxes.