Unire file
Introduzione
Aspose.Cells fornisce diverse modalità per unire i file. Per file semplici con dati, formattazione e formule, può essere utilizzato il metodo Workbook.Combine() per combinare diversi diari di lavoro e il metodo Worksheet.Copy() per copiare i fogli di lavoro in un nuovo diario 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 unire diversi file.
Unire file utilizzando Aspose.Cells
Il seguente esempio di codice illustra come unire file di grandi dimensioni utilizzando il metodo CellsHelper.MergeFiles. Prende due file semplici ma di grandi dimensioni, Book1.xls e Book2.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-.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"); |