Saving Excel files to CSV, PDF and other formats

Different Ways to Save Your Files

Aspose.Cells API provides a class named Workbook that represents an Excel file and provides all necessary properties and methods that developers may need to work with their Excel files. The Workbook class provides a save method that is used to save Excel files. The save method has many overloads that are used to save Excel files in different ways.

Developers can also specify the file format in which their files should be saved. The files can be saved in several formats such as XLS, SpreadsheetML, CSV, Tab Delimited, Tab-separated values TSV, XPS and many more. These file formats are specified using the SaveFormat enumeration.

SaveFormat enumeration contains many pre-defined file formats (that can be chosen by you) as follows:

File Format Types Description
AUTO API tries to detect the appropriate format from the file extension specified in the first parameter to the save method
CSV Represents a CSV file
XLSX Represents an Office Open XML SpreadsheetML file
XLSM Represents XML-based XLSM file
XLTX Represents an Excel template file
XLTM Represents an Excel Macro-enabled template file
XLAM Represents an Excel XLAM file
TSV Represents a Tab-separated values file
TAB_DELIMITED Represents a Tab Delimited text file
HTML Represents an HTML file(s)
M_HTML Represents an MHTML file(s)
ODS Represents an OpenDocument Spreadsheet file
EXCEL_97_TO_2003 Represents an XLS file that is the default format for Excel 1997 to 2003 revisions
SPREADSHEET_ML Represents a SpreadSheetML file
XLSB Represents an Excel 2007 binary XLSB file
UNKNOWN Represents unrecognized format, cannot be saved.
PDF Represents a PDF Document
XPS Represents an XML Paper Specification (XPS) file
TIFF Represents a Tagged Image File Format (TIFF) file
SVG Represents an XML-based Scalable Vector Graphics (SVG) file
DIF Represents Data Interchange Format.
NUMBERS Represents a numbers file.
MARKDOWN Represents a markdown document.
Normally, there are two ways to save Excel files as follows:
  1. Saving the file to some location
  2. Saving the file to a stream

Saving File to Some Location

If developers need to save their files to some storage location then they can simply specify the file name (with its complete storage path) and desired file format (using the SaveFormat enumeration) while calling the save method of Workbook object.

Example:

Saving Workbook to Text or CSV Format

Sometimes, you want to convert or save a workbook with multiple worksheets into text format. For text formats (for example TXT, TabDelim, CSV etc.), by default both Microsoft Excel and Aspose.Cells save the contents of the active worksheet only.

The following code example explains how to save an entire workbook into text format. Load the source workbook which could be any Microsoft Excel or OpenOffice spreadsheet file (so XLS, XLSX, XLSM, XLSB, ODS and so on) with any number of worksheets.

When the code is executed, it converts the data of all sheets in the workbook to TXT format.

You can modify the same example to save your file to CSV. By default, TxtSaveOptions.Separator is a comma, so do not specify a separator if saving to CSV format. Please note: If you are using the evaluation version and even if the parameter of method TxtSaveOptions.setExportAllSheets(boolean value) is set to true, the program will still only export one worksheet.

Example:

Saving Text Files with Custom Separator

Text files contain spreadsheet data without formatting. The file is a kind of plain text file that can have some customized delimiters between its data.

Saving File to a Stream

If developers need to save their files to a Stream then they should create a FileOutputStream object and then save the file to that Stream object by calling the save method of Workbook object. Developers can also specify the desired file format (using the SaveFormat enumeration) while calling the save method.

Example:

Saving File to Other Format

XLS Files

XLSX Files

PDF Files

Set ContentCopyForAccessibility option

With the PdfSaveOptions class, you can get or set the PDF AccessibilityExtractContent option to control the content access in the converted PDF. It means it allows screen reader software to utilize the text within the PDF file for reading the PDF file.  You can disable it by applying a change permissions password and deselecting the two items in the screenshot here.

Export Custom properties to PDF

With the PdfSaveOptions class, you can export the custom properties in source workbook to the PDF. PdfCustomPropertiesExport enumerator is provided for specifying the way by which properties are exported. These properties can be observed in Adobe Acrobat Reader by clicking on File and then properties option as shown in the following image. Template file “sourceWithCustProps.xlsx”  can be downloaded here for testing and output PDF file “outSourceWithCustProps” is available here for analysis.

todo:image_alt_text

Convert Excel Workbook to Markdown

The Aspose.Cells API provides support for exporting spreadsheets to Markdown format. To export the active worksheet to Markdown, pass SaveFormat.Markdown as the second parameter of Workbook.Save method. You may also use MarkdownSaveOptions class to specify additional settings for exporting worksheet to Markdown.

The following code example demonstrates exporting active worksheet to Markdown by using SaveFormat.Markdown enumeration member. Please see the output Markdown file generated by the code for reference.

Advance topics