Excelファイルを保存する
紹介
Aspose.Cells.GridDesktopコントロールのコンテンツをExcelファイルとして保存するには、Aspose.Cells.GridDesktopは以下のメソッドを提供しています。
- ファイルとして保存
- ストリームとして保存
ファイルを保存
デスクトップアプリケーションを作成し、フォームにGridControlコントロールと2つのボタンを追加します。ボタンのテキストプロパティをそれぞれ Save as File と Save as Stream に設定してください。
ファイルとして保存する
Save as File ボタンのクリックイベントを作成し、以下のコードを中に貼り付けてください。
// 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データをストリームに保存するのをサポートしています。Save as Stream ボタンのクリックイベントを作成し、以下のコードを中に貼り付けてください。
// 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(); |