保存Excel文件

介绍

要将Aspose.Cells.GridDesktop控件的内容保存为Excel文件,Aspose.Cells.GridDesktop提供了以下方法。

  1. 保存为文件
  2. 保存为流

保存文件

创建一个桌面应用程序,并向窗体添加带有GridControl控件的两个按钮。将按钮的文本属性分别设置为另存为文件另存为流

另存为文件

创建另存为文件按钮的单击事件,并在其中粘贴以下代码。

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

保存为流

有时,开发人员可能需要将Grid内容保存到流中(例如,MemoryStream)。为了简化这个任务,Aspose.Cells.GridDesktop控件也支持将Grid数据保存到流中。创建另存为流按钮的单击事件,并在其中粘贴以下代码。

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