如何利用c#控制台应用程序将多个excel文件合并成一个excel文件?

1、利用c#将多个excel文件合并成一个excel文件
2、最好源程序,在网上下载的就算了。
3、悬赏20c币,各位大神显身手。

图片说明

其它人如果也需要:https://download.csdn.net/download/caozhy/11173400

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel;

namespace Q760776
{
    class Program
    {
        static void Main(string[] args)
        {
            string file1 = @"C:\Users\caozh\Desktop\book1.xls";
            string file2 = @"C:\Users\caozh\Desktop\book2.xls";
            string output = @"C:\Users\caozh\Desktop\book3.xls";
            File.Copy(file1, output, true);
            Excel.Application app1 = new Excel.Application();
            Excel.Workbook wb1 = app1.Workbooks.Open(output);
            Excel.Worksheet ws1 = (Excel.Worksheet)wb1.Sheets[1];
            Excel.Application app2 = new Excel.Application();
            Excel.Workbook wb2 = app2.Workbooks.Open(file2);
            Excel.Worksheet ws2 = (Excel.Worksheet)wb2.Sheets[1];
            try
            {
                int offset = ws1.UsedRange.Rows.Count;
                for (int i = 0; i < ws2.UsedRange.Rows.Count; i++)
                {
                    for (int j = 0; j < ws2.UsedRange.Columns.Count; j++)
                    {
                        var id1 = ((char)('A' + j)).ToString() + (i + 1 + offset).ToString();
                        var id2 = ((char)('A' + j)).ToString() + (i + 1).ToString();
                        ws1.Cells.Range[id1].Value2 = ws2.Cells.Range[id2].Value2;
                    }
                }
            }
            finally
            {
                wb1.Save();
                wb1.Close();
                app1.Quit();
                wb2.Close();
                app2.Quit();
                Process.GetProcessesByName("excel").Select(x => { x.Kill(); return 0; });
            }
        }
    }
}

这个如果自己来开发很麻烦,因为Excel本身是一对XML组成的zip格式文件(把Excel解压缩之后可以看到里面是一堆的XML)。
Excel本身有一套自己的协议规范,如果要去合并多个Excel文件,就需要修改其中的xml并且要符合Excel本身的协议规范(openxml)
自己来实现是比较复杂的,推荐找一些第三方实现的控件或者组件,在控件和组件里面本身实现功能的基础上的来做会比较好。
这方面做得好的组件比如Spread.Net系列等。JAVA这边也有,例如POI等

我这边对word/excel之类的操作都是用第三方的,aspose.words.dll 和 aspose.cells.dll,非常好用,能快速上手,建议可以尝试。

确实使用第三方控件会方便一些,我就用的Spire.xls for .net。代码操作简单,实现效果不错,有需要的可以试试。这是他们官网上的相关教程,可以参考里面的代码示例。https://www.e-iceblue.cn/document/merge-excel-files.html