La tua prima applicazione Aspose.Cells Ciao mondo

Come Creare l’Applicazione Ciao Mondo

I passi seguenti creano l’applicazione Hello World utilizzando l’API Aspose.Cells:

  1. Crea un’istanza della classe Workbook.
  2. Se hai una licenza, quindi applicala. Se stai utilizzando la versione di valutazione, salta le righe di codice relative alla licenza.
  3. Crea un nuovo file Excel, oppure apri un file Excel esistente.
  4. Accedi a qualsiasi cella desiderata di un foglio di lavoro nel file di Excel.
  5. Inserisci le parole Hello World! in una cella accessibile.
  6. Genera il file modificato di Microsoft Excel.

L’implementazione dei passaggi sopra è dimostrata negli esempi seguenti.

Come Creare un Nuovo Workbook

Nell’esempio seguente viene creato un nuovo workbook da zero, viene scritto Hello World! nella cella A1 del primo foglio di lavoro e viene salvato il file Excel.

// 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");

Come Aprire un File Esistente

Nell’esempio seguente viene aperto un file di modello Microsoft Excel esistente chiamato “Sample.xlsx”, viene inserito il testo “Hello World!” nella cella A1 del primo foglio di lavoro e viene salvato il 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();