导出Microsoft Excel文件

导出Excel文件

导出为文件

要将 Aspose.Cells.GridWeb 控件的内容另存为 Excel 文件:

  1. 将Aspose.Cells.GridWeb控件添加到Web表单中。
  2. 将您的工作保存为指定路径的 Excel 文件。
  3. 运行应用程序。

将Aspose.Cells.GridWeb控件添加到Windows表单时,控件会自动实例化并添加到具有默认大小的表单中。您不必创建一个Aspose.Cells.GridWeb控件对象,您只需拖放控件并开始使用。

下面的代码示例说明了如何将网格内容保存为 Excel 文件。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Generates a temporary file name.
string filename = Session.SessionID + "_out.xls";
string path = (this.Master as Site).GetDataDir() + "\\GridWebBasics\\";
// Saves to the file.
this.GridWeb1.SaveToExcelFile(path + filename);
// Sents the file to browser.
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment; filename=" + filename);
Response.WriteFile(path + filename);
Response.End();

上述代码片段可以以多种方式使用。常见的方法是添加一个按钮,当点击时将网格内容保存为Excel文件。Aspose.Cells.GridWeb提供了更简单的方法。Aspose.Cells.GridWeb具有一个名为SaveCommand的事件。上述代码片段可以添加到SaveCommand事件的事件处理程序中,允许用户通过点击Aspose.Cells.GridWeb的内置保存按钮来保存他们的工作。

GridWeb的SaveCommand事件

todo:image_alt_text

点击GridWeb的内置保存按钮将网格内容保存到Excel

todo:image_alt_text

作为流导出

还可以将网格内容保存到流(例如MemoryStream)中。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Generates a temporary file name.
string filename = Session.SessionID + "_out.xls";
string path = (this.Master as Site).GetDataDir() + "\\GridWebBasics\\";
FileStream fs = File.Create(path + filename);
// Saving Grid content of the control to a stream
GridWeb1.SaveToExcelFile(fs);
// Closing stream
fs.Close();
// Sents the file to browser.
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition", "attachment; filename=" + filename);
Response.WriteFile(path + filename);
Response.End();