Microsoft® Excel 文件合并 via .NET
使用 C# 代码将 2 个或多个 Excel 文件合并到一个电子表格中
.NET Excel 库 提供多种方式将工作簿与各种类型的内容(如公式、数据、图像、图表等)组合到单个电子表格文件中。支持的文件格式包括 XLS、XLSX、XLSB、XLT、XLTX、XLTM、ODS、CSV、TSV 等。
将 Excel 文件与图像和图表合并
组合 2 个包含图像和图表的 Excel 文件的最简单方法是调用 工作簿.合并 方法。它允许将类似类型的 Excel 文件合并到单个电子表格中。
C# 合并Excel文件的代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// load first Excel file | |
var book1 = new Aspose.Cells.Workbook("withCharts.xlsx"); | |
// load second Excel file into a separate instance | |
var book2 = new Aspose.Cells.Workbook("withImages.xlsx"); | |
// combine two workbooks | |
book1.Combine(book2); | |
// save the target workbook | |
book1.Save("combined.xlsx"); |
合并多个 Excel 文件
CellsHelper.MergeFiles 方法支持将Excel文件的数据、样式和公式合并到相同格式的新电子表格中。这是使用缓存合并多个文件的有效方法。
C# 合并多个Excel文件的代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create an Array (length=2) | |
String[] files = new String[2]; | |
// specify file paths to be merged | |
files[0] = "Book1.xls"; | |
files[1] = "Book2.xls"; | |
// merge the files to save the result | |
Aspose.Cells.CellsHelper.MergeFiles(files, "cache", "merged.xls"); |
通过复制工作表合并 Excel 文件
工作表.副本 可用于将数据和格式从源工作表复制到工作簿内或工作簿之间的另一个工作表。该方法将源工作表对象作为参数。
C# 跨 Excel 文件复制工作表的代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// load spreadsheet files into 2 instances of Workbook | |
var book1 = new Aspose.Cells.Workbook("input.xlsx"); | |
var book2 = new Aspose.Cells.Workbook("input.ods"); | |
// loop over the worksheet collection | |
foreach (var sheet in book1.Worksheets) | |
{ | |
// add a blank worksheet | |
book2.Worksheets.Add(sheet.Name); | |
// copy worksheet from source to target | |
book2.Worksheets[sheet.Name].Copy(sheet); | |
} | |
// Save the file in any spreadsheet format | |
book2.Save("combined.xls", Aspose.Cells.SaveFormat.Auto); |