sourcecode

특정 범위의 Excel 셀을 한 워크시트에서 다른 워크시트로 복사

copyscript 2023. 8. 22. 22:24
반응형

특정 범위의 Excel 셀을 한 워크시트에서 다른 워크시트로 복사

저는 한 워크북의 워크시트에서 다른 워크북의 워크시트로 셀 범위를 복사하는 C# 프로그램을 작성하고 있습니다.하지만 제가 직면한 문제는 첫 번째 워크북의 전체 워크시트를 복사하여 붙여넣을 수 있다는 것입니다.특정 범위(5열[1열]에서 10열까지]에서 100열[1열]까지)만 선택하여 2열 8열부터 두 번째 워크북 워크시트에 붙여넣는 방법을 알고 싶습니다.

또한 아래와 같은 루프를 사용하지 않고 직접적인 방법으로 C1에서 C100까지 열을 채우는 방법을 알고 싶습니다.

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

여기 제가 지금까지 작성한 코드가 있습니다.

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();

        }
    }
}

이 작업을 수행할 수 있어야 합니다.

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

        from.Copy(to);

Mrtig는 매우 우아한 해결책을 가지고 있습니다.그러나 Excel의 별도 사례에 워크북이 있으면 작동하지 않습니다.핵심은 단 한 번의 사례로 여는 것입니다.이 접근 방식을 사용하여 보여주도록 예제를 수정했습니다.

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();
}

전체 범위에 대해 동일한 값을 설정하는 첫 번째 부분의 경우 루프 대신 다음 사항이 해결됩니다.

범위 1 = 작업Sheet.get_Range("A1:B100"); 범위 1.값 = "고정";

그리고 복사는 @mrtig가 제안한 것을 시도할 수 있습니다.

언급URL : https://stackoverflow.com/questions/25463453/copying-of-specific-range-of-excel-cells-from-one-worksheet-to-another-worksheet

반응형