Bug 54567

Summary: Set more than one print area to the same sheet
Product: POI Reporter: Samuele <samuele.bagatin>
Component: HSSFAssignee: POI Developers List <dev>
Status: RESOLVED WORKSFORME    
Severity: normal CC: dominik.stadler
Priority: P2    
Version: 3.8-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   

Description Samuele 2013-02-15 17:28:12 UTC
It seems impossible to have more than one printArea in the same sheet. I tried in different way, and I don't found a way to set manually a print layout.
Comment 1 Samuele 2013-02-15 17:29:29 UTC
I'm using apache poi 3.8, and I don't find anything that help me to set 3 print layout pages
Comment 2 Mark B 2013-02-16 11:15:20 UTC
From your bug report, I am not clear about exactly what you are reporting - in the first post you take of print areas, in the second print layout. However,  I am going to assume that you are referring to a problem setting more than one print area on a sheet and have to say that I think this should be a feature request or enhancement and not a bug report.

If you use Excel itself, you should find that it is possible to create just a single print area on each worksheet. What Excel does allow you to do, however, is to add additional areas to an existing print area and this is why I think it should be a feature request and not a bug report. The API will need to be modified so that the setPrintArea() methods return an object that allows you to add other cell ranges to the print area, and a new method added to the Workbook class so that it is possible to get a reference to the print area object (if any) for a specific sheet.

In the meantime, can I ask which version of the file format are you targetting? The older binary (.xls) format or the newer OOXML (.xlsx) file format? If the latter, it may be possible to dig down into the openxml4j layer that underpins POI and work with the objects there. Having said that, I do not know if there is a class to model the print area at that level yet but do suspect there may be.
Comment 3 Mark B 2013-02-16 17:45:04 UTC
Assuming you were asking how to set multpile print areas on a sheet, tehn it is possible to do so using the current api. Try something like this;

import java.io.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

/**
 *
 * @author Mark Beardsley
 */
public class TestPrintArea {
    
    private static final int MAX_ROWS = 50;
    private static final int MAX_COLS = 15;
    
    public TestPrintArea(String filename) throws FileNotFoundException, IOException {
        Workbook workbook = null;
        Sheet sheet = null;
        Row row = null;
        Cell cell = null;
        FileOutputStream fos = null;
        try {
            if(filename.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook();
            }
            else {
                workbook = new HSSFWorkbook();
            }
            
            sheet = workbook.createSheet("To Test Print Area.");
            
            for(int i = 0; i < MAX_ROWS; i++) {
                row = sheet.createRow(i);
                
                for(int j = 0; j < MAX_COLS; j++) {
                    cell = row.createCell(j);
                    cell.setCellValue((i + 1) * (j + 1));
                }
            }
            
            // Use a comma to separate the ramge for each print area
            workbook.setPrintArea(0, "$A$1:$B$2,$D$1:$H$3");
            
            fos = new FileOutputStream(filename);
            workbook.write(fos);
        }
        finally {
            if(fos != null) {
                try {
                    fos.close();
                    fos = null;
                }
                catch(IOException ioEx) {
                    // I G N O R E
                }
            }
        }
    }
}

Do not know if there is a limit to the number of print areas that can be set this way.
Comment 4 Dominik Stadler 2013-05-30 16:20:31 UTC
Based on last comment it seems to be resolved, reporter did not reply for more than three months, so assuming this is report is done...