Microsoft Excel ファイルのエクスポート
Contents
[
Hide
]
Aspose.Cells.GridWeb コントロールを使用して、GUI モードの Web サイトで新しい Microsoft Excel ファイルを作成したり、既存の Microsoft Excel ファイルを操作したりできます。その後、ファイルを Excel ファイルに保存できます。 Aspose.Cells.GridWeb は、オンラインのスプレッドシート エディターとして効果的に機能します。このトピックでは、グリッド コンテンツを Excel ファイルに保存する方法について説明します。
Excel ファイルのエクスポート
ファイルとしてエクスポート
Aspose.Cells.GridWeb コントロールのコンテンツを Excel ファイルとして保存するには:
- Aspose.Cells.GridWeb コントロールを Web フォームに追加します。
- 指定したパスに Excel ファイルとして作業を保存します。
- アプリケーションを実行します。
Aspose.Cells.GridWeb コントロールを Web フォームに追加する方法がわからない場合は、以下を参照してください。GridWeb を Web フォームに追加する
Aspose.Cells.GridWeb コントロールが Windows フォームに追加されると、コントロールは自動的にインスタンス化され、既定のサイズでフォームに追加されます。 Aspose.Cells.GridWeb コントロール オブジェクトを作成する必要はありません。コントロールをドラッグ アンド ドロップして使用を開始するだけです。
次のコード例は、グリッド コンテンツを Excel ファイルに保存する方法を示しています。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
ファイル システムが NTFS の場合、ASPNET または Everyone ユーザー アカウントに読み取り/書き込みアクセスを許可しないと、実行時にアクセス拒否の例外が発生します。
上記のコード スニペットは、いくつかの方法で使用できます。一般的な方法は、クリックしたときにグリッド コンテンツを Excel ファイルに保存するボタンを追加することです。 Aspose.Cells.GridWeb は、タスクに対するより簡単なアプローチを提供します。 Aspose.Cells.GridWeb には SaveCommand というイベントがあります。上記のコード スニペットを SaveCommand イベントのイベント ハンドラに追加すると、ユーザーは Aspose.Cells.GridWeb の組み込みセーブボタン。
GridWeb の SaveCommand イベント
GridWeb の組み込みの [保存] ボタンをクリックして、グリッド コンテンツを Excel に保存する
Visual Studio で作業している場合は、SaveCommand イベントのイベント ハンドラーを簡単に作成できます。プロパティペイン。詳細については、次を参照してください。GridWeb イベントの操作
ストリームとしてエクスポート
グリッド コンテンツをストリーム (たとえば、MemoryStream) に保存することもできます。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |