La tua prima applicazione Aspose.Cells Ciao mondo
Creazione dell’applicazione Hello World
Per creare l’applicazione Hello World utilizzando l’API di Aspose.Cells:
- Creare un’istanza della classe Workbook.
- Applicare la licenza:
- Se hai acquistato una licenza, utilizza la licenza nella tua applicazione per accedere a tutte le funzionalità complete di Aspose.Cells
- Se stai utilizzando la versione di valutazione del componente (se stai usando Aspose.Cells senza licenza), salta questo passaggio.
- Creare un nuovo file Microsoft Excel o aprire un file esistente in cui desideri aggiungere/aggiornare del testo.
- Accedere a qualsiasi cella di un foglio di lavoro nel file Microsoft Excel.
- Inserisci le parole Hello World! in una cella accessibile.
- Genera il file modificato di Microsoft Excel.
Gli esempi seguenti dimostrano i passaggi sopra indicati.
Creazione di un’organizzazione
Nell’esempio seguente viene creata una nuova organizzazione da zero, vengono scritte le parole “Ciao mondo!” nella cella A1 del primo foglio di lavoro e il file viene salvato.
Il foglio di calcolo generato
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(CreatingWorkbook.class); | |
// 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(); | |
// Applying the Aspose.Cells license | |
license.setLicense(fstream); | |
// Instantiating a Workbook object that represents a Microsoft Excel | |
// file. | |
Workbook wb = new Workbook(); | |
// Note when you create a new workbook, a default worksheet, "Sheet1", is by default added to the workbook. Accessing the | |
// first worksheet in the book ("Sheet1"). | |
Worksheet sheet = wb.getWorksheets().get(0); | |
// Access cell "A1" in the sheet. | |
Cell cell = sheet.getCells().get("A1"); | |
// Input the "Hello World!" text into the "A1" cell | |
cell.setValue("Hello World!"); | |
// Save the Microsoft Excel file. | |
wb.save(dataDir + "MyBook.xls", FileFormatType.EXCEL_97_TO_2003); | |
wb.save(dataDir + "MyBook.xlsx"); | |
wb.save(dataDir + "MyBook.ods"); |
Apertura di un file esistente
Nell’esempio seguente viene aperto un file modello Microsoft Excel esistente chiamato book1.xls, si scrive la frase “Ciao Mondo!” nella cella A1 nel primo foglio di lavoro, e si salva il foglio di lavoro come un nuovo file.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(OpeningExistingFile.class); | |
// 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(dataDir + "book1.xls"); | |
// Get the reference of "A1" cell from the cells of a worksheet | |
Cell cell = workbook.getWorksheets().get(0).getCells().get("A1"); | |
// Set the "Hello World!" value into the "A1" cell | |
cell.setValue("Hello World!"); | |
// Write the Excel file | |
workbook.save(dataDir + "HelloWorld.xls", FileFormatType.EXCEL_97_TO_2003); | |
workbook.save(dataDir + "HelloWorld.xlsx"); | |
workbook.save(dataDir + "HelloWorld.ods"); |