相关疑难解决方法(0)

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)
        { …
Run Code Online (Sandbox Code Playgroud)

c# excel interop

10
推荐指数
3
解决办法
5万
查看次数

标签 统计

c# ×1

excel ×1

interop ×1