如何将多个Excel文件合并到一个Excel文件中

时间:2014-12-04 03:12:48

标签: c# excel office-interop excel-interop

所以我试图制作一个excel表聚合器。在我的工作中,我们得到了一些人,他们向我们发送了一堆个人excel文件,这些文件都是相关的,每个文件只使用了一张。

我在某种程度上追随this previous post's ideas。但在这样做的时候,我复制的一些excel表格空白了。只有某些。我不知道为什么有些人是空白而其他人都没事。

以下是我用来打开和复制excel文件的代码

         OpenFileDialog browse = new OpenFileDialog();
            browse.Multiselect = true;
            DialogResult result = browse.ShowDialog();

            if (result == DialogResult.OK)

                try //try to open it. If its a proper excel file
                {   
                    excel = new Excel.Application();
                    excel.Workbooks.Add("");
                    finalized = excel.Workbooks[1];
                    excel.SheetsInNewWorkbook = 1;
                    for(int i=0; i< browse.FileNames.Length; i++)
                    {
                        excel.Workbooks.Add(browse.FileNames[i]);
                    }
                    //skip the first workbook as it is the finalized one
                    //also note everything in excel starts at 1 and not 0
                    for(int i=2; i<excel.Workbooks.Count; i++)
                    {
                        int count = excel.Workbooks[i].Worksheets.Count;
                        excel.Workbooks[i].Activate();
                        for (int j = 1; j < count; j++)
                        {

                            Excel._Worksheet pastee = (Excel._Worksheet)excel.Workbooks[i].Worksheets[j];
                            Excel._Worksheet sheet = (Excel._Worksheet)finalized.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                            //Excel._Worksheet sheet = finalized.Sheets[1];
                            pastee.Copy(Before: sheet);


                        }//end of for j
                    }//end of for i
                }//end of try

以下是我用来保存Excel文件的代码

            SaveFileDialog browse = new SaveFileDialog();
            browse.Title = "Save as Excel";
            browse.Filter = "Excel workbook | *.xlsx";
            DialogResult result = browse.ShowDialog();

            finalized.SaveAs(browse.FileName, Excel.XlFileFormat.xlWorkbookDefault);

            MessageBox.Show("Success", "Message");
            //unlock the file
            Global.releaseComObjects(finalized, excel);

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

在你的内循环中,你可以为你的最终版本添加一个新的工作表。工作簿(&#39; sheet&#39;)并为每个源表复制一个工作表。所以每张表格都是&#39;由Add命令创建的将为空,因为实际上您为每个源表创建了两个工作表。另一个问题是 - 正如你所提到的 - excel中的数组是基于1的;所以你必须循环到j <= count而不是j < count

所以我认为代码会更好用:

Excel.Worksheet dummy = finalized.Worksheets[1];

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

    for (int j = 1; j <= count; j++)
    {
        Excel._Worksheet pastee = (Excel._Worksheet)excel.Workbooks[i].Worksheets[j];
        pastee.Copy(dummy);
    }
}

dummy.Delete();

答案 2 :(得分:0)

将工作表合并为一个的最简单方法是通过名为Spire.Xls的第三方组件。它是一个独立的.NET组件。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Xls;
using System.Data;


namespace Spire.XLS
{
    class Program
    {
        static void Main(string[] args)
    {

        Workbook workbook = new Workbook();
        //load the first workbook
        workbook.LoadFromFile(@"merge1.xlsx");
        //load the second workbook
        Workbook workbook2 = new Workbook();
        workbook2.LoadFromFile(@"merge2.xlsx");

        //import the second workbook's worksheet into the first workbook using a datatable
        Worksheet sheet2 = workbook2.Worksheets[0];
        DataTable dataTable = sheet2.ExportDataTable();
        Worksheet sheet1 = workbook.Worksheets[0];
        sheet1.InsertDataTable(dataTable, false, sheet1.LastRow + 1, 1);


        //save the workbook
        workbook.SaveToFile("result.xlsx");
    }
    }
}