Your First Aspose.Cells Application - Hello World

How to Create the Hello World Application

The steps below creates the Hello World application using the Aspose.Cells API:

  1. Create an instance of the Workbook class.
  2. If you have a license, then apply it. If you are using the evaluation version, skip the license related code lines.
  3. Create a new Excel file, or open an existing Excel file.
  4. Access any desired cell of a worksheet in the Excel file.
  5. Insert the words Hello World! into a cell accessed.
  6. Generate the modified Microsoft Excel file.

The implementation of the above steps is demonstrated in the examples below.

How to Create a New Workbook

The following example creates a new workbook from the scratch, writes Hello World! into cell A1 on the first worksheet and saves the Excel file.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
try
{
// Create a License object
License license = new License();
// Set the license of Aspose.Cells to avoid the evaluation limitations
license.SetLicense(dataDir + "Aspose.Cells.lic");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
// Instantiate a Workbook object that represents Excel file.
Workbook wb = new Workbook();
// When you create a new workbook, a default "Sheet1" is added to the workbook.
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(dataDir + "MyBook_out.xlsx");

How to Open an Existing File

The following example opens an existing Microsoft Excel template file named “Sample.xlsx”, inputs “Hello World!” text into the A1 cell in the first worksheet and saves the workbook.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
try
{
// Create a License object
License license = new License();
// Set the license of Aspose.Cells to avoid the evaluation limitations
license.SetLicense("Aspose.Cells.lic");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "Sample.xlsx", FileMode.Open);
// Instantiate a Workbook object that represents the existing Excel file
Workbook workbook = new Workbook(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(dataDir + "HelloWorld_out.xlsx");
// Closing the file stream to free all resources
fstream.Close();