Example usage for org.apache.poi.ss.usermodel Sheet getHeader

List of usage examples for org.apache.poi.ss.usermodel Sheet getHeader

Introduction

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

Prototype

Header getHeader();

Source Link

Document

Gets the user model for the default document header.

Note that XSSF offers more kinds of document headers than HSSF does

Usage

From source file:org.pentaho.reporting.engine.classic.core.modules.output.table.xls.helper.ExcelPrinterBase.java

License:Open Source License

protected void configureSheetProperties(final Sheet sheet,
        final SheetPropertySource excelTableContentProducer) {
    final String pageHeaderCenter = excelTableContentProducer.getPageHeaderCenter();
    final String pageFooterCenter = excelTableContentProducer.getPageFooterCenter();
    final String pageHeaderLeft = excelTableContentProducer.getPageHeaderLeft();
    final String pageFooterLeft = excelTableContentProducer.getPageFooterLeft();
    final String pageHeaderRight = excelTableContentProducer.getPageHeaderRight();
    final String pageFooterRight = excelTableContentProducer.getPageFooterRight();

    if (isHeaderFooterValid(pageHeaderLeft, pageHeaderCenter, pageHeaderRight)) {
        if (pageHeaderLeft != null) {
            sheet.getHeader().setLeft(pageHeaderLeft);
        }/*from  w w w  .  ja  va 2 s.c o m*/
        if (pageHeaderCenter != null) {
            sheet.getHeader().setCenter(pageHeaderCenter);
        }
        if (pageHeaderRight != null) {
            sheet.getHeader().setRight(pageHeaderRight);
        }
    } else {
        logger.warn(
                "Page-Header exceeds the maximum length of 255 characters. No page-header will be added to the sheet.");
    }
    if (isHeaderFooterValid(pageFooterLeft, pageFooterCenter, pageFooterRight)) {
        if (pageFooterCenter != null) {
            sheet.getFooter().setCenter(pageFooterCenter);
        }
        if (pageFooterLeft != null) {
            sheet.getFooter().setLeft(pageFooterLeft);
        }
        if (pageFooterRight != null) {
            sheet.getFooter().setRight(pageFooterRight);
        }
    } else {
        logger.warn(
                "Page-Footer exceeds the maximum length of 255 characters. No page-footer will be added to the sheet.");
    }

    int sheetFreezeTop = excelTableContentProducer.getFreezeTop();
    int sheetFreezeLeft = excelTableContentProducer.getFreezeLeft();
    if (sheetFreezeTop > 0 || sheetFreezeLeft > 0) {
        sheet.createFreezePane(sheetFreezeLeft, sheetFreezeTop);
    }
}

From source file:thesisproject.createExcel.java

public static void main(String[] args) {
    Workbook wb = new HSSFWorkbook();
    Sheet sheet = wb.createSheet("new sheet");

    Header header = sheet.getHeader();
    header.setCenter("Center Header");
    header.setLeft("Left Header");
    String LAST_NAME_COLUMN_HEADER = "?";
    String Quantity_COLUMN_HEADER = "-";
    HSSFRow row;//from  w w w  . j  a v a  2  s. com
    HSSFCell cell;
    row = (HSSFRow) sheet.createRow(0);
    cell = row.createCell(0, Cell.CELL_TYPE_STRING);
    cell.setCellValue(LAST_NAME_COLUMN_HEADER);
    cell = row.createCell(1, Cell.CELL_TYPE_STRING);
    cell.setCellValue(Quantity_COLUMN_HEADER);
    try {
        FileOutputStream fileOut = new FileOutputStream("C:\\Users\\?\\Desktop\\workbook.xls");
        wb.write(fileOut);
        fileOut.close();
        System.out.println("Done");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:uk.ac.liverpool.spreadsheet.ExcelFeatureAnalysis.java

License:Apache License

private static void analyseSheet(Sheet ss, Element s, Namespace n, ExcelFeatureAnalysis efa) {
    // generic part
    boolean costumFormatting = false;
    boolean formulae = false;
    boolean UDF = false;
    boolean hasComments = false;

    Set<String> udfs = new HashSet<String>();
    FormulaEvaluator evaluator = ss.getWorkbook().getCreationHelper().createFormulaEvaluator();

    s.setAttribute("name", ss.getSheetName());
    s.setAttribute("firstRow", "" + ss.getFirstRowNum());
    s.setAttribute("lastRow", "" + ss.getLastRowNum());
    try {/*from www.  j  ava 2s  .  c o  m*/
        s.setAttribute("forceFormulaRecalc", "" + ss.getForceFormulaRecalculation());
    } catch (Throwable x) {
        //x.printStackTrace();
    }

    // shapes in detail? 
    Footer footer = ss.getFooter();
    if (footer != null) {
        s.setAttribute("footer", "true");
    }
    Header header = ss.getHeader();
    if (header != null) {
        s.setAttribute("header", "true");
    }
    PaneInformation paneInformation = ss.getPaneInformation();
    if (paneInformation != null) {
        s.setAttribute("panels", "true");
    }

    HSSFSheet hs = null;
    XSSFSheet xs = null;
    if (ss instanceof HSSFSheet) {
        hs = (HSSFSheet) ss;
        try {
            if (hs.getDrawingPatriarch() != null) {
                if (hs.getDrawingPatriarch().containsChart())
                    s.addContent(new Element("charts", sn));
                if (hs.getDrawingPatriarch().countOfAllChildren() > 0)
                    s.addContent(new Element("shapes", sn));
            }
        } catch (Exception x) {
            x.printStackTrace();
        }

        if (hs.getSheetConditionalFormatting().getNumConditionalFormattings() > 0) {
            s.setAttribute("conditionalFormatting", "true");
        }
    }
    if (ss instanceof XSSFSheet) {
        xs = (XSSFSheet) ss;

    }
    Iterator<Row> rows = ss.rowIterator();

    int firstColumn = (rows.hasNext() ? Integer.MAX_VALUE : 0);
    int endColumn = 0;
    while (rows.hasNext()) {
        Row row = rows.next();
        short firstCell = row.getFirstCellNum();
        if (firstCell >= 0) {
            firstColumn = Math.min(firstColumn, firstCell);
            endColumn = Math.max(endColumn, row.getLastCellNum());
        }
    }
    s.setAttribute("firstColumn", "" + firstColumn);
    s.setAttribute("lastColumn", "" + endColumn);
    rows = ss.rowIterator();
    while (rows.hasNext()) {
        Row row = rows.next();
        for (Cell cell : row)
            if (cell != null) {
                try {
                    if (!cell.getCellStyle().getDataFormatString().equals("GENERAL"))
                        costumFormatting = true;
                } catch (Throwable t) {
                }

                if (cell.getCellComment() != null)
                    hasComments = true;
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    // System.out.println(cell.getRichStringCellValue().getString());
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    //                        if (DateUtil.isCellDateFormatted(cell)) {
                    //                            // System.out.println(cell.getDateCellValue());
                    //                        } else {
                    //                            // System.out.println(cell.getNumericCellValue());
                    //                        }
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    // System.out.println(cell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    // System.out.println(cell.getCellFormula());
                    formulae = true;
                    if (!UDF)
                        try {
                            evaluator.evaluate(cell);
                        } catch (Exception x) {
                            if (x instanceof NotImplementedException) {
                                Throwable e = x;

                                //e.printStackTrace();
                                while (e != null) {
                                    for (StackTraceElement c : e.getStackTrace()) {
                                        if (c.getClassName().contains("UserDefinedFunction")) {
                                            UDF = true;
                                            System.out.println("UDF " + e.getMessage());
                                            udfs.add(e.getMessage());
                                        }
                                    }
                                    e = e.getCause();
                                }

                            }
                        }
                    break;
                default:
                }

            }
    }
    if (costumFormatting) {
        Element cf = new Element("customisedFormatting", sn);
        s.addContent(cf);
    }
    if (formulae) {
        Element cf = new Element("formulae", sn);
        s.addContent(cf);
    }
    if (UDF) {
        Element cf = new Element("userDefinedFunctions", sn);
        for (String sss : udfs)
            cf.addContent(new Element("userDefinedFunction", sn).setAttribute("functionName", sss));
        s.addContent(cf);
    }
    if (hasComments) {
        Element cf = new Element("cellComments", sn);
        s.addContent(cf);
    }
}

From source file:uk.co.spudsoft.birt.emitters.excel.tests.SimpleTableReportTest.java

License:Open Source License

@Test
public void testRunReport() throws BirtException, IOException {

    InputStream inputStream = runAndRenderReport("SimpleTable.rptdesign", "xlsx");
    assertNotNull(inputStream);//from ww w  .  j av a 2  s  .c  o m
    try {

        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        assertNotNull(workbook);

        assertEquals(1, workbook.getNumberOfSheets());
        assertEquals("Simple Table Report", workbook.getSheetAt(0).getSheetName());

        Sheet sheet = workbook.getSheetAt(0);
        assertEquals(2, firstNullRow(sheet));

        assertEquals("1", sheet.getRow(0).getCell(0).getStringCellValue());
        assertEquals("2", sheet.getRow(1).getCell(0).getStringCellValue());
        assertEquals(3.0, sheet.getRow(0).getCell(1).getNumericCellValue(), 0.001);
        assertEquals(Cell.CELL_TYPE_BLANK, sheet.getRow(1).getCell(1).getCellType());

        assertEquals("Title\nSubtitle", sheet.getHeader().getLeft());
        assertEquals("The Writer", sheet.getFooter().getLeft());
        assertEquals("1", sheet.getFooter().getCenter());
    } finally {
        inputStream.close();
    }
}

From source file:wuliu.test.POITest.java

License:Open Source License

public static void main(String args[]) throws EncryptedDocumentException, InvalidFormatException, IOException {
    InputStream inp = new FileInputStream(
            "/Users/admin/Workspace/workspace/wuliu/code/src/test/resources/template.xlsx");
    Workbook wb = WorkbookFactory.create(inp);
    Sheet sheet = wb.getSheetAt(0);
    System.out.println(sheet.getHeader().getRight());
}