Skip to main content
 首页 » 编程设计

c#之Excel 选择性粘贴和添加操作

2024年02月23日19leader

当我们想在所有 excel 工作表单元格中添加一个数字(例如 5)时,您可以复制包含值 5 的单元格,选择我们想要的其他单元格的范围(例如 10x10 范围)并右键单击 -> 粘贴特殊然后检查添加操作并单击确定。

我想在 C# 中使用 Excel Interop dll 将 5 添加到选定范围内的所有单元格。如何实现?

请您参考如下方法:

执行选择性粘贴 -> 添加操作相当简单。假设您已经有一个 Worksheet 对象,以下将起作用:

// Copy the initial value from cell A1 
 
xlWorksheet.get_Range("A1", "A1").Copy(Missing.Value); 
 
// Paste special (with Addition) the value over cells A2 to J11 
 
xlWorksheet.get_Range("A2", "J11").PasteSpecial(Excel.XlPasteType.xlPasteAll, 
    Excel.XlPasteSpecialOperation.xlPasteSpecialOperationAdd, false, false); 

您可以找到 PasteSpecial 方法的完整说明 here .