下面的代码在 Excel 工作簿之间执行复制粘贴,并且我可以根据您的需要提供三种方法。第 1 - 从工作表上的所有行和列复制数据,第 2 - 仅将数据复制到 B 列(2 列),无论其他列中是否有更多数据,第 3 - 类似于 opt2,但具有特殊粘贴功能。此外,它会保存所有内容,关闭并终止 PID,因此没有打开的 Excel Com-Object。
using System;
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using Range = Microsoft.Office.Interop.Excel.Range;
using System.Diagnostics;
namespace Excel
{
class CopyPaste2
{
public CopyPaste2()
{
//copy-paste dynamic range
source2WB = @"C:\WIP\source2WB.xlsm";
destinationWB = @"C:\WIP\destinationWB.xlsm";
Application xlApp = new Application();
xlApp.Visible = true;
Workbook sourceWorkbook2 = xlApp.Workbooks.Open(source2WB, 0, false, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
Workbook destinationWorkbook = xlApp.Workbooks.Open(destinationWB, 0, false, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
Worksheet SourceWorksheet = sourceWorkbook2.Worksheets.get_Item("Source2WSname");
Worksheet DestinationWorksheet = destinationWorkbook.Worksheets.get_Item("destinationWSname");
//Option 1 - copy data from all columns with value
//Range last = SourceWorksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing);
//Range sourceRng = SourceWorksheet.get_Range("A1", last);
//Option 2 - copy only data up to column defined (i.e.A - 1 col, B - 2 cols etc.) regardless if there is more data in other columns
Range last = SourceWorksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).End[XlDirection.xlToLeft];
Range sourceRng = SourceWorksheet.get_Range("B1", last);
//Option 3 - copy only data up to column defined (i.e.A - 1 col, B - 2 cols etc.) regardless if there is more data in other columns
//Range last = SourceWorksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).End[XlDirection.xlToLeft];
//Range sourceRng = SourceWorksheet.get_Range("B1", last);
// sourceRng.Copy(Missing.Value);
//Option 3 - paste data into column 3 under last used row
//Range destUsedRange = DestinationWorksheet.UsedRange;
//int nRows = destUsedRange.Rows.Count +1;
//Range destPaste = (Range)DestinationWorksheet.Cells[nRows, 3];
// destPaste.PasteSpecial(XlPasteType.xlPasteValues, XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
//Option 1 and 2 - paste data into column 3 under last used row
Range destUsedRange = DestinationWorksheet.UsedRange;
int nRows = destUsedRange.Rows.Count +1;
Range destPaste = (Range)DestinationWorksheet.Cells[nRows, 3];
sourceRng.Copy(destPaste);
destinationWorkbook.Save();
sourceWorkbook2.Close();
destinationWorkbook.Close();
xlApp.Quit();
int pid = -1;
HandleRef hwnd = new HandleRef(xlApp, (IntPtr)xlApp.Hwnd);
GetWindowThreadProcessId(hwnd, out pid);
KillProcess(pid, "EXCEL");
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowThreadProcessId(HandleRef handle, out int processId);
static public void KillProcess(int pid, string processName)
{
// to kill current process of excel
Process[] AllProcesses = Process.GetProcessesByName(processName);
foreach (Process process in AllProcesses)
{
if (process.Id == pid)
{
process.Kill();
}
}
AllProcesses = null;
}
}
}