我需要將工作表從一個工作簿複製到另一個工作表,並且我有點卡住了。前提是我有一個「主」工作簿,用於存儲多個報表的模板,然後需要創建特定工作表的空白副本並將其添加到新的工作簿中。C# - 如何將一個Excel工作表從一個工作簿複製到另一個?
這是我到目前爲止有:
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(),這樣可以節省一切從工作表原始工作簿放入新的工作簿中。任何想法如何糾正?
由於提前,
桑尼