Votre première application Aspose.Cells Hello World
Comment créer l’application Bonjour le monde
Les étapes ci-dessous créent l’application Bonjour le monde en utilisant l’API Aspose.Cells :
- Créez une instance de la classe Workbook.
- Si vous avez une licence, alors appliquez-la. Si vous utilisez la version d’évaluation, ignorez les lignes de code relatives à la licence.
- Créez un nouveau fichier Excel, ouvrez un fichier Excel existant.
- Accédez à n’importe quelle cellule souhaitée d’une feuille de calcul dans le fichier Excel.
- Insérez les mots Bonjour le Monde ! dans une cellule accessible.
- Générez le fichier Microsoft Excel modifié.
La mise en œuvre des étapes ci-dessus est démontrée dans les exemples ci-dessous.
Comment créer un nouveau classeur
L’exemple suivant crée un nouveau classeur à partir de zéro, écrit Bonjour le monde! dans la cellule A1 de la première feuille de calcul et enregistre le fichier 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"); |
Comment ouvrir un fichier existant
L’exemple suivant ouvre un fichier modèle Microsoft Excel existant nommé “Sample.xlsx”, saisit le texte “Bonjour le monde!” dans la cellule A1 de la première feuille de calcul et enregistre le classeur.
// 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(); |