Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The data in the datatable(from the database) should be written(save) to the excel sheet(2007).The excel sheet is a taken from a template. can somebody provide me with a source code(C#).
Posted
Updated 9-Feb-12 22:25pm
v3

Check this link to export from DataGridView to excel
same logic can be applied to export data from DataTable as well
http://vb.net-informations.com/excel-2007/vb.net_export_from_datagridview_to_excel.htm[^]


You can convert VB.NET code to C# code from following link
http://www.developerfusion.com/tools/convert/vb-to-csharp/[^]

Hope this might help you.
 
Share this answer
 
 
Share this answer
 
v2
plz look this :

in Add Reference tab .NET

add :

C#
Microsoft.Offilce.Interop.Excel



and in using add :

C#
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;

//----
try
            {
                var xl = new Excel.Application();

                xl.Workbooks.Add();
                xl.DefaultSheetDirection = (int)Excel.Constants.xlRTL;

                xl.Visible = true;
                xl.Cells[1, 1] = "cell1";
                xl.Cells[1, 2] = "cell2";
                xl.Cells[1, 3] = "cell3";






                for (int i = 0; i < dataGridView1.Rows.Count; i++)

                    for (int j = 0; j < 3; j++)
                    {
                        xl.Cells[i + 2, j + 1] = cl.clearSpase(dataGridView1.Rows[i].Cells[j].Value.ToString());

                    }

            }
            catch (Exception ex)
            {
                
            }
 
Share this answer
 
v2
Comments
Xaiver101 10-Feb-12 3:44am    
Is it saving as a excel 2007 format if i use your code???
esmailian 10-Feb-12 4:17am    
No , you were not told Save as in question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900