Skip to end of metadata
Go to start of metadata

Aspose.Cells allows developers to create Excel files from scratch using its flexible API. Once you create Excel files, you would also need to save your workbook (file). Aspose.Cells provides a variety of ways to save these files. In this topic, we will discuss all those possible ways that can be adopted by developers to save their files.

Contents Summary
 

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

EXCEL_97_TO_2003

Represents an XLS file that is the default format for Excel 1997 to 2003 revisions

XLSB

Represents an Excel 2007 binary XLSB file

XLAM

Represents an Excel XLAM file

XLSM

Represents XML-based XLSM file

XLSX

Represents an Office Open XML SpreadsheetML file

XLTM

Represents an Excel Macro-enabled template file

XLTX

Represents an Excel template file

SPREADSHEET_ML

Represents a SpreadSheetML file

TSVRepresents 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

PDF

Represents a PDF Document

SVG

Represents an XML-based Scalable Vector Graphics (SVG) file

TIFF

Represents a Tagged Image File Format (TIFF) file

XPS

Represents an XML Paper Specification (XPS) file

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.

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.

Labels
  • No labels