ファイルをマージ

序章

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");