Bug 53678

Summary: Clone Sheet with control form (Checkboxes)
Product: POI Reporter: Benjamin <benjamin.ruhlmann>
Component: XSSFAssignee: POI Developers List <dev>
Status: NEW ---    
Severity: major CC: benjamin.ruhlmann
Priority: P2    
Version: 3.8-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: Windows Vista   
Attachments: Excel file passed in argument
The original file with Form Control
The Cloned Document

Description Benjamin 2012-08-08 09:45:37 UTC
Created attachment 29185 [details]
Excel file passed in argument

When cloning a sheet with checkboxes in it (control form type), the resulting file is corrupted.

Step to reproduce : use this with the file attached
    public static void main(String[] args) throws FileNotFoundException, IOException {
        Workbook wb = new XSSFWorkbook(new FileInputStream(args[0]));
        wb.cloneSheet(0);
        FileOutputStream fileOut = new FileOutputStream(args[0].replace(".xlsx", "-edited.xlsx"));
        wb.write(fileOut);
        fileOut.flush();
        fileOut.close();
    }

result : the second sheet is corrupted and do not have the checkbox
Comment 1 Tom Cole 2013-03-08 15:09:30 UTC
Created attachment 30027 [details]
The original file with Form Control

This is the original file. The first sheet has conditional formatting, some VB code, a macro and a Form Control Button.
Comment 2 Tom Cole 2013-03-08 15:10:08 UTC
Created attachment 30028 [details]
The Cloned Document

This is the newly created file with the first sheet cloned.
Comment 3 Tom Cole 2013-03-08 15:15:07 UTC
I believe this condition exists with any Form Control. I have attached a workbook where the first sheet has a form control button on it. If you clone that sheet, the generated sheet has errors in it. If you remove the form control, the sheet clones just fine (even with conditional formatting).

I used the following program to clone the original:

public class ReadWriteTest {

	public ReadWriteTest(String input, String output) {
		try {
			File inputFile = new File(input);
			if (inputFile.exists()) {
				Workbook excel = new XSSFWorkbook(new FileInputStream(inputFile));
				Sheet newSheet = excel.cloneSheet(0);
				excel.setSheetName(excel.getSheetIndex(newSheet), "Clone");
				File outputFile = new File(output);
				boolean create = ! outputFile.exists();
				if (! create) {
					create = (JOptionPane.showConfirmDialog(null, "File exists, do you want to overwrite?") == JOptionPane.OK_OPTION);
				}
				if (create) {
					excel.write(new FileOutputStream(outputFile));
				}
				System.exit(0);
			}
			else {
				System.out.println("Input file " + input + " not found.");
				System.exit(1);
			}
		}
		catch(Exception ex) {
			ex.printStackTrace();
			System.exit(1);
		}
	}
	
	public static void main(String[] args) {
		if (args.length == 2) {
			new ReadWriteTest(args[0], args[1]);
		}
		else {
			System.out.println("USAGE: ReadWriteTest <input_file_name> <output_file_name>");
			System.exit(1);
		}
	}
}