how to copy and paste an excel sheet with source formatting in c# window application?

Viewed 219

i tired this code but it only paste the copied sheet as a embed sheet in my destination location. i want to create to create a window application that should make a copy of source excel file and paste to destination location.kindly help me

        Excel.Application srcxlApp;
        Excel.Workbook srcworkBook;
        Excel.Worksheet srcworkSheet;
        Excel.Range srcrange;
        Excel.Application destxlApp;
        Excel.Workbook destworkBook;
        Excel.Worksheet destworkSheet;
        Excel.Range destrange;
        string srcPath;
        string destPath;

        //Opening of first worksheet and copying
        srcPath = tbpath.Text;
        srcxlApp = new Excel.Application();
        srcworkBook = srcxlApp.Workbooks.Open(srcPath);
        srcworkSheet = srcworkBook.Worksheets.get_Item(1);
        srcrange = srcworkSheet.UsedRange;
        srcrange.Copy(Type.Missing);

        //opening of the second worksheet and pasting
        destPath = "C:\\Users\\Dell\\Desktop\\don\\Book1.xlsx";
        destxlApp = new Excel.Application();
        destworkBook = destxlApp.Workbooks.Open(destPath, 0, false);
        destworkSheet = destworkBook.Worksheets.get_Item(1);
        destrange = destworkSheet.Cells[1, 1];
        // destrange.Select();

        destworkSheet.PasteSpecial(Excel.XlPasteType.xlPasteAll, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
       // destworkSheet.PasteSpecial(Type.Missing, Type.Missing);
        destworkBook.SaveAs("C:\\Users\\Dell\\Desktop\\don" + DateTime.Now.ToString("MM_dd_yyyy") + ".xlsx");
        srcxlApp.Application.DisplayAlerts = false;
        destxlApp.Application.DisplayAlerts = false;
        destworkBook.Close(true, null, null);
        destxlApp.Quit();
        srcworkBook.Close(false, null, null);
        srcxlApp.Quit();
0 Answers
Related