Microsoft Excelファイルをエクスポート

Excelファイルをエクスポート

ファイルとしてエクスポート

Aspose.Cells.GridWebコントロールのコンテンツをExcelファイルとして保存するには:

  1. Aspose.Cells.GridWebコントロールをWebフォームに追加します。
  2. 指定したパスに作業内容をExcelファイルとして保存します。
  3. アプリケーションを実行します。

Aspose.Cells.GridWebコントロールがウィンドウフォームに追加されると、デフォルトのサイズで自動的にインスタンス化され、フォームに追加されます。 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の組み込みSaveボタンをクリックしてグリッドのコンテンツを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();