简体   繁体   中英

Alternative way of merging excel worksheets in c#

I am wondering if there is another way of merging several excel worksheets into one without using Microsoft.Office.Interop.Excel; namespace.

I am currently using C# for this and will need to find an alternative way in C#.

The reason for it, is out of my control but i was wondering if there is another way of doing this without it.

I am open to ideas and any help what so ever.

Below is the C# so far (fully working)

using Microsoft.Office.Interop.Excel;

namespace MergeWorkBooks
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            string mergedWorkbookName = "MergedWorkbook.xlsx";
            string[] sourceFilePaths = new string[] { "STORE_OPS_00_COVER.xlsx", "STORE_OPS_01_STOCK_ACCOUNT_MOVEMENT_REPORT.xlsx", "STORE_OPS_02_SALES_SUMMARY.xlsx", "STORE_OPS_03_DISPATCHES.xlsx" };
            string destinationFilePath = @"C:\Users\bb\Documents\" + mergedWorkbookName;

            MergeWorkbooks(destinationFilePath, sourceFilePaths);
        }

        public static void MergeWorkbooks(string destinationFilePath, params string[] sourceFilePaths)
        {
            var app = new Application();
            app.DisplayAlerts = false; // No prompt when overriding

            // Create a new workbook (index=1) and open source workbooks (index=2,3,...)
            Workbook destinationWb = app.Workbooks.Add();
            foreach (var sourceFilePath in sourceFilePaths)
            {
                app.Workbooks.Add(sourceFilePath);
            }

            // Copy all worksheets
            Worksheet after = destinationWb.Worksheets[1];
            for (int wbIndex = app.Workbooks.Count; wbIndex >= 2; wbIndex--)
            {
                Workbook wb = app.Workbooks[wbIndex];
                for (int wsIndex = wb.Worksheets.Count; wsIndex >= 1; wsIndex--)
                {
                    Worksheet ws = wb.Worksheets[wsIndex];
                    ws.Copy(After: after);
                }
            }

            // Close source documents before saving destination. Otherwise, save will fail
            for (int wbIndex = 2; wbIndex <= app.Workbooks.Count; wbIndex++)
            {
                Workbook wb = app.Workbooks[wbIndex];
                wb.Close();
            }

            // Delete default worksheet
            after.Delete();

            // Save new workbook
            destinationWb.SaveAs(destinationFilePath);
            destinationWb.Close();

            app.Quit();
        }
    }
}

I don't know the exact code, but I'm pretty sure that EPPlus ( http://epplus.codeplex.com/ , or via nuget) would be able to do what you are wanting. I've never actually used it to load excel files, though know you can, but you can copy worksheets from other worksheets.

EPPlus does not require Excel installed on the machine like interop does, and also does not as much cleanup as it is a C# library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM