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

我正在写一个C#程序,它将一个工作簿的工作表中的单元格范围复制到其他工作簿的工作表中。 但是我面对的问题是我只能复制和粘贴第一个工作簿的整个工作表。 我想知道如何只select一个特定的范围(从第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(); } } } 

对于整个范围设置相同的值的第一部分,而不是循环下面将工作

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

而复制你可以尝试@mrtigbuild议。

你应该能够做到这一点:

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

mrtig有一个非常优雅的解决scheme。 但是,如果您将工作簿放在不同的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(); }