I need to export data to Excel by getting data from Database

Hi,
I am developing a website. On web page I need to create a link when user clicks on that link report shoud display and when user click on save button it should save as an excel file.

I would appreciate if you can email me the sample code to do this.

Thanks,

Hi,
You could use WebWorksheets.ImportDataView Method to import data to the web control from a database query. Our Aspose.Excel.Web setup bundle includes the ImportDataView sample code.

@fractal,
Aspose.Excel is discontinued and no more under active development now. A new product Aspose.Cells is introduced that contains all the latest features available in different versions of MS Excel. You can import data from a variety of objects into Excel file. Here is an example that demonstrates the feature of importing data table into Excel file that is filled from database.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET

// Instantiating a Workbook object
Workbook workbook = new Workbook();

// Obtaining the reference of the worksheet
Worksheet worksheet = workbook.Worksheets[0];

// Instantiating a "Products" DataTable object
DataTable dataTable = new DataTable("Products");

// Adding columns to the DataTable object
dataTable.Columns.Add("Product ID", typeof(Int32));
dataTable.Columns.Add("Product Name", typeof(string));
dataTable.Columns.Add("Units In Stock", typeof(Int32));

// Creating an empty row in the DataTable object
DataRow dr = dataTable.NewRow();

// Adding data to the row
dr[0] = 1;
dr[1] = "Aniseed Syrup";
dr[2] = 15;

// Adding filled row to the DataTable object
dataTable.Rows.Add(dr);

// Creating another empty row in the DataTable object
dr = dataTable.NewRow();

// Adding data to the row
dr[0] = 2;
dr[1] = "Boston Crab Meat";
dr[2] = 123;

// Adding filled row to the DataTable object
dataTable.Rows.Add(dr);

// Importing the contents of DataTable to the worksheet starting from "A1" cell,
// Where true specifies that the column names of the DataTable would be added to
// The worksheet as a header row
worksheet.Cells.ImportDataTable(dataTable, true, "A1");

// Saving the Excel file
workbook.Save("DataImport.out.xls");

You may refer to the following article for more information about importing data in Excel file:
Import Data into Worksheet

For trials purpose, download the latest free version here:
Aspose.Cells for .NET (Latest Version)

A comprehensive solution is available here that can be used to test different features of this product.