Click here to Skip to main content
15,902,022 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
 Microsoft.Office.Interop.Excel.Application objApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbooks objBooks = objApp.Workbooks;
            Microsoft.Office.Interop.Excel.Workbook objBook = objBooks.Add(Missing.Value);
            Microsoft.Office.Interop.Excel.Worksheet objSheet = null;

            Microsoft.Office.Interop.Excel.Range range = null;

            if (dt.Rows.Count > 0)
            {
                objSheet = (Microsoft.Office.Interop.Excel.Worksheet)objBook.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                ((Microsoft.Office.Interop.Excel.Worksheet)objBook.Sheets[1]).Select(Missing.Value);
 int row = 0;
                
                objSheet.Cells[row + 1, 2] = "SocCode";
 objBook.Close(true, @"D:\DMR.xlsx", Missing.Value);
                objBooks.Close();

            }

I am Generating Excel File that i file created i want open through code any body help me.........
Posted
Comments
[no name] 30-Apr-14 6:53am    
it means are you getting any kind of exception ? show provide us details

1 solution

try this one..

C#
if (dataGridView1.Rows.Count > 0)
           {
               Microsoft.Office.Interop.Excel.ApplicationClass XcelApp = newMicrosoft.Office.Interop.Excel.ApplicationClass();
               XcelApp.Application.Workbooks.Add(Type.Missing);

               for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
               {
                   XcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
               }

               for (int i = 0; i < dataGridView1.Rows.Count; i++)
               {
                   for (int j = 0; j < dataGridView1.Columns.Count; j++)
                   {
                       XcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                   }
               }
               XcelApp.Columns.AutoFit();
               XcelApp.Visible = true;
           }
 
Share this answer
 
Comments
satheeshkumar chinnadurai 30-Apr-14 4:53am    
That excel file opened but i am created excel not opened..... plz help me
satheeshkumar chinnadurai 30-Apr-14 5:20am    
objApp.Visible = true;
ProcessStartInfo startInfo = new ProcessStartInfo("D:\\DMR.xlsx");
Process.Start(startInfo);
satheeshkumar chinnadurai 30-Apr-14 5:20am    
finally i got solution

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