将多个工作表合并为单个工作表
Contents
[
Hide
]
有时候,您需要将多个工作表合并成一个工作表。通过使用 Aspose.Cells API,可以轻松实现这一目标。本文将展示一个代码示例,该示例读取源工作簿,并将所有源工作表的数据合并到目标工作簿中的一个工作表中。
以下代码片段向您展示了如何将多个工作表合并为单个工作表。
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
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string filePath = dataDir+ "SampleInput.xlsx"; | |
Workbook workbook = new Workbook(filePath); | |
Workbook destWorkbook = new Workbook(); | |
Worksheet destSheet = destWorkbook.Worksheets[0]; | |
int TotalRowCount = 0; | |
for (int i = 0; i < workbook.Worksheets.Count; i++) | |
{ | |
Worksheet sourceSheet = workbook.Worksheets[i]; | |
Range sourceRange = sourceSheet.Cells.MaxDisplayRange; | |
Range destRange = destSheet.Cells.CreateRange(sourceRange.FirstRow + TotalRowCount, sourceRange.FirstColumn, | |
sourceRange.RowCount, sourceRange.ColumnCount); | |
destRange.Copy(sourceRange); | |
TotalRowCount = sourceRange.RowCount + TotalRowCount; | |
} | |
dataDir = dataDir + "Output.out.xlsx"; | |
destWorkbook.Save(dataDir); |