Answers for "how to copy data from one excel file to another excel file using visual studio c#"

C#
0

how to copy data from one excel file to another excel file using visual studio c#

//using Spire.Xls;
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
    class Program
    {
      
        static void Main(string[] args)
        {
            Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
            Workbook curWorkBook = null;
            Workbook destWorkbook = null;
            Worksheet workSheet = null;
            Worksheet newWorksheet = null;
            Object defaultArg = Type.Missing;
            try
            {
                // Copy the source sheet
                curWorkBook = app.Workbooks.Open("D:\\Book1.xlsx", defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg);
                workSheet = (Worksheet)curWorkBook.Sheets[1];
                workSheet.UsedRange.Copy(defaultArg);

                // Paste on destination sheet
                destWorkbook = app.Workbooks.Open("D:\\Book2.xlsx", defaultArg, false, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg);
                newWorksheet = (Worksheet)destWorkbook.Worksheets.Add(defaultArg, defaultArg, defaultArg, defaultArg);
                newWorksheet.UsedRange._PasteSpecial(XlPasteType.xlPasteValues, XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
            }
            catch (Exception exc)
            {
                //.Windows.Forms.MessageBox.Show(exc.Message);
            }
            finally
            {
                if (curWorkBook != null)
                {
                    curWorkBook.Save();
                    curWorkBook.Close(defaultArg, defaultArg, defaultArg);
                }

                if (destWorkbook != null)
                {
                    destWorkbook.Save();
                    destWorkbook.Close(defaultArg, defaultArg, defaultArg);
                }
            }
            app.Quit();

        }
    }
}
Posted by: Guest on May-30-2021

Code answers related to "how to copy data from one excel file to another excel file using visual studio c#"

C# Answers by Framework

Browse Popular Code Answers by Language