Saving an Excel File

Introduction

To save the content of Aspose.Cells.GridDesktop control as an Excel file, Aspose.Cells.GridDesktop provides follwoing methods.

  1. Saving as a File
  2. Saving as a Stream

Saving File

Create a desktop application and add two buttons with a GridControl control to the form. Set text properties of buttons as Save as File and Save as Stream respectively.

Saving as a File

Create the Click event of the Save as File button and paste the following code inside it.

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

Saving as a Stream

Sometimes, it might be required by developers to save their Grid contents to a stream (For example, MemoryStream). To facilitate this task, Aspose.Cells.GridDesktop control also supports saving Grid data to a stream. Create the Click event of the Save as Stream button and paste the following code inside it.

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