Copy excel is not working properly if the source excel have more than one pivot sheet

Hi Team,

Good Morning!!!

I am copying an excel file having multiple pivot sheets, total sheets and other data sheets into another destination excel. Observed that the destination excel is created but it got corrupted. Because when try to open MS Excel was saying “found problem with content and try to recover” kind of message. And i clicked on “yes” it opened and there was a pop up saying that some issue with the PivotTable and was discarded. Can you please help me to resolve this issue. And one more thing was, its been observed if the source excel have more than one pivot sheet. In case if the source excel have only one pivot sheet then it was fine. I am enclosing the sample code, source excel and generated destination excel please help me as early as possible.

Sample code:

package com.example.demo.totals;

import java.io.IOException;
import java.io.InputStream;

import com.aspose.cells.License;
import com.aspose.cells.PivotField;
import com.aspose.cells.PivotFieldCollection;
import com.aspose.cells.PivotTable;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;

public class CopyExcel {

public static void main(String[] args) {
	setAsposeExcelLicense();
	
			// Instantiate a new workbook
			Workbook sourceWb = null;
			Workbook distWb = null;
			try {
				sourceWb = new Workbook("31884.xls");
				distWb = new Workbook();
				distWb.getWorksheets().removeAt(0);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			for (int i = 0; i < sourceWb.getWorksheets().getCount(); i++) {
				Worksheet sourceSheet = sourceWb.getWorksheets().get(i);
				String sheetName = sourceSheet.getName();
					copySheet(distWb, sourceSheet, true);
				
			}

			distWb.calculateFormula(true);
			try {
				distWb.save("31884-dist.xls");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			
			// Print message
			System.out.println("Process completed successfully");

}


private static License setAsposeExcelLicense() {
	License license = new License();
	try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("Aspose_Total.lic")) {
		license.setLicense(is);
	} catch (IOException e) {
		e.printStackTrace();
	}
	return license;
}

private static Worksheet copySheet(Workbook destinationWb, Worksheet sourceSheet, boolean hide) {
	Worksheet destinationSheet = destinationWb.getWorksheets().add(sourceSheet.getName());
	try {
		destinationSheet.copy(sourceSheet);
		
		destinationSheet.refreshPivotTables();
	} catch (Exception e) {
		e.printStackTrace();
	}
	destinationSheet.setVisible(hide);
	return destinationSheet;
}

}

excel copy pivottable issue.zip (240.8 KB)

@koteswaragunda
By testing with sample files and code on the latest version v24.10, we were able to open the result file normally without any exceptions. Please refer to the attachment. 31884-dist_out.zip (42.6 KB)

We recommend you to kindly try using our latest version: Aspose.Cells for Java 24.10.

@John.He no other option available other than migrating to latest version? because its a decision and a process needs to be involved by the management so asking any alternatives for this issue we will definitely migrate to latest version in near future.

@koteswaragunda
Sorry, there is currently no alternative solution. if you keep using an older version, it won’t help you much. We couldn’t use the old version to evaluate the issue, nor could the old version include fixes or enhancements. All enhancements and fixes are based only on the latest API set. We’ve been extending the API over the years, so don’t try to trace the release notes to see where name definition issues were first fixed.

In short, try to upgrade to the latest version and use it. Please let us know if you still find any issues. We will check it shortly.