DataGrid samples

I am evaluating products to use in a current project and would like to test Aspose quickly. I need to export multiple DataGrids to multiple workbook sheets. Where can I get a sample to use as a template/example to learn how this is done with Aspose?

It’s easy to import datagrids with Aspose.Excel. You can try the following code:

Excel excel = new Excel();

excel.Worksheets[0].Cells.ImportDataGrid(this.dataGrid1, 1, 1, this.dataGrid1.Items.Count, this.dataGrid1.Columns.Count, true);

excel.Worksheets[1].Cells.ImportDataGrid(this.dataGrid2, 1, 1, this.dataGrid2.Items.Count, this.dataGrid2.Columns.Count, true);

excel.Worksheets[2].Cells.ImportDataGrid(this.dataGrid3, 1, 1, this.dataGrid3.Items.Count, this.dataGrid3.Columns.Count, true);

Ok, that works. Now the next step - how do I send the excel file to the browser?

The goal is to send the contents of a datagrid to the user as an excel document.

-Wes

Hi Wes,

After you import data from the datagrids, you can use Excel.Save method to send the result file to the user.

excel.Save(“book1.xls”, SaveType.OpenInExcel, FileFormatType.Default, this.Response);

Ah! So simple. Hmmm - but one problem - all my Datagrid header and formatting are gone - can those be exported also?

-Wes

Hi Wes,

I will investigate this feature and I think it’s feasible. But I have lost of other feature requests on hand. So it will not be availabe in a short time.

So this is not available at this time? How about exporting a datagrid to a template that contains the headers?

-Wes

Not availabe right now. I will think about importing datagrid headers in Aspose.Excel. Now you can try to export the headers with your own code.

@asewes,
Aspose.Excel is no more continued now and is replaced by an advanced product Aspose.Cells which not only contains all the features available in Aspose.Excel but also contains the latest features available in different versions of MS Excel. In this new product variety of ways are there to import data including import from DataGrid. Here is an example which demonstrates this feature:

// Create a DataTable object and set it as DataSource of DataGrid.
DataTable dataTable = new DataTable("Products");
dataTable.Columns.Add("Product ID", typeof(Int32));
dataTable.Columns.Add("Product Name", typeof(string));
dataTable.Columns.Add("Units In Stock", typeof(Int32));
DataRow dr = dataTable.NewRow();
dr[0] = 1;
dr[1] = "Aniseed Syrup";
dr[2] = 15;
dataTable.Rows.Add(dr);
dr = dataTable.NewRow();
dr[0] = 2;
dr[1] = "Boston Crab Meat";
dr[2] = 123;
dataTable.Rows.Add(dr);

// Now take care of DataGrid
DataGrid dg = new DataGrid();
dg.DataSource = dataTable;
dg.DataBind();

// We have a DataGrid object with some data in it.
// Lets import it into our spreadsheet

// Creat a new workbook
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];

// Importing the contents of the data grid to the worksheet
worksheet.Cells.ImportDataGrid(dg, 0, 0, false);

// Save it as Excel file
workbook.Save(dataDir + "output.xlsx");

This article explains all the means of importing data into Excel file:
Import Data into Worksheet

For trials purpose latest version can be downloaded here:
Aspose.Cells for .NET (Latest Version)

Here is a runnable solution which can be used to test different features with the help of hundreds of ready to run examples.