Example usage for org.apache.poi.ss.usermodel Workbook setPrintArea

List of usage examples for org.apache.poi.ss.usermodel Workbook setPrintArea

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Workbook setPrintArea.

Prototype

void setPrintArea(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow);

Source Link

Document

For the Convenience of Java Programmers maintaining pointers.

Usage

From source file:net.sf.excelutils.WorkbookUtils.java

License:Apache License

/**
 * Set Print Area//from w  w  w. j ava2 s  .  c  om
 *
 * @param wb
 * @param sheetIndex
 */
public static void setPrintArea(Workbook wb, int sheetIndex) {
    // sheet
    Sheet sheet = wb.getSheetAt(sheetIndex);
    if (null != sheet) {
        // #endRow
        Row endRow = sheet.getRow(sheet.getLastRowNum());
        if (null != endRow) {
            Cell cell = WorkbookUtils.getCell(endRow, 0);
            String cellStr = cell.getStringCellValue();
            cellStr = cellStr == null ? "" : cellStr.trim();
            if (cellStr.startsWith(EndRowTag.KEY_ENDROW)) {
                // search #endColumn
                int endColumn = endRow.getLastCellNum();
                for (int i = endRow.getLastCellNum(); i >= endRow.getFirstCellNum(); i--) {
                    Cell endCell = WorkbookUtils.getCell(endRow, i);
                    String endCellStr = endCell.getStringCellValue();
                    endCellStr = endCellStr == null ? "" : endCellStr.trim();
                    if (endCellStr.startsWith(EndRowTag.KEY_ENDCOLUMN)) {
                        endColumn = i;
                        break;
                    }
                }
                wb.setPrintArea(sheetIndex, endRow.getFirstCellNum(), endColumn, sheet.getFirstRowNum(),
                        sheet.getLastRowNum() - 1);
                sheet.removeRow(endRow);
            }
        }
    }
}

From source file:packtest.WorkingWithPageSetup.java

License:Apache License

public static void main(String[] args) throws Exception {
    Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();

    /**/*  ww  w  .  j ava  2  s  .c  om*/
     * It's possible to set up repeating rows and columns in your printouts by using the setRepeatingRowsAndColumns() function in the Workbook object.
     *
     * This function Contains 5 parameters:
     * The first parameter is the index to the sheet (0 = first sheet).
     * The second and third parameters specify the range for the columns to repreat.
     * To stop the columns from repeating pass in -1 as the start and end column.
     * The fourth and fifth parameters specify the range for the rows to repeat.
     * To stop the columns from repeating pass in -1 as the start and end rows.
     */
    Sheet sheet1 = wb.createSheet("new sheet");
    Sheet sheet2 = wb.createSheet("second sheet");

    // Set the columns to repeat from column 0 to 2 on the first sheet
    Row row1 = sheet1.createRow(0);
    row1.createCell(0).setCellValue(1);
    row1.createCell(1).setCellValue(2);
    row1.createCell(2).setCellValue(3);
    Row row2 = sheet1.createRow(1);
    row2.createCell(1).setCellValue(4);
    row2.createCell(2).setCellValue(5);

    Row row3 = sheet2.createRow(1);
    row3.createCell(0).setCellValue(2.1);
    row3.createCell(4).setCellValue(2.2);
    row3.createCell(5).setCellValue(2.3);
    Row row4 = sheet2.createRow(2);
    row4.createCell(4).setCellValue(2.4);
    row4.createCell(5).setCellValue(2.5);

    // Set the columns to repeat from column 0 to 2 on the first sheet
    sheet1.setRepeatingColumns(CellRangeAddress.valueOf("A:C"));
    // Set the the repeating rows and columns on the second sheet.
    CellRangeAddress cra = CellRangeAddress.valueOf("E2:F3");
    sheet2.setRepeatingColumns(cra);
    sheet2.setRepeatingRows(cra);

    //set the print area for the first sheet
    wb.setPrintArea(0, 1, 2, 0, 3);

    FileOutputStream fileOut = new FileOutputStream("xssf-printsetup.xlsx");
    wb.write(fileOut);
    fileOut.close();

    wb.close();
}

From source file:poi.xssf.usermodel.examples.WorkingWithPageSetup.java

License:Apache License

public static void main(String[] args) throws Exception {
    Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();

    /**/*from w  ww.j ava 2  s  .  c  om*/
     * It's possible to set up repeating rows and columns in your printouts by using the setRepeatingRowsAndColumns() function in the Workbook object.
     *
     * This function Contains 5 parameters:
     * The first parameter is the index to the sheet (0 = first sheet).
     * The second and third parameters specify the range for the columns to repreat.
     * To stop the columns from repeating pass in -1 as the start and end column.
     * The fourth and fifth parameters specify the range for the rows to repeat.
     * To stop the columns from repeating pass in -1 as the start and end rows.
     */
    Sheet sheet1 = wb.createSheet("new sheet");
    Sheet sheet2 = wb.createSheet("second sheet");

    // Set the columns to repeat from column 0 to 2 on the first sheet
    Row row1 = sheet1.createRow(0);
    row1.createCell(0).setCellValue(1);
    row1.createCell(1).setCellValue(2);
    row1.createCell(2).setCellValue(3);
    Row row2 = sheet1.createRow(1);
    row2.createCell(1).setCellValue(4);
    row2.createCell(2).setCellValue(5);

    Row row3 = sheet2.createRow(1);
    row3.createCell(0).setCellValue(2.1);
    row3.createCell(4).setCellValue(2.2);
    row3.createCell(5).setCellValue(2.3);
    Row row4 = sheet2.createRow(2);
    row4.createCell(4).setCellValue(2.4);
    row4.createCell(5).setCellValue(2.5);

    // Set the columns to repeat from column 0 to 2 on the first sheet
    wb.setRepeatingRowsAndColumns(0, 0, 2, -1, -1);
    // Set the the repeating rows and columns on the second sheet.
    wb.setRepeatingRowsAndColumns(1, 4, 5, 1, 2);

    //set the print area for the first sheet
    wb.setPrintArea(0, 1, 2, 0, 3);

    FileOutputStream fileOut = new FileOutputStream("xssf-printsetup.xlsx");
    wb.write(fileOut);
    fileOut.close();
}