【发布时间】:2011-04-18 00:38:05
【问题描述】:
我需要将工作表从一个工作簿复制到另一个工作簿,但我有点卡住了。前提是我有一个“主”工作簿,其中存储了许多报告的模板,然后我需要创建特定工作表的空白副本并将其添加到新工作簿中。
这是我目前所拥有的:
private void CreateNewWorkbook(Tables table)
{
Excel.Application app = null;
Excel.Workbook book = null;
Excel.Worksheet sheet = null;
try
{
string startPath = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
string filePath = System.IO.Path.Combine(startPath, "sal1011forms.xls");
Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
app = new Excel.Application();
book = app.Workbooks.Open(filePath);
sheet = (Excel.Worksheet)book.Worksheets.get_Item((int)table + 1);
sfd.AddExtension = true;
sfd.FileName = table.ToString() + ".xls";
sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (sfd.ShowDialog() == true)
{
sheet.SaveAs(sfd.FileName);
}
}
finally
{
if (book != null)
{
book.Close();
}
if (app != null)
{
app.Quit();
}
this.ReleaseObject(sheet);
this.ReleaseObject(book);
this.ReleaseObject(app);
}
}
目前我遇到的唯一问题是,当我在工作表上调用 .Save() 时,它会将原始工作簿中的所有工作表保存到新工作簿中。有关如何纠正此问题的任何想法?
提前致谢,
桑尼
【问题讨论】:
-
您好,您在脚本组件中使用了哪些引用?