Dearl all,

Is there any way or codes for export dataset or datatabe to excel file. Whenever I use click event in the button, it will save my report in an excel file. My information inside in datatable (dataset) from database and I want to save it in excel file.

Thank you for attend...
Have a nice weekend...

Thank you...

There is even easier method to export DataSet to Excel, with this Excel C# library:

// Create new ExcelFile.
var ef = new ExcelFile();

// Imports all the tables from DataSet to new file.
foreach (DataTable dataTable in dataSet.Tables)
{
    // Add new worksheet to the file.
    var ws = ef.Worksheets.Add(dataTable.TableName);

    // Insert the data from DataTable to the worksheet starting at cell "A1".
    ws.InsertDataTable(dataTable, "A1", true);
}

// Save the file to XLS format.
ef.SaveXls("DataSet.xls");

Dearl all,

Is there any way or codes for export dataset or datatabe to excel file. Whenever I use click event in the button, it will save my report in an excel file. My information inside in datatable (dataset) from database and I want to save it in excel file.

Thank you for attend...
Have a nice weekend...

Hopefully, you can also find following method works. Before you can run the code, remember to reference .net Excel dll from: http://spreadsheet.codeplex.com/

//connect database
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString @"Provider=""Microsoft.Jet.OLEDB.4.0"";Data Source=""demo.mdb"";User Id=;Password="
OleDbCommand command = new OleDbCommand();
command.CommandText = "select * from parts";
DataSet dataSet = new System.Data.DataSet();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command.CommandText,connection);
dataAdapter.Fill(dataSet);
DataTable t = dataSet.Tables[0];
//export datatable to excel
Workbook book = new Workbook();
Worksheet sheet = book.Worksheets[0];
sheet.InsertDataTable(t, true, 1, 1);
book.SaveToFile("insertTableToExcel.xls");
System.Diagnostics.Process.Start("insertTableToExcel.xls");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.