在c#中合并excel工作表的另一种方法

我想知道是否有另一种方式合并几个Excel工作表成一个没有使用Microsoft.Office.Interop.Excel; 命名空间。

我目前正在使用C#为此,将需要在C#中find一种替代方法。

究其原因,是我无法控制的,但我想知道如果没有它,还有另外一种方法。

我对任何想法和任何帮助都是开放的。

下面是C#到目前为止(完全工作)

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

我不知道确切的代码,但是我很确定EPPlus( http://epplus.codeplex.com/ ,或者通过nuget)可以做你想做的事情。 我从来没有真正用它来加载Excel文件,虽然知道你可以,但你可以从其他工作表复制工作表。

EPPlus不需要像interop那样在机器上安装Excel,也不需要像C#库那样进行清理。