You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Jason Vincent <jt...@gmail.com> on 2008/08/28 18:56:15 UTC

printing issue: Landscape, fixed width, repeating rows

Hi all.

I'm having a printing issue with my application.  I've gotten the
landscape and 1 page fixed width printing to work.  As soon as I try
to set some repeating Rows and Columns the print, rendering gets all
messed up.  In Excel, the print setup and preview appears to still be
in landscape mode, but the content of the page is scaled down and
rotated into portrait mode.

Below is a sample application that demonstrates what I'm seeing.  The
file written to landscapeTest_noRepeat.xls is rendered as I would
expect without the first two rows getting repeated on page 2.
landscapeTest_withRepeat.xls is the file that now contains the
rendering issue.

I've seen occasional rendering differences when I change the order in
which certain settings were set.  I believe I've tried all the
permutations with this example; perhaps I missed one.

Thanks in advance.
Jason.

<pre>
package com.vincent.poi;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.Region;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Test app to demonstrate issue with repeating rows with landscape
 * printing with fixed to 1 page width printing.
 */
public class LandscapeSinglePageTest {

    private static final Log LOGGER =
LogFactory.getLog(LandscapeSinglePageTest.class);
    private static HSSFWorkbook wb;
    private static final int ROW_COUNT = 150;
    private static final int COLUMN_COUNT = 30;


    public static void main(String[] args) {
        try {
            wb = new HSSFWorkbook();
            HSSFSheet sheet = wb.createSheet();

            HSSFRow row1 = sheet.createRow(0);
            HSSFCell cell1 = row1.createCell((short) 0);
            cell1.setCellValue(new HSSFRichTextString("This is line 1
to repeat."));

            HSSFRow row2 = sheet.createRow(1);
            HSSFCell cell2 = row2.createCell((short) 0);
            cell2.setCellValue(new HSSFRichTextString("This is line 2
to repeat."));

            sheet.createRow(2);

            sheet.addMergedRegion(new Region(0, (short) 0, 0, (short)
(COLUMN_COUNT - 1)));
            sheet.addMergedRegion(new Region(1, (short) 0, 1, (short)
(COLUMN_COUNT - 1)));

            for (int i = 3; i < ROW_COUNT; i++) {
                HSSFRow row = sheet.createRow(i);
                for (short j = 0; j < COLUMN_COUNT; j++) {
                    HSSFCell cell = row.createCell(j);
                    cell.setCellValue(i * j);
                }
            }

            sheet.setHorizontallyCenter(true);
            sheet.setAutobreaks(true);

            HSSFPrintSetup printSetup = sheet.getPrintSetup();
            printSetup.setFitWidth((short) 1);
            printSetup.setFitHeight((short) 100);
            printSetup.setLandscape(true);

            File file = new File("landscapeTest_noRepeat.xls");
            writeFile(file);

            wb.setRepeatingRowsAndColumns(0, 0, COLUMN_COUNT, 0, 2);

            File file2 = new File("landscapeTest_withRepeat.xls");
            writeFile(file2);

        } catch (Exception e) {
            LOGGER.error("ahh...", e);
        }
    }

    private static void writeFile(File file) throws IOException {
        FileOutputStream out = new FileOutputStream(file);
        wb.write(out);
        out.flush();
        out.close();
        LOGGER.info("File written to :" + file.getAbsolutePath());
    }


}

</pre>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: printing issue: Landscape, fixed width, repeating rows

Posted by Nick Burch <ni...@torchbox.com>.
On Thu, 28 Aug 2008, Jason Vincent wrote:
> I'm having a printing issue with my application.  I've gotten the 
> landscape and 1 page fixed width printing to work.  As soon as I try to 
> set some repeating Rows and Columns the print, rendering gets all messed 
> up.  In Excel, the print setup and preview appears to still be in 
> landscape mode, but the content of the page is scaled down and rotated 
> into portrait mode.

I suspect this is a little used and poorly tested area of poi. It might be 
worth you producing three files:
* the closest to the print layout that you want, as produced by poi, that
   excel renders properly
* the same file, re-saved by poi with the print layout you want, which
   excel doesn't render correctly
* the same file, re-saved by excel with the print layout you want

Finally, use org.apache.poi.hssf.dev.BiffViewer to try to figure out what 
poi's doing differently to excel. Hopefully the poi fix will fall neatly 
out of that discovery :)

Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org