简体   繁体   English

如何在C#中以编程方式选择多个工作表并同时复制到同一个工作簿

[英]How to programmatically select multiple sheets and copy to the same workbook at the same time in C#

I'm looking for some advices.我正在寻找一些建议。

I'm sorry, I don't know how to make it clarify in text, so, I will show you what I need to achieve via Excel instead.对不起,我不知道如何在文本中澄清,所以,我将向您展示我需要通过 Excel 实现的目标。

  1. Select multiple sheet by Ctrl+Left click (To make Sheet1, Sheet2 active)通过 Ctrl+左键单击选择多个工作表(使工作表 1、工作表 2 处于活动状态)

在此处输入图片说明

  1. Right click > Move or Copy... > select (move to end) > check "Creat a copy" > Ok右键单击> 移动或复制...> 选择(移至结尾)> 选中“创建副本”> 确定

在此处输入图片说明

  1. Result结果

在此处输入图片说明

I would like to know 2 things我想知道两件事

  1. How to select multiple sheets如何选择多个工作表
  2. How to copy selected sheets to the same workbook in 1 shot如何一次将选定的工作表复制到同一工作簿

The reason why I want to copy both in 1 shot because I put formula in Sheet1 in cell A1 which is "=Sheet2!A1".我想一次复制两者的原因是因为我将公式放在单元格 A1 中的 Sheet1 中,即“=Sheet2!A1”。 If I copy it one by one the formula won't change but If I select both sheets and copy at the same time, the formula will change to "=Sheet2(2)!A1" automatically << This is what I intend to do.如果我一一复制,公式不会改变,但如果我同时选择两个工作表并复制,则公式将自动更改为“=Sheet2(2)!A1”<<这就是我打算做的.

Which excel interop command should I use to achieve this result ?我应该使用哪个 excel interop 命令来实现这个结果?

I think this is what you need:我认为这就是你需要的:

            var MyApp = new Excel.Application();
            MyApp.Visible = false;
            var MyBook = MyApp.Workbooks.Open(path);

            Excel.Worksheet sheet = (Excel.Worksheet)MyBook.Sheets[1];
            sheet.Copy(MyBook.Sheets[MyBook.Sheets.Count]);

            sheet = (Excel.Worksheet)MyBook.Sheets[2];
            sheet.Copy(MyBook.Sheets[MyBook.Sheets.Count]);

            MyBook.Save();

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

相关问题 C# - 如何同时执行多个 Web 请求 - C# - how to do multiple web requests at the same time 如何在控制台应用程序中同时写多个位置? C# - How to write in multiple positions in a console application at the same time? C# 使用C#将多个工作表导出到同一Excel文件中 - Export multiple sheets in same Excel file using C# C# - 同时声明多个数组 - C# - Declaring multiple arrays at the same time C#同时使用多个tcpclients - C# multiple tcpclients at the same time C#Excel:以编程方式打开和复制工作簿,而忽略依赖项 - C# Excel: Programmatically open and copy workbook while ignoring dependency C#进程在控制台中查看输出并同时获取它的副本 - C# process see output in console and get a copy of it at the same time 如何选择同时显示的所有值删除内容并使用Selenium C#输入新值 - How to select all value present at the same time delete the content and enter new value using selenium c# 如何同时发送多行到mysql(如批量复制)? - how to send multiple row to mysql at same time(like bulk copy)? c# openfiledialog 以与用户选择的顺序相同的顺序选择多个文件 - c# openfiledialog to select multiple files with same order as user selects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM