.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

Please help me how to export the database table data to excel sheet

Posted By: Aditya Varma 999     Posted Date: June 14, 2014    Points:5   Category :C#
In the Button click event i am adding the below ode for exporting data from the database table to excel sheet.Please help to solve this porblem



Microsoft.Office.Interop.Excel.Application xlApp; Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;

xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
// xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet=(Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
int i = 0;
int j = 0;

for (i = 0; i <= dataGridView1.RowCount - 1; i++)
{
for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
{
DataGridViewCell cell = dataGridView1[j, i];
xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
}
}

xlWorkBook.SaveAs("csharp.net-informations.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();

releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);

MessageBox.Show("Excel file created , you can find the file c:\\csharp.net-informations.xls");

}

private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();

Responses
Author: ASPEvil             
Posted Date: June 15, 2014     Points: 20   

Hello,

For this purpose, you have to first load/fetch all your database's table data to a database object of dot net, like: Data Table, DataSet etc, then export data from that Database object to Excel. How to do this? Please see the following links which show this process in detail:-
-------------------------------------------------
http://www.gemboxsoftware.com/support/articles/import-export-dataset-xls-xlsx-ods-csv-html-net

http://www.codeproject.com/Tips/344604/Export-to-EXCEL-from-Datatable-in-Csharp-Net

http://social.msdn.microsoft.com/Forums/vstudio/en-US/ea02f8e8-edf5-4aca-8c74-ef3a94b5328d/export-dataset-into-excel-sheet-in-c?forum=csharpgeneral
-------------------------------------------------



Hope this will help.


Thanks.

Author: Ashutosh Jha             
Posted Date: June 19, 2014     Points: 20   

Hi,

Please follow the below link. It will resolve your issue.

http://www.c-sharpcorner.com/UploadFile/9c6206/export-data-from-database-table-to-excel-file-in-Asp-Net-mvc/

Regards,
Ashutosh Jha
ashutosh741@gmail.com

http://www.tricksroad.com
Author: gorge             
Posted Date: July 15, 2014     Points: 20   

Hello Sir,

sorry ,I can't figure what's wrong with your code ,however, this technical article on C#Corner may help you.
http://www.c-sharpcorner.com/UploadFile/deveshomar/exporting-datatable-to-excel-in-C-Sharp-using-interop/

Or,you can try other solutions provided a similiar post on donetfunda
http://www.dotnetfunda.com/forums/show/17274/how-to-export-datatable-to-excel-in-csharp

Hope this will give you some clue.

Kind regards

Author: sonyamorris             
Posted Date: January 17, 2015     Points: 20   

You can get the guidelines about the database from the internet and <a href="http://american-writers.org/"> custom essay writing service </a> .Many different types writing information are available on the internet.Correct export guidelines are available on the different resources.

Author: Debendra             
Posted Date: January 22, 2016     Points: 20   

http://www.c-sharpcorner.com/UploadFile/9c6206/export-data-from-database-table-to-excel-file-in-Asp-Net-mvc/



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend