【问题标题】:How do I merge multiple excel files to a single excel file如何将多个excel文件合并为一个excel文件
【发布时间】:2014-12-04 03:12:48
【问题描述】:

所以我试图制作一个 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);

【问题讨论】:

  • 你到底想做什么?您想复制非空白的 excel 文件,还是复制时正在获取 excel 文件? @Alexander Ryan Baggett
  • 正如我试图在描述中解释的那样。我想打开多个excel文件。将这些文件中的每张工作表复制到 1 个新的 Excel 文件中。
  • 我打开的文件的excel表格不是空白的。
  • 实际上找到了一个免费的插件,效果很好:rondebruin.nl/win/addins/rdbmerge.htm

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


【解决方案1】:

在您的内部循环中,您将一个新的工作表添加到您的“最终”工作簿(“工作表”)中,并为每个源工作表复制一个工作表。因此,Add 命令创建的每个“工作表”都将为空,因为实际上您为每个源工作表创建了两个工作表。另一个问题是,正如您所提到的,excel 中的数组是基于 1 的;所以你必须循环到j &lt;= count 而不是j &lt; 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();

【讨论】:

  • 我试过了,它似乎只复制文本 - 没有任何样式/字体/颜色等可能是 Excel.Interop 版本的问题,我不确定。
【解决方案2】:
【解决方案3】:

将工作表合并为一个的最简单方法是通过名为 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");
    }
    }
}

【讨论】:

  • 不购买 Spire.Xls 限制为 3 页
猜你喜欢
  • 2015-01-12
  • 2020-06-12
  • 2020-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多