| Aspose.Cells allows developers to create Excel files from scratch using its flexible API, you may also manipulate existing files. Once you create Excel files, you would also need to save your work (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. |
Different Ways to Save Your Files
Aspose.Cells API provides a class named Workbook. Workbook class represents an Excel file and provides all necessary properties and methods that developers may need to work with their Excel files. Workbook class provides Save method that is used to save Excel files. 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, XLSX, XLSM, SpreadsheetML, CSV, Tab Delimited text, ODS, PDF and HTML. These file formats are specified using the SaveFormat enumeration.
Developers can specify the file format type attribute of Workbook class that denotes the format of the Excel file using the SaveFormat enumeration.
SaveFormat enumeration contains many pre-defined file formats (that can be chosen by you accordingly) as follows:
| File Format Types | Description |
|---|---|
| CSV | Represents a CSV file |
| Excel97To2003 | Represents an Excel 97 - 2003 file |
| Xlsx | Represents an Excel 2007 xlsx file |
| Xlsm | Represents an Excel 2007 xlsm file |
| Xltx | Represents an Excel 2007 template xltx file |
| Xltm | Represents an Excel 2007 macro-enabled xltm file |
| Xlsb | Represents an Excel 2007 binary xlsb file |
| SpreadsheetML | Represents a Spreadsheet XML file |
| TabDelimited | Represents a Tab Delimited text file |
| ODS | Represents an ODS file |
| Html | Represents html file(s) |
| Represents a pdf file | |
| XPS | Represents an XPS document |
| TIFF | Represents Tagged Image File Format (TIFF) |
Normally, there are three ways to save Excel files as follows:
- Saving file to some location
- Saving file to a stream
- Saving file to ASP.NET HttpResponse object
1. 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:
//Save in Excel 97 – 2003 format workbook.Save("C:\\book1.xls"); //OR workbook.Save("C:\\book1.xls", new XlsSaveOptions(SaveFormat.Excel97To2003)); //Save in Excel2007 xlsx format workbook.Save("C:\\book1.xlsx", SaveFormat.Xlsx); //Save in Excel2007 xlsb format workbook.Save("C:\\book1.xlsb", SaveFormat.Xlsb); //Save in ODS format workbook.Save("C:\\book1.ods", SaveFormat.ODS); //Save in Pdf format workbook.Save("C:\\book1.pdf", SaveFormat.Pdf); //Save in Html format workbook.Save("C:\\book1.html", SaveFormat.Html); //Save in SpreadsheetML format workbook.Save("C:\\book1.xml", SaveFormat.SpreadsheetML);
'Save in Excel 97 – 2003 format workbook.Save("C:\book1.xls") 'OR workbook.Save("C:\book1.xls", New XlsSaveOptions(SaveFormat.Excel97To2003)) 'Save in Excel2007 xlsx format workbook.Save("C:\book1.xlsx", SaveFormat.Xlsx) 'Save in Excel2007 xlsb format workbook.Save("C:\book1.xlsb", SaveFormat.Xlsb) 'Save in ODS format workbook.Save("C:\book1.ods", SaveFormat.ODS) 'Save in Pdf format workbook.Save("C:\book1.pdf", SaveFormat.Pdf) 'Save in Html format workbook.Save("C:\book1.html", SaveFormat.Html) 'Save in SpreadsheetML format workbook.Save("C:\book1.xml", SaveFormat.SpreadsheetML)
Saving Text Files with Custom Separator
Text files are used to contain spreadsheet data but without any formatting. The file is a kind of plain text file that can have some customized delimiters between its data.
Example:
//Instantiate Text File's Save Options TxtSaveOptions options = new TxtSaveOptions(); //Specify the separator options.Separator = Convert.ToChar(";"); //Save the file with the options workbook.Save("e:\\test\\abc.txt", options);
'Instantiate Text File's Save Options Dim options As New TxtSaveOptions() 'Specify the separator options.Separator = Convert.ToChar(";") 'Save the file with the options workbook.Save("e:\test\abc.txt", options)
2. Saving File to a Stream
If developers need to save their files to a Stream then they should create a MemoryStream or FileStream 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:
//Save to stream of Excel 97 – 2003 format workbook.Save(stream); //OR workbook.Save(stream, new XlsSaveOptions(SaveFormat.Excel97To2003)); //Save to stream of Excel2007 xlsx format workbook.Save(stream, SaveFormat.Xlsx); //Save to stream of Excel2007 xlsb format workbook.Save(stream, SaveFormat.Xlsb); //Save to stream of ODS format workbook.Save(stream, SaveFormat.ODS); //Save to stream of Pdf format workbook.Save(stream, SaveFormat.Pdf); //Save to stream of Html format workbook.Save(stream, SaveFormat.Html); //Save to stream of SpreadsheetML format workbook.Save(stream, SaveFormat.SpreadsheetML);
'Save to stream of Excel 97 – 2003 format workbook.Save(stream) 'OR workbook.Save(stream, New XlsSaveOptions(SaveFormat.Excel97To2003)) 'Save to stream of Excel2007 xlsx format workbook.Save(stream, SaveFormat.Xlsx) 'Save to stream of Excel2007 xlsb format workbook.Save(stream, SaveFormat.Xlsb) 'Save to stream of ODS format workbook.Save(stream, SaveFormat.ODS) 'Save to stream of Pdf format workbook.Save(stream, SaveFormat.Pdf) 'Save to stream of Html format workbook.Save(stream, SaveFormat.Html) 'Save to stream of SpreadsheetML format workbook.Save(stream, SaveFormat.SpreadsheetML)
3. Saving file to Response Object
Sometimes, it's required by the developers to generate file dynamically and send the generated file directly to the client browser. In order to do so, developers can use a special overloaded version of Save method that accepts four parameters:
- ASP.NET HttpResponse Object
- File Name, represents the file name
- ContentDisposition, represents the content disposition type of the generated file
- SaveOptions, represents the file format type
The ContentDisposition enumeration determines whether the file (being sent to the browser) will provide an option to open by itself directly in the browser or in an application associated with .xls/.xlsx or other extension.
The enumeration contains the following pre-defined save types:
| Type | Description |
|---|---|
| Attachement | Sends the spreadsheet to the browser and opens in an application as an attachment associated with .xls/.xlsx or other extension |
| Inline | Sends the document to the browser and presents an option to save the spreadsheet to disk or open inside the browser |
i) XLS File(s)
Example:
//Creating an Workbook object Workbook workbook = new Workbook(); //... do something here //Save in default format and send the file to user so that he may open the file in //some application or save it to some location workbook.Save(this.Response, "Report.xls", ContentDisposition.Inline, new XlsSaveOptions());
'Creating an Workbook object Dim workbook As Workbook = New Workbook() '... do something here 'Save in default format and send the file to user so that he may open the file in 'some application or save it to some location workbook.Save(Me.Response, "Report.xls", ContentDisposition.Inline, New XlsSaveOptions())
ii) XLSX File(s)
Example:
//Creating an Workbook object Workbook workbook = new Workbook(FileFormatType.Xlsx); //... Your code goes here //Save in xlsx format and send the file to user so that he may open the file in //some application or save it to some location workbook.Save(this.Response, "Report.xlsx", ContentDisposition.Attachment, new OoxmlSaveOptions()); Response.End();
'Creating an Workbook object
Dim workbook As Workbook = New Workbook(FileFormatType.Xlsx)
'... Your code goes here
'Save in xlsx format and send the file to user so that he may open the file in
'some application or save it to some location
workbook.Save(Me.Response, "Report.xlsx", ContentDisposition.Attachment, New OoxmlSaveOptions())
Response.End()
iii) PDF File(s)
Example:
//Creating an Workbook object Workbook workbook = new Workbook(); //... Your code goes here //Save in pdf format and send the file to the client browser, he may open the file in //some application or save it to some location workbook.Save(this.Response, "Report.pdf", ContentDisposition.Attachment, new PdfSaveOptions()); Response.End();
'Creating an Workbook object
Dim workbook As Workbook = New Workbook()
'... Your code goes here
'Save in pdf format and send the file to the client browser, he may open the file in
'some application or save it to some location
workbook.Save(Me.Response, "Report.pdf", ContentDisposition.Attachment, New PdfSaveOptions())
Response.End()
