[英]How to copy an excel sheet from workbook A to workbook B
I have the below code to copy Sheet1 from a workbook (test1.xlsx) to another workbook (test2.xlsx).我有以下代码将 Sheet1 从工作簿 (test1.xlsx) 复制到另一个工作簿 (test2.xlsx)。 The code doesn't have any errors and it takes forever to execute and I had to stop the code with no change in the files.代码没有任何错误,执行需要很长时间,我不得不停止代码而不更改文件。 Please let me know what is wrong.请让我知道出了什么问题。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
namespace Read_from_Excel_file
{
class Program
{
static void Main(string[] args)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook test1 = xlApp.Workbooks.Open(@"C:\Users\namokhtar\Desktop\test1.xlsx");
Excel.Workbook test2 = xlApp.Workbooks.Open(@"C:\Users\namokhtar\Desktop\test2.xlsx");
test2.Worksheets.Copy(test1.Worksheets["Sheet1"]);
test2.Save();
test1.Close();
test2.Close();
xlApp.Quit();
}
}
}
I think you need to specify where to copy the worksheet to.我认为您需要指定将工作表复制到的位置。 So at line所以在线
test2.Worksheets.Copy(test1.Worksheets["Sheet1"]);
you have to specify which worksheet in test2 you want to copy the worksheet in. test2.Worksheets["whateverworksheetyouwanttooverwrite"].Copy(test1.Worksheets["Sheet1"]);
您必须指定 test2 中的哪个工作表要复制该工作表。 test2.Worksheets["whateverworksheetyouwanttooverwrite"].Copy(test1.Worksheets["Sheet1"]);
Source: ( https://docs.microsoft.com/nl-nl/visualstudio/vsto/how-to-programmatically-copy-worksheets?view=vs-2017 )来源:( https://docs.microsoft.com/nl-nl/visualstudio/vsto/how-to-programmatically-copy-worksheets?view=vs-2017 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.