3
Answers

Error in copy-paste excel using C#

The below written for copy and paste excel using c#.here i am facing some error..pls give your idea to solve my error..

  Microsoft.Office.Interop.Excel.Application eAPP;
  Microsoft.Office.Interop.Excel.Workbook eBOOK;
  Microsoft.Office.Interop.Excel.Worksheet eSHEET;
  string[] files = System.IO.Directory.GetFiles(@"D:\temp\sankalp_rep");
  string filename;
  foreach (string flename in files)
  {
  eAPP = new Microsoft.Office.Interop.Excel.Application();
  eBOOK = eAPP.Workbooks.Open(flename, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, false, false);
  eBOOK = eAPP.ActiveWorkbook;
  eSHEET = (Microsoft.Office.Interop.Excel.Worksheet)eBOOK.ActiveSheet;
  eSHEET.Cells.Copy("A2");
  if (eAPP.Workbooks.Count <= 1)
  {
  eAPP = new Microsoft.Office.Interop.Excel.Application();
  }
  eAPP.Workbooks.Open(@"D:\my.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, false, false);
 
  eBOOK = eAPP.ActiveWorkbook;
  eSHEET = (Microsoft.Office.Interop.Excel.Worksheet)eBOOK.ActiveSheet;
 
  eAPP.DisplayAlerts = false;
  eSHEET.Paste("A2", "v500");
  eSHEET.Cells.Rows.AutoFit();
  eAPP.ActiveWindow.Zoom = 60;
  eAPP.ActiveWorkbook.Save();
  //  eAPP.ActiveWorkbook.Close();
  eAPP.Quit();
  releaseObject(eAPP);
  releaseObject(eBOOK);
  releaseObject(eSHEET);
  }
  foreach (System.Diagnostics.Process proc in System.Diagnostics.Process.GetProcessesByName("EXCEL"))
  {
  proc.Kill();
  }

Answers (3)