繁体   English   中英

将特定范围的 Excel 单元格从一个工作表复制到另一个工作表

[英]copying of specific range of excel cells from one worksheet to another worksheet

我正在编写一个 C# 程序,它将一系列单元格从一个工作簿的工作表复制到另一个工作簿的工作表。 但我面临的问题是我只能复制和粘贴第一个工作簿的整个工作表。 我想知道如何仅选择特定范围(从第 5 行 [第 1 列到第 10 列] 到第 100 行 [第 1 列到第 10 列])并将其粘贴到从第 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();
}

对于为整个范围设置相同值的第一部分,而不是循环以下将解决

range1 = workSheet.get_Range("A1:B100"); range1.Value = "固定";

对于复制,您可以尝试@mrtig 的建议。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM