Introduction
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 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, SpreadsheetML, CSV, Tab Delimited text, and Intermediate XML for sub-sequent conversion into PDF using Aspose.Pdf. These file formats are specified using the FileFormatType enumeration.
FileFormatType enumeration contains many pre-defined file formats (that can be chosen by you) as follows:
|
File Format Types
|
Description
|
|
AsposePdf
|
Specifies the spreadsheet in Aspose.Pdf.Xml format that can be read by Aspose.Pdf to produce a PDF file. Only supported in Enterprise Edition
|
|
CSV
|
Represents a CSV file
|
|
Default
|
Represents an Excel 2000 file
|
|
Excel97
|
Represents an Excel 97 file
|
|
Excel2000
|
Represents an Excel 2000 file
|
|
ExcelXP
|
Represents an Excel XP file
|
|
Excel2003
|
Represents an Excel 2003 file
|
|
Excel2007Xlsx
|
Represents an Excel 2007 xlsx file
|
|
Excel2007Xlsm
|
Represents an Excel 2007 xlsm file
|
|
Excel2007Xltx
|
Represents an Excel 2007 template xltx file
|
|
Excel2007Xltm
|
Represents an Excel 2007 macro-enabled xltm file
|
|
SpreadsheetML
|
Represents a SpreadSheetML file
|
|
TabDelimited
|
Represents a Tab Delimited text file
|
|
Html
|
Represents html file(s)
|
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 FileFormatType enumeration) while calling the Save method of Workbook object.
Example:
[C#]
//Save in default Excel2000 format
workbook.Save("C:\\book1.xls");
//Save in Excel2003 format
workbook.Save("C:\\book1.xls", FileFormatType.Excel2003);
//Save in Excel2007 xlsx format
workbook.Save("C:\\book1.xlsx", FileFormatType.Excel2007Xlsx);
//Save in SpreadsheetML format
workbook.Save("C:\\book1.xml", FileformatType.SpreadsheetML);
[VB.NET]
'Save in default Excel2000 format
workbook.Save("C:\book1.xls")
'Save in Excel2003 format
workbook.Save("C:\book1.xls", FileFormatType.Excel2003)
'Save in Excel2007 xlsx format
workbook.Save("C:\book1.xlsx", FileFormatType.Excel2007Xlsx)
'Save in SpreadsheetML format
workbook.Save("C:\book1.xml", FileformatType.SpreadsheetML)
[JAVA]
//Save in default Excel2000 format
workbook.save("C:\\book1.xls");
//Save in Excel2003 format
workbook.save("C:\\book1.xls",FileFormatType.EXCEL2003);
//Save in Excel2007 xlsx format
workbook.save("C:\\book1.xlsx", FileFormatType.EXCEL2007);
//Save in SpreadsheetML format
workbook.save("C:\\book1.xml", FileformatType.SPREADSHEETML);
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 FileFormatType enumeration) while calling the Save method.
Example:
[C#]
...
//Create a Stream object
FileStream stream = new FileStream("C:\\book1.xls",FileMode.Open);
//Save to stream in default Excel2000 format
workbook.Save(stream, FileFormatType.Default);
//Save in Excel2003 format
workbook.Save(stream, FileFormatType.Excel2003);
//Save in Excel2007 xlsx format
workbook.Save("C:\\book1.xlsx", FileFormatType.Excel2007Xlsx);
//Save in SpreadsheetML format
workbook.Save(stream, FileformatType.SpreadsheetML);
[VB.NET]
...
'Create a Stream object
Dim stream As FileStream = New FileStream("C:\\book1.xls",FileMode.Open)
'Save in default Excel2000 format
workbook.Save(stream, FileFormatType.Default)
'Save in Excel2003 format
workbook.Save(stream, FileFormatType.Excel2003)
'Save in Excel2007 xlsx format
workbook.Save("C:\book1.xlsx", FileFormatType.Excel2007Xlsx)
'Save in SpreadsheetML format
workbook.Save(stream, FileformatType.SpreadsheetML)
[JAVA]
//Create a Stream object
FileOutputStream stream=new FileOutputStream("C:\\book1.xls");
//Save to stream in default Excel2000 format
workbook.save(stream, FileFormatType.DEFAULT);
//Save in Excel2003 format
workbook.save(stream, FileFormatType.EXCEL2003);
//Save in SpreadsheetML format
workbook.Save(stream, FileformatType.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:
- File Name, represents the file name
- File Format, represents the file format (can be selected using FileFormatType enumeration)
- Save Type, represents the save type of the generated file
- ASP.NET HttpResponse Object
Save type can be selected by using the SaveType enumeration that 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 extension.
SaveType enumeration contains the following pre-defined save types:
|
Save Types
|
Description
|
|
OpenInExcel
|
Sends the spreadsheet to the browser and opens in an application associated with .xls extension
|
|
OpenInBrowser
|
Sends the document to the browser and presents an option to save the spreadsheet to disk or open inside the browser
|
Example:
[C#]
//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("Report.xls", FileFormatType.Default, SaveType.OpenInExcel, Response);
[VB.NET]
'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("Report.xls", FileFormatType.Default, SaveType.OpenInExcel, Response)