ファイルの結合
紹介
Aspose.Cellsでは、ファイルを結合するための異なる方法が提供されています。データ、書式、数式を含む単純なファイルの場合は、Workbook.Combine()メソッドを使用して複数のブックを結合し、Worksheet.Copy()メソッドを使用してワークシートを新しいブックにコピーすることができます。これらの方法は使いやすく効果的ですが、多くのファイルを結合する場合は、多くのシステムリソースを消費する可能性があります。このような場合は、より効率的な方法であるCellsHelper.MergeFiles静的メソッドを使用します。
Aspose.Cellsを使用してファイルをマージする
次のサンプルコードは、CellsHelper.MergeFilesメソッドを使用して大きなファイルを結合する方法を説明しています。単純ながらも大きなBook1.xlsとBook2.xlsという2つの大きなファイルが含まれています。これらのファイルには、フォーマット済みのデータと数式のみが含まれています。
// 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); | |
// Create an Array (length=2) | |
string[] files = new string[2]; | |
// Specify files with their paths to be merged | |
files[0] = dataDir + "Book1.xls"; | |
files[1] = dataDir + "Book2.xls"; | |
// Create a cachedFile for the process | |
string cacheFile = dataDir + "test.txt"; | |
// Output File to be created | |
string dest = dataDir + "output.xlsx"; | |
// Merge the files in the output file. Supports only .xls files | |
CellsHelper.MergeFiles(files, cacheFile, dest); | |
// Now if you need to rename your sheets, you may load the output file | |
Workbook workbook = new Workbook(dataDir + "output.xlsx"); | |
int i = 1; | |
// Browse all the sheets to rename them accordingly | |
foreach (Worksheet sheet in workbook.Worksheets) | |
{ | |
sheet.Name = "Sheet1" + i.ToString(); | |
i++; | |
} | |
// Re-save the file | |
workbook.Save(dataDir + "output.xlsx"); |