Enregistrer un fichier Excel
Introduction
Pour enregistrer le contenu du contrôle Aspose.Cells.GridDesktop en tant que fichier Excel, Aspose.Cells.GridDesktop fournit les méthodes suivantes.
- Enregistrement en tant que fichier
- Enregistrement en tant que flux
Enregistrement du fichier
Créez une application de bureau et ajoutez deux boutons avec un contrôle GridControl au formulaire. Définir les propriétés de texte des boutons commeEnregistrer en tant que fichier etEnregistrer en tant que flux respectivement.
Enregistrement en tant que fichier
Créez l’événement Click duEnregistrer en tant que fichier bouton et collez le code suivant à l’intérieur.
// 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 = Utils.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Saving Grid contents to an Excel file | |
gridDesktop1.ExportExcelFile(dataDir + "book1_out.xls"); | |
// Saving Grid contents to MS Excel 2007 Xlsx file format | |
gridDesktop1.ExportExcelFile(Utils.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType) + "book1_out.xlsx", FileFormatType.Excel2007Xlsx); |
Enregistrement en tant que flux
Parfois, il peut être demandé aux développeurs d’enregistrer le contenu de leur grille dans un flux (par exemple, MemoryStream). Pour faciliter cette tâche, le contrôle Aspose.Cells.GridDesktop prend également en charge l’enregistrement des données de grille dans un flux. Créez l’événement Click duEnregistrer en tant que flux bouton et collez le code suivant à l’intérieur.
// 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 = Utils.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Opening an Excel file as a stream | |
FileStream fs = File.Open(dataDir + "book1_out.xls", FileMode.Open, FileAccess.ReadWrite); | |
// Saving Grid contents of the control to a stream | |
gridDesktop1.ExportExcelFile(fs); | |
// Closing stream | |
fs.Close(); |