| 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:
//Creating an Workbook object with an Excel file path Workbook workbook = new Workbook("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:
//Create a Stream object FileInputStream fstream = new FileInputStream("C:\\book1.xls"); //Creating an Workbook object with the stream object Workbook workbook = new Workbook(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. |
| CSV | Represents a CSV file |
| Default | Represents an Excel 2003 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 |
| Excel2007Xlsb | Represents an Excel 2007 binary xlsb file |
| SpreadsheetML | Represents a SpreadSheetML file |
| TabDelimited | Represents a Tab Delimited text file |
| HTML | Represents html file(s) |
| ODS | Represents an OpenDocument Spreadsheet 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:
//Createing and EXCEL_97_TO_2003 LoadOptions object LoadOptions loadOptions = new LoadOptions(FileFormatType.EXCEL_97_TO_2003); //Creating an Workbook object with excel 97 file path and the loadOptions object Workbook workbook = new Workbook("C:\\book1.xls", loadOptions);
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:
//Createing and EXCEL_97_TO_2003 LoadOptions object LoadOptions loadOptions = new LoadOptions(FileFormatType.EXCEL_97_TO_2003); //Creating an Workbook object with excel 2000 file path and the loadOptions object Workbook workbook = new Workbook("C:\\book1.xls", loadOptions);
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:
//Createing and EXCEL_97_TO_2003 LoadOptions object LoadOptions loadOptions = new LoadOptions(FileFormatType.EXCEL_97_TO_2003); //Creating an Workbook object with excel 2003 file path and the loadOptions object Workbook workbook = new Workbook("C:\\book1.xls", loadOptions);
Opening Microsoft Excel XP Files
To open Microsoft Excel XP files, developers should call open method and select the EXCELlXP (pre-defined) value of FileFormatType enumeration.
Example:
//Createing and EXCEL_97_TO_2003 LoadOptions object LoadOptions loadOptions = new LoadOptions(FileFormatType.EXCEL_97_TO_2003); //Creating an Workbook object with XP file path and the loadOptions object Workbook workbook = new Workbook("C:\\book1.xls", loadOptions);
Opening Microsoft Excel 2007 Xlsx Files
To open Microsoft Excel 2007 xlsx files, developers should call open method and select the EXCEL2007 (pre-defined) value of FileFormatType enumeration.
Example:
//Createing and XLSX LoadOptions object LoadOptions loadOptions = new LoadOptions(FileFormatType.XLSX); //Creating an Workbook object with 2007 xlsx file path and the loadOptions object Workbook workbook = new Workbook("C:\\book1.xlsx", loadOptions);
Opening Files with Different Formats
Aspose.Cells allows developers to open spreadsheet files with different formats such as SpreadsheetML, CSV, Tab Delimited files. To open such files, developers can use the same methodology as they use for opening files of different Microsoft Excel versions.
Opening SpreadsheetML Files
SpreadsheetML files are the XML representations of your spreadsheets including all information about the spreadsheet such as formatting, formulae etc. Since Microsoft Excel XP, an XML export option is added to Microsoft Excel that exports your spreadsheets to SpreadsheetML files.
To open SpreadsheetML files, developers should call open method and select the SPREADSHEETML (pre-defined) value of FileFormatType enumeration.
Example:
//Createing and EXCEL_2003_XML LoadOptions object LoadOptions loadOptions = new LoadOptions(FileFormatType.EXCEL_2003_XML); //Creating an Workbook object with SpreadsheetML file path and the loadOptions object Workbook workbook = new Workbook("C:\\book1.xml", loadOptions);
Opening CSV Files
Comma Separated Values (CSV) files contain records whose values are delimited or separated by commas. In CSV files, data is stored in tablular format that has fields separated by the comma character and quoted by the double quote character. If a field's value contains a double quote character it is escaped with a pair of double quote characters. You can also use Microsoft Excel to export your spreadsheet data to a CSV file.
To open CSV files, developers should call o pe n method and select the CSV (pre-defined) value of FileFormatType enumeration.
Example:
//Createing and CSV LoadOptions object LoadOptions loadOptions = new LoadOptions(FileFormatType.CSV); //Creating an Workbook object with CSV file path and the loadOptions object Workbook workbook = new Workbook("C:\\book1.csv", loadOptions);
Opening Tab Delimited Files
Tab Delimited files are also used to contain spreadsheet data but without any formatting. Data is arranged in rows and columns such as tables and spreadsheets. Shortly, a tab delimited file is a special kind of plain text file with a tab between each column in the text.
To open tab delimited files, developers should call Open method and select the TAB_DELIMITED (pre-defined) value of FileFormatType enumeration.
Example:
//Createing and TAB_DELIMITED LoadOptions object LoadOptions loadOptions = new LoadOptions(FileFormatType.TAB_DELIMITED); //Creating an Workbook object with Tab Delimited text file path and the loadOptions object Workbook workbook = new Workbook("C:\\book1.txt", loadOptions);
Opening Encrypted Excel Files
We know that it's possible to create encrypted Excel files using Microsoft Excel. To open such encrypted files, developers should call a special overloaded Open method and select the DEFAULT (pre-defined) value of FileFormatType enumeration. This method would also take the password for the encrypted file a shown below in the example.
Example:
//Createing and EXCEL_97_TO_2003 LoadOptions object LoadOptions loadOptions = new LoadOptions(FileFormatType.EXCEL_97_TO_2003); //Setting the password for the encrypted Excel file loadOptions.setPassword("password1"); //Creating an Workbook object with file path and the loadOptions object Workbook workbook = new Workbook("C:\\book1.txt", loadOptions);
Important to Know
Aspose.Cells supports Excel file formats from Excel 97 to Excel 2007. But, if you save your Excel file in the following format as shown below in the figure then your Excel file will fail to open using Aspose.Cells.

Figure: Don't save your Excel files in this format
It is because that in this case, Excel file will contain data in Excel 5.0/95 format, which is not supported by Aspose.Cells. So, please save your Excel file as Microsoft Excel Workbook as shown below:

Figure: Save your Excel files in this format
