【发布时间】:2015-08-09 03:22:04
【问题描述】:
我正在尝试将现有 Excel 文件中的工作表复制到我当前的工作簿中。我有一个带有以下代码的 Excel 插件:
string wsName = "template." + ((System.Windows.Forms.Button)sender).Tag + "." + ((System.Windows.Forms.Button)sender).Text;
Microsoft.Office.Interop.Excel.Application x = new Microsoft.Office.Interop.Excel.Application();
x.Visible = false; x.ScreenUpdating = false;
x.Workbooks.Open(Properties.Settings.Default.TemplatePath);
try
{
foreach (Worksheet w in x.Worksheets)
if (w.Name == wsName)
w.Copy(Type.Missing, Globals.ThisAddIn.Application.Workbooks[1].Worksheets[1]);
}
catch
{ }
finally
{
x.DisplayAlerts = false; x.Workbooks.Close(); x.DisplayAlerts = true; // close application with disabled alerts
x.Quit(); x.Visible = true; x.ScreenUpdating = true;
x = null;
}
我根据模板路径在后台打开第二个 excel 实例。我找到工作表 w。这一切都很好,但是,我试图将工作表复制到活动工作簿中我无法使其工作。 当前的错误是找不到方法 Copy ,但是当我删除参数时它执行得很好。所以我认为我的问题出在 Globals.ThisAddIn.Appllication.Workbooks[1] 中。评估该参数确实会返回在 AddIn 中处于活动状态的当前工作簿。
【问题讨论】: