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

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

Introduction

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

Prototype

void setMargin(short margin, double size);

Source Link

Document

Sets the size of the margin in inches.

Usage

From source file:com.actelion.research.spiritapp.report.AbstractReport.java

License:Open Source License

/**
 * Creates a new sheet, ensuring that the name is safe and unique
 * @param workbook// w w w  . j  a  v a2 s  . c o m
 * @param sheetName
 * @return
 */
public Sheet createSheet(Workbook workbook, String sheetName) {
    Set<String> names = new HashSet<String>();
    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        names.add(workbook.getSheetName(i));
    }

    String safe = WorkbookUtil.createSafeSheetName(sheetName);

    String name;
    for (int i = 0;; i++) {
        name = safe + (i == 0 ? "" : " (" + i + ")");
        if (!names.contains(name))
            break;
    }
    Sheet sheet = wb.createSheet(name);
    sheet.setAutobreaks(true);
    sheet.setMargin(Sheet.LeftMargin, 1);
    sheet.setMargin(Sheet.RightMargin, 1);
    sheet.setMargin(Sheet.BottomMargin, .5);
    sheet.setMargin(Sheet.TopMargin, .5);

    sheet.setFitToPage(true);
    sheet.getPrintSetup().setLandscape(true);
    sheet.getPrintSetup().setFitWidth((short) 1);
    sheet.getPrintSetup().setFitHeight((short) 99);

    //      Footer footer = sheet.getFooter();
    //       footer.setRight( "Page " + HeaderFooter.page() + " of " + HeaderFooter.numPages() );

    return sheet;
}

From source file:jdbreport.model.io.xls.poi.Excel2003Writer.java

License:Apache License

private void saveSheet(Workbook wb, ReportModel model, ReportBook reportBook, String reportTitle)
        throws SaveReportException {

    CreationHelper createHelper = wb.getCreationHelper();

    Sheet sheet = wb.createSheet(reportTitle);
    sheet.setDisplayGridlines(reportBook.isShowGrid());
    sheet.setPrintGridlines(false);//from   w  w  w  .  j a  va 2s  .  co  m
    sheet.setFitToPage(model.isStretchPage());
    sheet.setDisplayRowColHeadings(model.isShowHeader() || model.isShowRowHeader());
    ReportPage rp = model.getReportPage();
    sheet.setMargin(Sheet.TopMargin, rp.getTopMargin(Units.INCH));
    sheet.setMargin(Sheet.BottomMargin, rp.getBottomMargin(Units.INCH));
    sheet.setMargin(Sheet.LeftMargin, rp.getLeftMargin(Units.INCH));
    sheet.setMargin(Sheet.RightMargin, rp.getRightMargin(Units.INCH));
    sheet.getPrintSetup().setLandscape(rp.getOrientation() == ReportPage.LANDSCAPE);
    short paperSize = convertPaperSize(rp.getPaperSize());
    if (paperSize > 0) {
        sheet.getPrintSetup().setPaperSize(paperSize);
    }

    TableColumnModel cm = model.getColumnModel();

    for (int c = 0; c < model.getColumnCount(); c++) {
        if (model.isColumnBreak(c)) {
            sheet.setColumnBreak(c);
        }

        //char width in points
        float char_width = 5.5f;
        sheet.setColumnWidth(c,
                (int) ((((ReportColumn) cm.getColumn(c)).getNativeWidth() - 2) / char_width * 256));
    }

    fillStyles(wb, reportBook);

    createRows(model, sheet);

    drawing = sheet.createDrawingPatriarch();
    for (int row = 0; row < model.getRowCount(); row++) {
        saveRow(wb, sheet, reportBook, model, row, createHelper);
    }
    drawing = null;
}

From source file:net.algem.planning.export.PlanningExportService.java

License:Open Source License

/**
 * Export to Excel destination file./* ww  w .j  a va2s.c o  m*/
 *
 * @param dayPlan list of day schedules
 * @param destFile destination file
 * @throws IOException
 */
public void exportPlanning(List<DayPlan> dayPlan, File destFile) throws IOException {
    GemLogger.info("Exporting planning to " + destFile);

    Hour defStartTime = new Hour(ConfigUtil.getConf(ConfigKey.START_TIME.getKey()));
    int offset = defStartTime.getHour();
    int totalh = 24 - offset; // total time length in hours

    HSSFWorkbook workbook = new HSSFWorkbook();
    Sheet sheet = workbook.createSheet("Planning");
    if (dayPlan.size() > 0) {
        DateFormat df = new SimpleDateFormat("EEEE dd MMM yyyy");
        Header header = sheet.getHeader();
        String hd = df.format(dayPlan.get(0).getSchedule().get(0).getDate().getDate());
        header.setCenter(HSSFHeader.fontSize((short) 12) + HSSFHeader.startBold() + hd + HSSFHeader.endBold());
    }

    PrintSetup printSetup = sheet.getPrintSetup();
    printSetup.setLandscape(true);
    printSetup.setPaperSize(paperSize);
    sheet.setFitToPage(true);
    sheet.setHorizontallyCenter(false);// was true before 2.15.8
    sheet.setMargin(Sheet.TopMargin, 0.75); // 1.905
    sheet.setMargin(Sheet.BottomMargin, 0.4); // 0.4 inch = 1.016 cm
    sheet.setMargin(Sheet.LeftMargin, 0.4);
    sheet.setMargin(Sheet.RightMargin, 0.4);

    Map<String, CellStyle> styles = createStyles(workbook);

    Row headerRow = sheet.createRow(0);
    for (int i = 0; i < dayPlan.size(); i++) {
        Cell roomCell = headerRow.createCell(i + 1);
        // Set the width (in units of 1/256th of a character width)
        //sheet.setColumnWidth(i + 1, totalh * 256);// max number of characters must not depend of time length
        sheet.setColumnWidth(i + 1, 24 * 256); // cours.titre character varying(32)
        roomCell.setCellValue(dayPlan.get(i).getLabel());
        roomCell.setCellStyle(styles.get("header"));
    }
    int offsetMn = offset * 60;// offset in minutes
    List<Row> rows = new ArrayList<>();
    System.out.println(" offset = " + offset + " totalh = " + totalh);
    for (int t = 0, rowNumber = 1; t < totalh * 60; t += 5, rowNumber++) { // 1 row = 5mn
        Hour hour = new Hour(offsetMn + t);
        Row row = sheet.createRow(rowNumber);
        //row.setHeightInPoints(25);
        row.setHeightInPoints(PrintSetup.A3_PAPERSIZE == paperSize ? 12 : 6);
        // TIME SUBDIVISIONS
        if (t % 15 == 0) {
            Cell cell = row.createCell(0);
            if (t % 30 == 0) {
                cell.setCellValue(hour.toString());//show time
                if (t % 60 == 0) {
                    cell.setCellStyle(styles.get("hour"));
                } else {
                    cell.setCellStyle(styles.get("hour-half"));
                }
            } else {
                cell.setCellStyle(styles.get("hour-quarter"));
            }
        } else { // BETWEEN SUBDIVISION
            Cell cell = row.createCell(0);
            if ("23:55".equals(hour.toString())) { // last slice
                cell.setCellStyle(styles.get("hour-last"));
            } else {
                cell.setCellStyle(styles.get("hour"));
            }
            if (rowNumber % 3 == 0) { // merge every 3 rows
                sheet.addMergedRegion(new CellRangeAddress(rowNumber - 2, rowNumber, 0, 0));
            }
        }
        rows.add(row);
    }

    Map<java.awt.Color, CellStyle> coursStyleCache = new HashMap<>();

    for (int i = 0; i < dayPlan.size(); i++) {
        DayPlan plan = dayPlan.get(i);
        int col = i + 1;
        for (ScheduleObject event : plan.getSchedule()) {
            // if event starts before default starting time
            if (event.getStart().toMinutes() < offsetMn) {
                event.setStart(new Hour(offset * 60));
            }
            int startRowPos = (event.getStart().toMinutes() - offsetMn) / 5 + 1;
            int endRowPos = (event.getEnd().toMinutes() - offsetMn) / 5;

            Cell courseCell = rows.get(startRowPos - 1).createCell(col);
            courseCell.setCellValue(getLabel(event, workbook));// title text

            CellStyle style = getCourseStyle(workbook, event, coursStyleCache);
            courseCell.setCellStyle(style);
            if (startRowPos != endRowPos) {
                sheet.addMergedRegion(new CellRangeAddress(startRowPos, endRowPos, col, col));
                for (int row = startRowPos; row < endRowPos; row++) {
                    rows.get(row).createCell(col).setCellStyle(style);
                }
            }
        }

    }

    try (FileOutputStream out = new FileOutputStream(destFile)) {
        workbook.write(out);
    }

}

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

License:Open Source License

@Override
public void prepareMarginDimensions(Sheet sheet, IPageContent page) {
    double headerHeight = 0.5;
    double footerHeight = 0.5;
    if ((page.getHeaderHeight() != null) && isAbsolute(page.getHeaderHeight())) {
        headerHeight = page.getHeaderHeight().convertTo(DimensionType.UNITS_IN);
        sheet.getPrintSetup().setHeaderMargin(headerHeight);
    }//from  ww w .j  a v a  2s .co m
    if ((page.getFooterHeight() != null) && isAbsolute(page.getFooterHeight())) {
        footerHeight = page.getFooterHeight().convertTo(DimensionType.UNITS_IN);
        sheet.getPrintSetup().setFooterMargin(footerHeight);
    }
    if ((page.getMarginBottom() != null) && isAbsolute(page.getMarginBottom())) {
        sheet.setMargin(Sheet.BottomMargin,
                footerHeight + page.getMarginBottom().convertTo(DimensionType.UNITS_IN));
    }
    if ((page.getMarginLeft() != null) && isAbsolute(page.getMarginLeft())) {
        sheet.setMargin(Sheet.LeftMargin, page.getMarginLeft().convertTo(DimensionType.UNITS_IN));
    }
    if ((page.getMarginRight() != null) && isAbsolute(page.getMarginRight())) {
        sheet.setMargin(Sheet.RightMargin, page.getMarginRight().convertTo(DimensionType.UNITS_IN));
    }
    if ((page.getMarginTop() != null) && isAbsolute(page.getMarginTop())) {
        sheet.setMargin(Sheet.TopMargin, headerHeight + page.getMarginTop().convertTo(DimensionType.UNITS_IN));
    }
}

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

License:Open Source License

@Override
public void prepareMarginDimensions(Sheet sheet, IPageContent page) {
    double headerHeight = 0.5;
    double footerHeight = 0.5;
    if ((page.getHeaderHeight() != null) && isAbsolute(page.getHeaderHeight())) {
        headerHeight = page.getHeaderHeight().convertTo(DimensionType.UNITS_IN);
        sheet.setMargin(Sheet.HeaderMargin, headerHeight);
    }//from w ww .ja  va2s  .  co  m
    if ((page.getFooterHeight() != null) && isAbsolute(page.getFooterHeight())) {
        footerHeight = page.getFooterHeight().convertTo(DimensionType.UNITS_IN);
        sheet.setMargin(Sheet.FooterMargin, footerHeight);
    }
    if ((page.getMarginBottom() != null) && isAbsolute(page.getMarginBottom())) {
        sheet.setMargin(Sheet.BottomMargin,
                footerHeight + page.getMarginBottom().convertTo(DimensionType.UNITS_IN));
    }
    if ((page.getMarginLeft() != null) && isAbsolute(page.getMarginLeft())) {
        sheet.setMargin(Sheet.LeftMargin, page.getMarginLeft().convertTo(DimensionType.UNITS_IN));
    }
    if ((page.getMarginRight() != null) && isAbsolute(page.getMarginRight())) {
        sheet.setMargin(Sheet.RightMargin, page.getMarginRight().convertTo(DimensionType.UNITS_IN));
    }
    if ((page.getMarginTop() != null) && isAbsolute(page.getMarginTop())) {
        sheet.setMargin(Sheet.TopMargin, headerHeight + page.getMarginTop().convertTo(DimensionType.UNITS_IN));
    }
}