Jump to content


Check out our Community Blogs

zaqalop

Member Since 01 Jun 2012
Offline Last Active Jan 12 2015 12:00 AM
-----

#631634 Copying Excel Sheets

Posted by zaqalop on 12 June 2012 - 08:48 PM

Hi, My deepest apologies, I just type without testing on my machine,

here is a full code, should work and already tested

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Excel;
namespace ExcelTest
{
    class Program
    {
	    static void Main(string[] args)
	    {
		    string pathFileSource = "D:\\BookSource.xlsx";
		    string pathFileDestination = "D:\\BookDest.xlsx";
		    Application excel = new Application();
		    Workbook wbSource = excel.Workbooks.Open(pathFileSource, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
		    Workbook wbDestination = excel.Workbooks.Open(pathFileDestination, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
		    Worksheet WorksheetSource = wbSource.Sheets[1];
		    //Copy all range in this worksheet
		    WorksheetSource.UsedRange.Copy(Missing.Value);
		    Worksheet WorksheetDestination = wbDestination.Sheets[1];
		    // Select used Range, paste value only
		    WorksheetDestination.UsedRange.PasteSpecial(XlPasteType.xlPasteValues ,XlPasteSpecialOperation.xlPasteSpecialOperationAdd, false, false);
		 
		    //Close all workbook
		    wbSource.Close();
		    wbDestination.Save();
		    wbDestination.Close();
		    //Quit application
		    excel.Quit();
	    }
    }
}

  • 1


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download