Click here to Skip to main content
15,885,981 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
C#
Microsoft.Office.Interop.Excel.ApplicationClass Excelapp = new Microsoft.Office.Interop.Excel.ApplicationClass();
           Excelapp.Application.Workbooks.Add();

           int IndiceColumn = 0;
           foreach (DataGridViewColumn col in table.Columns)
           {

               IndiceColumn++;
               Excelapp.Cells[1, IndiceColumn] = col.Name;
           }

           int IndeceFile = 0;

           foreach (DataGridViewRow r in table.Rows)
           {
               {
                   IndeceFile++;
                   IndiceColumn = 0;
                   foreach (DataGridViewColumn col in table.Columns)
                   {
                       IndiceColumn++;
                       Excelapp.Cells[IndeceFile + 1, IndiceColumn] = r.Cells[col.Name].Value;
                   }
               }
              // Excelapp.Visible = true;
               Excelapp.ActiveWorkbook.SaveCopyAs(saveFileDialog1.FileName.ToString());

               Excelapp.ActiveWorkbook.Saved = true;
               Excelapp.Quit();

Its working Fine till First Row But Not getting On 2nd, But Crashing with Error exception from hresult:0x800a01a8 for Xls and exception from hresult:0x800a01C8 for Xlsx Files
I guess I need to Set Range but can Can Any one help me on How to do that
Posted
Updated 31-May-18 5:52am

1 solution

The model of Excel application is:
Excel->Workbooks[index]->Worksheets[index]->Cells[index, index]

This meant that you can't access to any cell direct from Excel application.

Have a look here[^]. There are tons of examples.
 
Share this answer
 
Comments
ZurdoDev 24-Jun-14 15:17pm    
Actually you can, http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.application_members(v=office.15).aspx

Cells is a member of Excel.Application.
Maciej Los 24-Jun-14 15:53pm    
Oh, you're right and wrong simultaneously...
You can't write to cell when none of workbook is opened. It's bad practice to use un-contextaual code. Check it!
ZurdoDev 24-Jun-14 15:54pm    
Correct. It will use the active worksheet, if there is one.
Maciej Los 24-Jun-14 16:02pm    
Seriously... Nope!
The proper way is to use something similar to:
Excel ExApp = New Excel;
Workbook wbk = ExApp.Workbooks.Add();
Worksheet wsh = wbk.Worksheets["WorksheetName"];
wsh.Cells[index, index] = "StringValue";
ZurdoDev 24-Jun-14 16:14pm    
No. Not at all. Your stubbornness is getting to be a little much. I pointed you to the official documentation that shows you can get to Cells through the Application. You can't argue that. You may not like it, and that is fair, but it can be done.

In fact, ActiveCell can be very commonly used especially in cases where the user is interacting with your app a lot. You can allow the user to select a cell and then your app will do what it needs based on which cell the user selected.

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