Introduction
In this beginner's topic, we'd show that how can developers create their first favourite application (that is Hello World) using the simplest API of Aspose.Cells. In this simple application, we will create an Excel file having Hello World text in the specified cell of a worksheet.
Steps to Create Hello World Application
Please follow the steps below to create Hello World application by using Aspose.Cells API:
- Create an instance of Workbook class
- If you have purchased a license then embed few license related lines of code to use that license in your application to get the full functionality
- If you are using the evaluation version of the component (i.e., using Aspose.Cells without license), you may just skip the license related coding lines
- Create a new Excel file or Open an existing Excel file in which you want to add/update some text
- Access any desired cell of a worksheet in the Excel file
- Insert Hello World! text into the cell accessed by you
- Finally generate the modified Excel file containing Hello World! text
The implementation of above steps is demonstrated in the examples below.
Example1:
The following example creates a new workbook from the scratch, inputs "Hello World!" text into the A1 cell in the first worksheet and save the excel file.
[C#]
//Create a License object
License license = new License();
//Set the license of Aspose.Cells to avoid the evaluation
//limitations
license.SetLicense("Aspose.Cells.lic");
//Instantiate a Workbook object that represents Excel file.
Workbook wb = new Workbook();
//Note when you create a new workbook, a default worksheet
//"Sheet1" is added (by default) to the workbook.
//Access the first worksheet "Sheet1" in the book.
Worksheet sheet = wb.Worksheets[0];
//Access the "A1" cell in the sheet.
Cell cell = sheet.Cells["A1"];
//Input the "Hello World!" text into the "A1" cell
cell.PutValue("Hello World!");
//Save the Excel file.
wb.Save("d:\\MyBook.xls",FileFormatType.Excel2003);
[VB.NET]
'Create a License object
Dim license As License = New License()
'Set the license of Aspose.Cells to avoid the evaluation
'limitations
license.SetLicense("Aspose.Cells.lic")
'Instantiate a Workbook object that represents Excel file.
Dim wb As Workbook = New Workbook()
'Note when you create a new workbook, a default worksheet
'"Sheet1" is added (by default) to the workbook.
'Access the first worksheet "Sheet1" in the book.
Dim sheet As Worksheet = wb.Worksheets(0)
'Access the "A1" cell in the sheet.
Dim cell As Cell = sheet.Cells("A1")
'Input the "Hello World!" text into the "A1" cell
cell.PutValue("Hello World!")
'Save the Excel file.
wb.Save("d:\MyBook.xls",FileFormatType.Excel2003)
[Java]
//Creating a file input stream to reference the license file
FileInputStream fstream=new FileInputStream("Aspose.Cells.lic");
//Create a License object
License license=new License();
//Set the license of Aspose.Cells to avoid the evaluation
//limitations
license.setLicense(fstream);
//Instantiate a Workbook object that represents Excel file.
Workbook wb = new Workbook();
//Note when you create a new workbook, a default worksheet
//"Sheet1" is added (by default) to the workbook.
//Access the first worksheet "Sheet1" in the book.
Worksheet sheet = wb.getWorksheets().getSheet(0);
//Access the "A1" cell in the sheet.
Cell cell = sheet.getCells().getCell("A1");
//Input the "Hello World!" text into the "A1" cell
cell.setValue("Hello World!");
//Save the Excel file.
wb.save("D:\\MyBook.xls",FileFormatType.EXCEL2003);
Following is the screen-shot after executing the above code:
|
Figure: Generated Spreadsheet
|
Example2:
The following example opens an existing excel template file named "book1.xls", inputs "Hello World!" text into the A1 cell in the first worksheet and does Save As the workbook.
[C#]
//Create a License object
License license = new License();
//Set the license of Aspose.Cells to avoid the evaluation limitations
license.SetLicense("Aspose.Cells.lic");
//Instantiate a Workbook object that represents an Excel file
Workbook workbook = new Workbook();
//Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream("C:\\book1.xls",FileMode.Open);
//Opening the Excel file through the file stream
workbook.Open(fstream);
//Get the reference of "A1" cell from the cells collection of a worksheet
Cell cell = workbook.Worksheets[0].Cells["A1"];
//Put the "Hello World!" text into the "A1" cell
cell.PutValue("Hello World!");
//Save the Excel file
workbook.Save("C:\\HelloWorld.xls", FileFormatType.Default);
//Closing the file stream to free all resources
fstream.Close();
[VB.NET]
'Create a License object
Dim license As License = New License()
'Set the license of Aspose.Cells to avoid the evaluation limitations
license.SetLicense("Aspose.Cells.lic")
'Instantiate a Workbook object that represents an Excel file
Dim workbook As Workbook = New Workbook()
'Creating a file stream containing the Excel file to be opened
Dim fstream As FileStream = New FileStream("C:\book1.xls",FileMode.Open)
'Opening the Excel file through the file stream
workbook.Open(fstream)
'Get the reference of "A1" cell from the cells collection of a worksheet
Dim cell As Cell = workbook.Worksheets(0).Cells("A1")
'Put the "Hello World!" text into the "A1" cell
cell.PutValue("Hello World!")
'Save the Excel file
workbook.Save("C:\HelloWorld.xls", FileFormatType.Default)
'Closing the file stream to free all resources
fstream.Close()
[Java]
//Creating a file input stream to reference the license file
FileInputStream fstream=new FileInputStream("Aspose.Cells.lic");
//Create a License object
License license=new License();
//Set the license of Aspose.Cells to avoid the evaluation limitations
license.setLicense(fstream);
//Instantiate a Workbook object that represents an Excel file
Workbook workbook=new Workbook();
//Reading the Excel file through the file stream
workbook.read("C:\\book1.xls");
//Get the reference of "A1" cell from the cells of a worksheet
Cell cell=workbook.getSheet(0).getCell("A1");
//Set the "Hello World!" value into the "A1" cell
cell.setValue("Hello World!");
//Write the Excel file
workbook.write("C:\\HelloWorld.xls",FileFormatType.DEFAULT);