Issue wrting data to an excel during runtime

Ask general questions here.
kaancha
Posts: 17
Joined: Wed Oct 22, 2014 4:12 pm

Issue wrting data to an excel during runtime

Post by kaancha » Thu Apr 14, 2016 2:03 pm

I am using this Excel object to write data to my current spreadsheet:
Excel.Application tApp = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");

My Issue:
It works only when I have my excel spreadsheet opened but it does not work if I do not open the spreadsheet manually.

I added some codes to open the spreadsheet, but it did not work either. Besides, I do not want to open / save and close the spreadsheet for each iteration.
Here is the code example:
Excel.Application eApp = new Excel.ApplicationClass();
eApp.Visible = true;
string workbookPath = "c:\\abc.xlsx";
Excel.Workbook excelWorkbook = eApp.Workbooks.Open(workbookPath);
eApp = null;


I even used this as a standalone test case to avoid opening each time for multiple iterations, but it did not work either.

What I am doing wrong here? I appreciate your help.

Diddy
Posts: 7
Joined: Tue Feb 09, 2016 4:00 pm

Re: Issue wrting data to an excel during runtime

Post by Diddy » Thu Apr 14, 2016 2:52 pm

If you use System.Runtime.InteropServices you need a running instance of Excel. As you just wanna write data without open/save/close the Excel application you should use Microsoft.Office.Interop.Excel like shown in the example here:
https://msdn.microsoft.com/en-us/librar ... s.80).aspx

Have fun!

kaancha
Posts: 17
Joined: Wed Oct 22, 2014 4:12 pm

Re: Issue wrting data to an excel during runtime

Post by kaancha » Thu Apr 14, 2016 8:46 pm

Thanks. the way I wrote also working now . Not sure why it didn't work earlier.