Introduction
Developers can make use of Aspose.Cells to open existing files for different purposes. For example, you can open an existing file to retrieve data from it or you can use a pre-defined Designer Spreadsheet file to speed up your development process etc. Aspose.Cells allows developers to open different kinds of source files. These source files can be Excel reports, SpreadsheetML, CSV or Tab Delimited files. In this topic, we will discuss about opening these different kinds of source files using Aspose.Cells.
Simple Ways to Open Excel Files
Opening through Path
Developers can simply open an Excel file through its file path by calling the Open method of the Workbook class. All you have to do, is to pass the file path to the Open method as shown below:
Example:
[C#]
//Creating a Workbook object
Workbook workbook = new Workbook();
//Calling Open method of Workbook object to open an Excel file using its file path
workbook.Open("C:\\book1.xls");
[VB.NET]
'Creating an Workbook object
Dim workbook As Workbook = New Workbook()
'Calling Open method of Workbook object to open an Excel file using its file path
workbook.Open("C:\book1.xls")
[JAVA]
//Creating an Workbook object
Workbook workbook = new Workbook();
//Calling Open method of Workbook object to open an Excel file using its file path
workbook.open("C:\\book1.xls");
Opening through Stream
Sometimes, the Excel file (that we need to open) is stored as a stream. In such cases, developers can use an overloaded version of the Open method that takes the Stream object (containing the Excel file) to open the file.
Example:
[C#]
//Creating a Workbook object
Workbook workbook = new Workbook();
//Create a Stream object
FileStream fstream = new FileStream("C:\\book1.xls",FileMode.Open);
//Calling Open method of Workbook object to open the file from a Stream object
//that contains the content of file and it should support seeking
workbook.Open(stream);
[VB.NET]
'Creating an Workbook object
Dim workbook As Workbook = New Workbook()
'Create a Stream object
Dim fstream As FileStream = New FileStream("C:\book1.xls",FileMode.Open)
'Calling Open method of Workbook object to open the file from a Stream object
'that contains the content of file and it should support seeking
workbook.Open(stream)
[JAVA]
//Creating an Workbook object
Workbook workbook = new Workbook();
//Create a Stream object
FileInputStream fstream = new FileInputStream("C:\\book1.xls");
//Calling Open method of Workbook object to open the file from a Stream object
//that contains the content of file and it should support seeking
workbook.open(fstream);
Opening Files of Different Microsoft Excel Versions
It's very common to believe that the Excel files (to be opened using Aspose.Cells) might be created using different versions of Microsoft Excel such as Microsoft Excel 97, 2000, XP, 2003 & 2007. In such cases, developers can use another overloaded Open method that not only takes the file (through a file path or as a stream) but also allows developers to specify the format of the Excel file 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 20003 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
|
Opening Microsoft Excel 97 Files
To open Microsoft Excel 97 files, developers should call Open method and select the Excel97 (pre-defined) value of FileFormatType enumeration.
Example:
[C#]
//Creating an Workbook object
Workbook workbook = new Workbook();
//Calling Open method of Workbook object to open Excel 97 file
workbook.Open("C:\\book1.xls", FileFormatType.Excel97);
[VB.NET]
'Creating an Workbook object
Dim workbook As Workbook = New Workbook()
'Calling Open method of Workbook object to open Excel 97 file
workbook.Open("C:\book1.xls", FileFormatType.Excel97)
[JAVA]
//Creating an Workbook object
Workbook workbook = new Workbook();
//Calling open method of Workbook object to open Excel 97 file
workbook.open("C:\\book1.xls", FileFormatType.EXCEL97);
Opening Microsoft Excel 2000 Files
To open Microsoft Excel 2000 files, developers should call Open method and select the Excel2000 (pre-defined) value of FileFormatType enumeration.
Example:
[C#]
//Creating a Workbook object
Workbook workbook = new Workbook();
//Calling Open method of Workbook object to open Excel 2000 file
workbook.Open("C:\\book1.xls", FileFormatType.Excel2000);
[VB.NET]
'Creating a Workbook object
Dim workbook As Workbook = New Workbook()
'Calling Open method of Workbook object to open Excel 2000 file
workbook.Open("C:\book1.xls", FileFormatType.Excel2000)
[JAVA]
//Creating a Workbook object
Workbook workbook = new Workbook();
//Calling open method of Workbook object to open Excel 2000 file
workbook.open("C:\\book1.xls", FileFormatType.EXCEL2000);
Opening Microsoft Excel 2003 Files
To open Microsoft Excel 2003 files, developers should call Open method and select the Excel2003 (pre-defined) value of FileFormatType enumeration.
Example:
[C#]
//Creating a Workbook object
Workbook workbook = new Workbook();
//Calling Open method of Workbook object to open Excel 2003 file
workbook.Open("C:\\book1.xls", FileFormatType.Excel2003);
[VB.NET]
'Creating a Workbook object
Dim workbook As Workbook = New Workbook()
'Calling Open method of Workbook object to open Excel 2003 file
workbook.Open("C:\book1.xls", FileFormatType.Excel2003)
[JAVA]
//Creating a Workbook object
Workbook workbook = new Workbook();
//Calling open method of Workbook object to open Excel 2003 file
workbook.open("C:\\book1.xls", FileFormatType.EXCEL2003);
Opening Microsoft Excel XP Files
To open Microsoft Excel XP files, developers should call Open method and select the ExcelXP (pre-defined) value of FileFormatType enumeration.
Example:
[C#]
//Creating a Workbook object
Workbook workbook = new Workbook();
//Calling Open method of Workbook object to open Excel XP file
workbook.Open("C:\\book1.xls", FileFormatType.ExcelXP);
[VB.NET]
'Creating a Workbook object
Dim workbook As Workbook = New Workbook()
'Calling Open method of Workbook object to open Excel XP file
workbook.Open("C:\book1.xls", FileFormatType.ExcelXP)
[JAVA]
//Creating a Workbook object
Workbook workbook = new Workbook();
//Calling open method of Workbook object to open Excel XP file
workbook.open("C:\\book1.xls", FileFormatType.EXCELXP);
Opening Microsoft Excel 2007 Xlsx Files
To open Microsoft Excel 2007 xlsx files, developers should call Open method and select the Excel2007Xlsx (pre-defined) value of FileFormatType enumeration.
Example:
[C#]
//Creating a Workbook object
Workbook workbook = new Workbook();
//Calling Open method of Workbook object to open Excel 2007 xlsx file
workbook.Open("C:\\book1.xlsx", FileFormatType.Excel2007Xlsx);
[VB.NET]
'Creating a Workbook object
Dim workbook As Workbook = New Workbook()
'Calling Open method of Workbook object to open Excel 2007 xlsx file
workbook.Open("C:\book1.xlsx", FileFormatType.Excel2007Xlsx)
[JAVA]
//Creating a Workbook object
Workbook workbook = new Workbook();
//Calling open method of Workbook object to open Excel 2007 xlsx file
workbook.open("C:\\book1.xls", FileFormatType.EXCEL2007);
Opening Files with Different Formats