【问题标题】:How to merge two excel files into one with their sheet names?如何将两个带有工作表名称的excel文件合并为一个?
【发布时间】:2011-09-27 11:49:44
【问题描述】:

为了合并两个excel表,我使用下面的代码。

using System;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection; 

namespace MergeWorkBooks
{
    class Program
    {
        static void Main(string[] args)
        {
            Excel.Application app = new Excel.Application();

            app.Visible = true;
            app.Workbooks.Add("");
            app.Workbooks.Add(@"c:\MyWork\WorkBook1.xls");
            app.Workbooks.Add(@"c:\MyWork\WorkBook2.xls");


            for (int i = 2; i <= app.Workbooks.Count; i++)
            {
                int count = app.Workbooks[i].Worksheets.Count;

                app.Workbooks[i].Activate();
                for (int j=1; j <= count; j++)
                {
                    Excel._Worksheet ws = (Excel._Worksheet)app.Workbooks[i].Worksheets[j];
                    ws.Select(Type.Missing);
                    ws.Cells.Select();

                    Excel.Range sel = (Excel.Range)app.Selection;
                    sel.Copy(Type.Missing);

                    Excel._Worksheet sheet = (Excel._Worksheet)app.Workbooks[1].Worksheets.Add(
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing
                    );

                    sheet.Paste(Type.Missing, Type.Missing);

                }
            }
        }
    }
}

此代码对我合并 Excel 工作簿很有用。但在合并时,我没有得到 excel 工作表名称。在这里我需要当excel同时合并时,工作表名称也应该转到合并的excel工作表。

【问题讨论】:

    标签: c# excel excel-interop


    【解决方案1】:

    以下内容对我来说很好,包括复制名称和名称冲突的地方,甚至处理 Sheet1(2) 等。

    Excel.Application app = new Excel.Application();
    app.Visible = true;
    app.Workbooks.Add("");
    app.Workbooks.Add(@"c:\MyWork\WorkBook1.xls");
      app.Workbooks.Add(@"c:\MyWork\WorkBook2.xls");
    for (int i = 2; i <= app.Workbooks.Count; i++)
    {
        for (int j = 1; j <= app.Workbooks[i].Worksheets.Count;j++ )
        {
            Excel.Worksheet ws = app.Workbooks[i].Worksheets[j];
            ws.Copy(app.Workbooks[1].Worksheets[1]);
        }
    }
    

    【讨论】:

    • 当我要运行它时,我遇到了错误。编译器错误消息:CS0266:无法将类型“object”隐式转换为“Microsoft.Office.Interop.Excel.Worksheet”。存在显式转换(您是否缺少演员表?)而且它还说“方法'Copy'没有重载需要'1'参数”
    • 嗯,除了要合并的 2 个工作簿的名称之外,这就是我的整个应用程序。我猜他们当时在 Office2010 中更改了它。
    • @TripatiSubudhi 您最终找到解决方案了吗?只需使用 Excel.Worksheet ws = (Excel.Worksheet) app.Workbooks[i].Worksheets[j]; ?
    【解决方案2】:

    无错误并改进答案

    在同一位置创建 result2.xlsx 文件,您可以根据需要找到最终的 excel 表格

     class Program
         {        
          static void Main(string[] args)
            {
            Application app = new Application();
            app.Visible = true;
            Workbook w1 = app.Workbooks.Add(@"D:\MyDownload\result2.xlsx");
            Workbook w2 = app.Workbooks.Add(@"D:\MyDownload\merge1.xlsx");
            Workbook w3 = app.Workbooks.Add(@"D:\MyDownload\merge2.xlsx");
            for (int i = 2; i <= app.Workbooks.Count; i++)
            {
                for (int j = 1; j <= app.Workbooks[i].Worksheets.Count; j++)
                {
                    Worksheet ws = (Worksheet)app.Workbooks[i].Worksheets[j];
                    ws.Copy(app.Workbooks[1].Worksheets[1]);
                }
            }
            app.Workbooks[1].SaveCopyAs(@"D:\MyDownload\result2.xlsx");
            w1.Close(0);
            w2.Close(0);
            w3.Close(0);
            app.Quit();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-09-15
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-13
      • 1970-01-01
      相关资源
      最近更新 更多