Copy Excel Worksheet One File to Another File in c#
Please flow the below steps .
Step 1
Add reference Microsoft.Office.Interop.Excel;
Step 2
Use below namespace .
using Microsoft.Office.Interop.Excel;
using System.Reflection;
Step 3
Copy & Paste below code inside the button click event or any event you want to copy worksheet destination file to source file .
ApplicationClass excelApplicationClass = new ApplicationClass();
_Workbook finalWorkbook = null;
Workbook workBook = null;
Worksheet workSheet = null;
Worksheet newWorksheet = null;
try
{
//Open the destination WorkBook
finalWorkbook = excelApplicationClass.Workbooks.Open(Server.MapPath("Test.xlsx"), false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//Open the source WorkBook
workBook = excelApplicationClass.Workbooks.Open(Server.MapPath("bargarh.xlsx"), false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//Open the WorkSheet
workSheet = (Worksheet)workBook.Sheets[1];
int countWorkSheet = finalWorkbook.Worksheets.Count;
//newWorksheet = (Worksheet)finalWorkbook.Worksheets.Add(Missing.Value, finalWorkbook.Sheets[countWorkSheet], 1, Missing.Value);
newWorksheet = (Worksheet)finalWorkbook.Sheets[countWorkSheet];
//newWorksheet.Name = "Sheet" + countWorkSheet+1;
//newWorksheet.Name = reportDetailBE.FileName ;
workSheet.Copy(Missing.Value, newWorksheet); //Copy from source to destination
finalWorkbook.Save();
workBook.Save();
Response.Write("Merged !");
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (finalWorkbook != null)
{
finalWorkbook.Close(true, Missing.Value, Missing.Value);
}
if (workBook != null)
workBook.Close(true, Missing.Value, Missing.Value);
if (workSheet != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
workSheet = null;
if (workBook != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
if (finalWorkbook != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(finalWorkbook);
workBook = null;
if (excelApplicationClass != null)
{
excelApplicationClass.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplicationClass);
excelApplicationClass = null;
}
}
Step 1
Add reference Microsoft.Office.Interop.Excel;
Step 2
Use below namespace .
using Microsoft.Office.Interop.Excel;
using System.Reflection;
Step 3
Copy & Paste below code inside the button click event or any event you want to copy worksheet destination file to source file .
ApplicationClass excelApplicationClass = new ApplicationClass();
_Workbook finalWorkbook = null;
Workbook workBook = null;
Worksheet workSheet = null;
Worksheet newWorksheet = null;
try
{
//Open the destination WorkBook
finalWorkbook = excelApplicationClass.Workbooks.Open(Server.MapPath("Test.xlsx"), false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//Open the source WorkBook
workBook = excelApplicationClass.Workbooks.Open(Server.MapPath("bargarh.xlsx"), false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//Open the WorkSheet
workSheet = (Worksheet)workBook.Sheets[1];
int countWorkSheet = finalWorkbook.Worksheets.Count;
//newWorksheet = (Worksheet)finalWorkbook.Worksheets.Add(Missing.Value, finalWorkbook.Sheets[countWorkSheet], 1, Missing.Value);
newWorksheet = (Worksheet)finalWorkbook.Sheets[countWorkSheet];
//newWorksheet.Name = "Sheet" + countWorkSheet+1;
//newWorksheet.Name = reportDetailBE.FileName ;
workSheet.Copy(Missing.Value, newWorksheet); //Copy from source to destination
finalWorkbook.Save();
workBook.Save();
Response.Write("Merged !");
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (finalWorkbook != null)
{
finalWorkbook.Close(true, Missing.Value, Missing.Value);
}
if (workBook != null)
workBook.Close(true, Missing.Value, Missing.Value);
if (workSheet != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
workSheet = null;
if (workBook != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
if (finalWorkbook != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(finalWorkbook);
workBook = null;
if (excelApplicationClass != null)
{
excelApplicationClass.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplicationClass);
excelApplicationClass = null;
}
}
If you have any problem please ask me by email :smruti.writeme@gmail.com
Or follow the below link for update in Facebook .
Thanks
Smruti
Comments
Post a Comment