ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   copy data from one excel to another in Visual C# (https://www.excelbanter.com/excel-programming/362233-copy-data-one-excel-another-visual-c.html)

berkeleydb_user

copy data from one excel to another in Visual C#
 
Guys

I need to copy data from one excel file to other. Am doing this in
asp.net with c# as backend code. Do I need to two excel Application
objects? No matter if I use two excel objects or one, I am ending up
with one excel object in memory even after closing them. Whats the best
way to do this without leaving any Excel apps around? Please help.

Heres what my code looks like:

Excel.Application exclApp = new Excel.ApplicationClass();
Excel.Workbook... srcWb = Open("source.xls",...)
Excel.WorkSheets srcSheets
Excel.Worksheet srcSheet

Excel.Workbook dstWb = open("dest.xls",....)
Excel.Worksheets dstSheets
Excel.Worksheet dstSheet..

dstSheet.range = srcSheet.range;

cleanup..

I call ReleaseComObject() on all above objects and finally.

exclApp.Quit();
GC.Collect();


[email protected]

copy data from one excel to another in Visual C#
 
I guess you didnt save the
workbook before closing it.

try this.

dstWb.Save()
srcWb.Save()
dstWb.Close()
srcWb.Close()

HTH


berkeleydb_user

copy data from one excel to another in Visual C#
 
Hi

I don't need to save them. I copy data from source excel to dest, run a
macro in dest excel, read that data to asp page and just exit both
excels.

I'm calling

dstWb.Save(false, Type.Missing, Type.Missing);
srcWb.Save(false, Type.Missing, Type.Missing);
dstWb.Close();
srcWb.Close();
exclApp.Quit();

In fact I even commented out all functionality of running macro,
reading values etc. Even then it doesn't get cleaned up. Could there be
some issue with my setup or could external things cause this.

But if I don't use source excel at all (commented out all code related
to srcWb) and just use whatever data is in dstExcel it does get cleaned
up.

thanks


[email protected]

copy data from one excel to another in Visual C#
 
I cant pin point the problem.
the only thing i can suggest
is to kill all excel process after
quiting.

like this....

string processName = "EXCEL";
foreach (Process thisproc in Process.GetProcessesByName(processName))
thisproc.Kill();


HTH



All times are GMT +1. The time now is 01:02 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com