0

I am creating an .xls excel file using apache poi. I need to set the Page Layout view by default. I am not able to find an api to do this.

I did look at a related question on .xlsx files - Set page view mode in excel file using apache poi

But i didn't find an equivalent way for .xls files.

Pls help.

Thanks.

Community
  • 1
  • 1

1 Answers1

0

You can do this as follows. Using Excel, create an empty workbook with a single sheet. While viewing this workbook using Excel, set the view to Page Layout, then save the empty excel spreadsheet to a file e.g. "empty.xls". Next, in your Java program, instead of creating a workbook from scratch, read the empty file as your starting point as in the example below.

    FileInputStream fis = new FileInputStream("empty.xls");
    Workbook wb = new HSSFWorkbook(fis);
    Sheet sheet = wb.getSheetAt(0);
    Row row = sheet.createRow(1);
    Cell cell = row.createCell(3);
    cell.setCellValue("You will see me in Page Layout View");
    FileOutputStream out = new FileOutputStream(new File("test.xls"));
    wb.write(out);
    out.close();

When you open up the Excel file "test.xls" it will already be in Page Layout view mode.

Wee Shetland
  • 955
  • 8
  • 18