How do I merge multiple excel files to a single excel file

后端 未结 3 1415
情话喂你
情话喂你 2021-01-23 13:26

So I was trying to make an excel sheet aggregator. In my line of work we get people who send us a bunch of individual excel files that are all related each with only 1 sheet use

3条回答
  •  生来不讨喜
    2021-01-23 14:14

    The simplest way to merge worksheets into one is through a third part component called Spire.Xls. It’s a standalone .NET component.

    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");
        }
        }
    }
    

提交回复
热议问题