Unire file

Introduzione

Aspose.Cells fornisce modi diversi per unire file. Per file semplici con dati, formattazione e formule, è possibile utilizzare il metodo Workbook.combine() per combinare diversi fogli di lavoro, e il metodo Worksheet.copy() può essere utilizzato per copiare i fogli di lavoro in una nuova cartella di lavoro. Questi metodi sono facili da usare ed efficaci, ma se hai molti file da unire, potresti scoprire che richiedono molte risorse di sistema. Per evitare ciò, utilizza il metodo statico CellsHelper.mergeFiles, un modo più efficiente per unergi file vari.

Unire file utilizzando Aspose.Cells

Il seguente codice di esempio illustra come unire file di grandi dimensioni utilizzando il metodo CellsHelper.mergeFiles. Prende due file semplici ma di grandi dimensioni, MyBook1.xls e MyBook2.xls. I file contengono solo dati formattati e formule.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getSharedDataDir(MergeFiles.class) + "CellsHelperClass/";
// 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 + "MergeFiles_out.xls";
// Merge the files in the output file
CellsHelper.mergeFiles(files, cacheFile, dest);
// Now if you need to rename your sheets, you may load the output file
Workbook workbook = new Workbook(dataDir + "MergeFiles_out.xls");
int cnt = 1;
// Browse all the sheets to rename them accordingly
for (int i = 0; i < workbook.getWorksheets().getCount(); i++) {
workbook.getWorksheets().get(i).setName("Sheet1" + cnt);
cnt++;
}
// Re-save the file
workbook.save(dataDir + "MergeFiles1_out.xls");