Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copying of specific range of excel cells from one worksheet to another worksheet

Tags:

c#

excel

I am writing a C# program which copies a range of cells from a worksheet of one workbook to a worksheet of an other workbook. But the problem I am facing is I am only able to copy and paste the whole worksheet of first workbook. I want to know how to select only a specific range(from row 5 [column 1 to column 10] to row 100 [column 1 to column 10]) and paste it in second workbook worksheet starting from row 2 column 8.

Also i want to know how a fill a column say from C1 to C100 with some value in a direct way instead of using the loop like below

for(i=1;i<2;i++)
{
for(j=1;j<101;i++)
{
worksheet.cells[i,j]="Fixed";
}
}

Here is the code that i have written so far

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            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="C:\\Documents and Settings\\HARRY\\Desktop\\incident.csv";
            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:\\Documents and Settings\\HARRY\\Desktop\\FIXED Aging incident Report.xls";
            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.Paste(Type.Missing, Type.Missing);
            destworkBook.SaveAs("C:\\Documents and Settings\\HARRY\\Desktop\\FIXED Aging incident Report " + DateTime.Now.ToString("MM_dd_yyyy") + ".xls");
            srcxlApp.Application.DisplayAlerts = false;
            destxlApp.Application.DisplayAlerts = false;
            destworkBook.Close(true, null, null);
            destxlApp.Quit();
            srcworkBook.Close(false, null, null);
            srcxlApp.Quit();

        }
    }
}
like image 662
Harry Avatar asked Aug 23 '14 15:08

Harry


2 Answers

You should be able to do this:

        Excel.Range from = srcworkSheet.Range("C1:C100");
        Excel.Range to = destworkSheet.Range("C1:C100");

        from.Copy(to);
like image 200
mrtig Avatar answered Nov 03 '22 00:11

mrtig


mrtig has a very elegant solution. But it won't work if you have the workbooks in separate instances of excel. So, the key is to open them in just one instance. I've modified your example to show using this approach:

public void CopyRanges()
{
    // only one instance of excel
    Excel.Application excelApplication = new Excel.Application();

    srcPath="C:\\Documents and Settings\\HARRY\\Desktop\\incident.csv";
    Excel.Workbook srcworkBook = excelApplication.Workbooks.Open(srcPath);
    Excel.Worksheet srcworkSheet = srcworkBook.Worksheets.get_Item(1);

    destPath = "C:\\Documents and Settings\\HARRY\\Desktop\\FIXED Aging incident Report.xls";
    Excel.Workbook destworkBook = excelApplication.Workbooks.Open(destPath,0,false);
    Excel.Worksheet destworkSheet = destworkBook.Worksheets.get_Item(1);

    Excel.Range from = srcworkSheet.Range("C1:C100");
    Excel.Range to = destworkSheet.Range("C1:C100");

    // if you use 2 instances of excel, this will not work
    from.Copy(to);

    destworkBook.SaveAs("C:\\Documents and Settings\\HARRY\\Desktop\\FIXED Aging incident Report " + DateTime.Now.ToString("MM_dd_yyyy") + ".xls");
    srcxlApp.Application.DisplayAlerts = false;
    destxlApp.Application.DisplayAlerts = false;
    destworkBook.Close(true, null, null);
    srcworkBook.Close(false, null, null);
    excelApplication.Quit();
}
like image 45
Jahmic Avatar answered Nov 02 '22 23:11

Jahmic