Can you export an asp.net datagrid to excel using the aspose.excel?
Yes, you can. You can export data in the datagrid to a DataTable, then use Cells.ImportDataTable method to import it in Aspose.Excel.
Aspose.Excel is discontinued and is replaced with a much-advanced version Aspose.Cells. This new product contains a lot of exciting features which are available in different versions of MS Excel. You can achieve your requirement using this new product as well, as shown in the following sample code:
Exporting Data to a DataTable
// Creating a new DataTable object DataTable dataTable = new DataTable(); // Adding specific columns to the DataTable object dataTable.Columns.Add("ProductName", System.Type.GetType("System.String")); dataTable.Columns.Add("CategoryName", System.Type.GetType("System.String")); dataTable.Columns.Add("QuantityPerUnit", System.Type.GetType("System.String")); dataTable.Columns.Add("UnitsInStock", System.Type.GetType("System.Int32")); // Exporting the data of the first worksheet of the Grid to the specific DataTable object dataTable = gridDesktop1.Worksheets.ExportDataTable(dataTable, 0, 0, 69, 4, true);
Once the data table is filled then you can import it to an Excel file as follows:
// Instantiating a Workbook object Workbook workbook = new Workbook(); // Obtaining the reference of the worksheet Worksheet worksheet = workbook.Worksheets; worksheet.Cells.ImportDataTable(dataTable, true, "A1"); // Saving the Excel file workbook.Save(dataDir + "DataImport.out.xls");
You may download the latest version of this product here:
Aspose.Cells for .NET (Latest Version)
A ready to run solution for testing the features of this new product is available here.