使用C#在Excel中复制/粘贴单元格 [英] Copy/paste cells in Excel with C#

查看:129
本文介绍了使用C#在Excel中复制/粘贴单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Excel文件中选择特定的用户范围并复制这些单元格,并使用C#插入复制的单元格 Shift:= xlDown .

How to select a specific user range in Excel file and copy those cells and Insert copied cells Shift:=xlDown using C#.

这是我需要转换为C#的VBA代码:

This is the VBA code I need to convert into C#:

Range("A9:L9").Select
Selection.Copy
Rows("10:10").Select
Selection.Insert Shift:=xlDown
Range("F10").Select

我不知道如何将此代码转换为C#代码以运行.

I don't know how to convert this code into C# code to run.

推荐答案

如果您还没有尝试过,那么可以尝试

If you have'nt tried this yet, then you can try this

VS 中添加对项目的引用: Microsoft.Office.Interop.Excel

Add reference to your project in VS: Microsoft.Office.Interop.Excel

using Excel = Microsoft.Office.Interop.Excel;
class Program
{
    static void Main(string[] args)
    {
       var excelapp = new Excel.Application();
       excelapp.Workbooks.Add();
       string path = "Your Excel Path";            
       Excel.Workbook workbook = excelapp.Workbooks.Open(path);
       Excel.Worksheet workSheet = workbook.Worksheets.get_Item(1);
       Excel.Range source = workSheet.Range["A9:L9"].Insert(Excel.XlInsertShiftDirection.xlShiftDown);
       Excel.Range dest = workSheet.Range["F10"];
       source.Copy(dest);
    }
}

这篇关于使用C#在Excel中复制/粘贴单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆