Example usage for org.apache.poi.hssf.usermodel HSSFPrintSetup setLandscape

List of usage examples for org.apache.poi.hssf.usermodel HSSFPrintSetup setLandscape

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFPrintSetup setLandscape.

Prototype

public void setLandscape(boolean ls) 

Source Link

Document

Set whether to print in landscape

Usage

From source file:com.square.core.util.poi.DocumentXls.java

License:Open Source License

/**
 * Genere l'entete du document.//from w  w w.ja  v a  2 s  .c  o m
 */
private void genererEnteteDocument() {
    // Cration du classeur
    classeur = new HSSFWorkbook();
    creerPage();

    // Dfinition de l'impression
    final HSSFPrintSetup impression = classeur.getSheetAt(0).getPrintSetup();
    impression.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
    impression.setLandscape(true);
}

From source file:de.maklerpoint.office.Schnittstellen.Excel.ExportExcelXLS.java

License:Open Source License

/**
 * /*from  w w  w .j a  v a 2s  .  c  o m*/
 * @throws FileNotFoundException
 * @throws IOException
 */

public void write() throws FileNotFoundException, IOException {
    FileOutputStream out = new FileOutputStream(new File(filename));
    HSSFWorkbook wb;

    wb = new HSSFWorkbook();

    Map<String, HSSFCellStyle> styles = createStyles(wb);
    HSSFSheet sheet = wb.createSheet(sheetName);

    //turn off gridlines
    sheet.setDisplayGridlines(false);
    sheet.setPrintGridlines(false);
    sheet.setFitToPage(true);
    sheet.setHorizontallyCenter(true);
    HSSFPrintSetup printSetup = sheet.getPrintSetup();
    printSetup.setLandscape(true);

    sheet.setAutobreaks(true);
    printSetup.setFitHeight((short) 1);
    printSetup.setFitWidth((short) 1);

    HSSFRow headerRow = sheet.createRow(0);
    headerRow.setHeightInPoints(12.75f);

    int[][] width = new int[titles.length][titles.length];

    for (int i = 0; i < titles.length; i++) {
        HSSFCell cell = headerRow.createCell(i);
        cell.setCellValue(titles[i]);
        cell.setCellStyle(styles.get("header"));

        width[i][0] = titles[i].length();
    }

    HSSFRow row;
    HSSFCell cell;
    int rownum = 1;

    for (int i = 0; i < data.length; i++, rownum++) {
        row = sheet.createRow(rownum);
        if (data[i] == null)
            continue;

        for (int j = 0; j < data[i].length; j++) {
            cell = row.createCell(j);
            if (data[i][j] == null)
                data[i][j] = "";

            cell.setCellValue(data[i][j].toString());

            if (data[i][j].toString().length() > width[j][0])
                width[j][0] = data[i][j].toString().length();
        }
    }

    for (int i = 0; i < titles.length; i++) {
        int widthShort = (256 * (width[i][0] + 3));

        sheet.setColumnWidth(i, widthShort);
    }

    int position = (titles.length / 2) - 1;

    row = sheet.createRow(rownum + 3);
    cell = row.createCell(position);

    if (footName == null) {
        SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm");
        cell.setCellValue("Export MaklerPoint vom " + df.format(new Date(System.currentTimeMillis()))
                + " - www.maklerpoint.de");
    } else {
        cell.setCellValue(footName);
    }

    sheet.setZoom(3, 4);

    wb.write(out);
    out.close();
}

From source file:de.maklerpoint.office.Schnittstellen.Excel.ExportKalenderExcel.java

License:Open Source License

/**
 * /*ww  w.j a v  a 2 s .  c  o m*/
 * @throws FileNotFoundException
 * @throws IOException
 */

public void write() throws FileNotFoundException, IOException {
    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);

    HSSFWorkbook wb = new HSSFWorkbook();
    Map<String, HSSFCellStyle> styles = createStyles(wb);

    for (int month = 0; month < 12; month++) {
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        //create a sheet for each month
        HSSFSheet sheet = wb.createSheet(months[month]);

        //turn off gridlines
        sheet.setDisplayGridlines(false);
        sheet.setPrintGridlines(false);
        sheet.setFitToPage(true);
        sheet.setHorizontallyCenter(true);
        HSSFPrintSetup printSetup = sheet.getPrintSetup();
        printSetup.setLandscape(true);

        //the following three statements are required only for HSSF
        sheet.setAutobreaks(true);
        printSetup.setFitHeight((short) 1);
        printSetup.setFitWidth((short) 1);

        //the header row: centered text in 48pt font
        HSSFRow headerRow = sheet.createRow(0);
        headerRow.setHeightInPoints(80);
        HSSFCell titleCell = headerRow.createCell(0);
        titleCell.setCellValue(months[month] + " " + year);
        titleCell.setCellStyle(styles.get("title"));
        //                sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$N$1"));

        //header with month titles
        HSSFRow monthRow = sheet.createRow(1);
        for (int i = 0; i < days.length; i++) {
            //set column widths, the width is measured in units of 1/256th of a character width
            sheet.setColumnWidth((i * 2), (5 * 256)); //the column is 5 characters wide
            sheet.setColumnWidth((i * 2 + 1), (13 * 256)); //the column is 13 characters wide
            //sheet.addMergedRegion(new Region(1, (short) 1, i*2, (short) (i * 2 + 1)));
            sheet.addMergedRegion(new CellRangeAddress(1, i * 2, 1, (i * 2 + 1))); // TODO Test
            HSSFCell monthCell = monthRow.createCell((i * 2));
            monthCell.setCellValue(days[i]);
            monthCell.setCellStyle(styles.get("month"));
        }

        int cnt = 1, day = 1;
        int rownum = 2;
        for (int j = 0; j < 6; j++) {
            HSSFRow row = sheet.createRow(rownum++);
            row.setHeightInPoints(100);
            for (int i = 0; i < days.length; i++) {
                HSSFCell dayCell_1 = row.createCell((i * 2));
                HSSFCell dayCell_2 = row.createCell((i * 2 + 1));

                int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);
                if (cnt >= day_of_week && calendar.get(Calendar.MONTH) == month) {
                    dayCell_1.setCellValue(day);
                    calendar.set(Calendar.DAY_OF_MONTH, ++day);

                    if (i == 0 || i == days.length - 1) {
                        dayCell_1.setCellStyle(styles.get("weekend_left"));
                        dayCell_2.setCellStyle(styles.get("weekend_right"));
                    } else {
                        dayCell_1.setCellStyle(styles.get("workday_left"));
                        dayCell_2.setCellStyle(styles.get("workday_right"));
                    }
                } else {
                    dayCell_1.setCellStyle(styles.get("grey_left"));
                    dayCell_2.setCellStyle(styles.get("grey_right"));
                }
                cnt++;
            }
            if (calendar.get(Calendar.MONTH) > month)
                break;
        }
    }

    // Write the output to a file        

    FileOutputStream out = new FileOutputStream(this.filename);
    wb.write(out);
    out.close();
}

From source file:excel.FileExcel.java

public File excel_create_cari_gudang(ArrayList<CariGudangReportData> CariGudang, Date waktuprint) {
    // find number of counter, save in Counter Index
    if (!CariGudang.isEmpty()) {
        DateFormat time = new SimpleDateFormat("hhmm");
        String fileName = "CariGudang_" + fmt.format(waktuprint) + "_" + time.format(waktuprint) + ".xls";
        File FileCariGudang = new File(fileName);

        HSSFWorkbook workbook;/*from   w ww  . j  a  v a  2s .  co  m*/
        HSSFSheet sheet;

        workbook = new HSSFWorkbook();
        sheet = workbook.createSheet();

        // set page
        HSSFPrintSetup ps = sheet.getPrintSetup();
        ps.setLandscape(true);
        ps.setFitHeight((short) 1);
        ps.setFitWidth((short) 1);
        sheet.setFitToPage(true);

        //Set Header Information 
        Header headerPage = sheet.getHeader();
        headerPage.setCenter(HeaderFooter.page());
        headerPage.setRight(fileName);

        //Set Footer Information with Page Numbers
        Footer footerPage = sheet.getFooter();
        footerPage.setCenter("Page " + HeaderFooter.page() + " of " + HeaderFooter.numPages());

        // prepare variable to edit the xls
        HSSFRow header;
        HSSFCell cell;
        HSSFCellStyle titlestyle = workbook.createCellStyle();
        HSSFCellStyle headerstyle = workbook.createCellStyle();
        HSSFCellStyle datastyle = workbook.createCellStyle();
        HSSFFont boldfont = workbook.createFont();
        HSSFFont normalfont = workbook.createFont();

        // create the title 
        header = sheet.createRow(1);
        cell = header.createCell(1);
        boldfont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        titlestyle.setFont(boldfont);
        titlestyle.setAlignment(CellStyle.ALIGN_CENTER);
        titlestyle.setBorderTop(HSSFCellStyle.BORDER_NONE);
        titlestyle.setBorderBottom(HSSFCellStyle.BORDER_NONE);
        titlestyle.setBorderLeft(HSSFCellStyle.BORDER_NONE);
        titlestyle.setBorderRight(HSSFCellStyle.BORDER_NONE);
        cell.setCellStyle(titlestyle);
        cell.setCellValue("TABEL CARI GUDANG");
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 4));

        // create file info
        header = sheet.createRow(4);
        cell = header.createCell(1);
        cell.setCellValue("Tanggal : ");
        cell = header.createCell(2);
        cell.setCellValue(fmt.format(waktuprint));

        header = sheet.createRow(5);
        cell = header.createCell(1);
        cell.setCellValue("Jam : ");
        cell = header.createCell(2);
        cell.setCellValue(time.format(waktuprint));

        // create the header
        headerstyle.setFont(boldfont);
        headerstyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
        headerstyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
        headerstyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
        headerstyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
        header = sheet.createRow(7);
        cell = header.createCell(1);
        cell.setCellStyle(headerstyle);
        cell.setCellValue("Kode Order");
        cell = header.createCell(2);
        cell.setCellStyle(headerstyle);
        cell.setCellValue("Kode Konter");
        cell = header.createCell(3);
        cell.setCellStyle(headerstyle);
        cell.setCellValue("Kode Barang");
        cell = header.createCell(4);
        cell.setCellStyle(headerstyle);
        cell.setCellValue("Jumlah");
        cell = header.createCell(5);
        cell.setCellStyle(headerstyle);
        cell.setCellValue("Nama Barang");
        cell = header.createCell(6);
        cell.setCellStyle(headerstyle);
        cell.setCellValue("Kategori");
        cell = header.createCell(7);
        cell.setCellStyle(headerstyle);
        cell.setCellValue("HargaTPG");
        cell = header.createCell(8);
        cell.setCellStyle(headerstyle);
        cell.setCellValue("Disc");
        cell = header.createCell(9);
        cell.setCellStyle(headerstyle);
        cell.setCellValue("Harga Net");
        cell = header.createCell(10);
        cell.setCellStyle(headerstyle);
        cell.setCellValue("Total Net");

        normalfont.setBoldweight(Font.BOLDWEIGHT_NORMAL);
        datastyle.setFont(normalfont);
        HSSFDataFormat df = workbook.createDataFormat();
        datastyle.setDataFormat(df.getFormat("#,###"));
        datastyle.setAlignment(CellStyle.ALIGN_RIGHT);
        datastyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        datastyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        datastyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        datastyle.setBorderRight(HSSFCellStyle.BORDER_THIN);

        int row_num = 0;

        double net = 0;
        double total_tpg = 0;
        double total_net = 0;
        double sum_total_tpg = 0;
        double sum_total_net = 0;

        int j;
        for (j = 0; j < CariGudang.size(); j++) {
            net = (double) CariGudang.get(j).harga_tpg * (100.0 - (double) CariGudang.get(j).disc) / 100.0;
            total_net = (double) net * (double) CariGudang.get(j).jumlah;
            sum_total_tpg += total_tpg;
            sum_total_net += total_net;

            header = sheet.createRow(8 + row_num);
            cell = header.createCell(1);
            cell.setCellStyle(datastyle);
            cell.setCellValue(CariGudang.get(j).kode_order);
            cell = header.createCell(2);
            cell.setCellStyle(datastyle);
            cell.setCellValue(CariGudang.get(j).kode_konter);
            cell = header.createCell(3);
            cell.setCellStyle(datastyle);
            cell.setCellValue(CariGudang.get(j).kode_barang);
            cell = header.createCell(4);
            cell.setCellStyle(datastyle);
            cell.setCellValue(CariGudang.get(j).jumlah);
            cell = header.createCell(5);
            cell.setCellStyle(datastyle);
            cell.setCellValue(CariGudang.get(j).nama_barang);
            cell = header.createCell(6);
            cell.setCellStyle(datastyle);
            cell.setCellValue(CariGudang.get(j).kategori);
            cell = header.createCell(7);
            cell.setCellStyle(datastyle);
            cell.setCellValue(CariGudang.get(j).harga_tpg);
            cell = header.createCell(8);
            cell.setCellStyle(datastyle);
            cell.setCellValue(CariGudang.get(j).disc);
            cell = header.createCell(9);
            cell.setCellStyle(datastyle);
            cell.setCellValue(net);
            cell = header.createCell(10);
            cell.setCellStyle(datastyle);
            cell.setCellValue(total_net);
            row_num++;
        }

        datastyle.setFont(boldfont);
        header = sheet.createRow(j + 8);
        cell = header.createCell(9);
        cell.setCellStyle(datastyle);
        cell.setCellValue("TOTAL");
        cell = header.createCell(10);
        cell.setCellStyle(datastyle);
        cell.setCellValue(sum_total_net);

        sheet.autoSizeColumn(1);
        sheet.autoSizeColumn(2);
        sheet.autoSizeColumn(3);
        sheet.autoSizeColumn(4);
        sheet.autoSizeColumn(5);
        sheet.autoSizeColumn(6);
        sheet.autoSizeColumn(7);
        sheet.autoSizeColumn(8);
        sheet.autoSizeColumn(9);
        sheet.autoSizeColumn(10);

        try {
            FileOutputStream out = new FileOutputStream(FileCariGudang);
            workbook.write(out);
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            FileCariGudang = null;
        } catch (IOException e) {
            e.printStackTrace();
            FileCariGudang = null;
        }
        return FileCariGudang;
    } else {
        return null;
    }
}

From source file:net.chaosserver.timelord.data.ExcelDataReaderWriter.java

License:Open Source License

/**
 * Generates the actual workbook of data.
 *
 * @param timelordData the data to generate a workbook for
 * @return the workbook//from ww w  .j  av a  2s . co m
 */
protected HSSFWorkbook generateWorkbook(TimelordData timelordData) {
    HSSFWorkbook wb = new HSSFWorkbook();

    // Build the Map of the Styles that will be applied to cells
    // in the workbook
    Map<String, HSSFCellStyle> styleMap = buildStyleMap(wb);
    Map<String, List<String>> sheetToNotes = new TreeMap<String, List<String>>(new DateComparator());

    // Since there is an issue re-ordering sheets after they
    // have been created.  First create the book with all needed
    // sheets
    preCreateAllSheets(wb, timelordData, sheetToNotes, styleMap);

    // After all the sheets have been pre-created, iterate through all
    // the tasks to add them into the sheets.
    int rowNum = addAllTasks(wb, timelordData, sheetToNotes, styleMap);

    // This section applies all the styles, creates the footers and adds
    // the notes onto the sheet.
    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        HSSFSheet sheet = wb.getSheetAt(i);
        String sheetName = wb.getSheetName(i);
        createFooterRows(sheet, rowNum, styleMap);

        // This will apply styles to the rows that had no task associated
        // for a given week.
        for (int j = 1; j < rowNum - 1; j++) {
            HSSFRow row = sheet.getRow(j);
            if (row == null) {
                row = sheet.createRow(j);
                row.setHeight((short) 0);
                HSSFCell cell = row.createCell((short) 0);
                cell.setCellStyle((HSSFCellStyle) styleMap.get("taskNameStyle"));
                cell.setCellValue("");

                cell = row.createCell(MAX_COLUMN);
                cell.setCellStyle((HSSFCellStyle) styleMap.get("totalColumnStyle"));
                cell.setCellFormula("SUM(B" + (j + 1) + ":H" + (j + 1) + ")");
            }
        }

        List<String> noteList = sheetToNotes.get(sheetName);
        createNotesRows(sheet, noteList);

        HSSFPrintSetup ps = sheet.getPrintSetup();
        ps.setLandscape(true);
    }

    // Finally order the sheets properly
    if (logger.isDebugEnabled()) {
        logger.debug("Re-ordering sheets under final order.");
    }

    return wb;
}

From source file:net.sf.jasperreports.engine.export.JRXlsExporter.java

License:Open Source License

protected void createSheet(CutsInfo xCuts, SheetInfo sheetInfo) {
    sheet = workbook.createSheet(sheetInfo.sheetName);
    patriarch = sheet.createDrawingPatriarch();
    HSSFPrintSetup printSetup = sheet.getPrintSetup();
    printSetup.setLandscape(jasperPrint.getOrientationValue() == OrientationEnum.LANDSCAPE);
    short paperSize = getSuitablePaperSize(jasperPrint);

    if (paperSize != -1) {
        printSetup.setPaperSize(paperSize);
    }/*from   w w  w  .  j  ava  2 s .c om*/

    XlsReportConfiguration configuration = getCurrentItemConfiguration();

    String password = configuration.getPassword();
    if (password != null) {
        sheet.protectSheet(password);
    }

    boolean isIgnorePageMargins = configuration.isIgnorePageMargins();
    if (jasperPrint.getLeftMargin() != null) {
        sheet.setMargin((short) 0,
                LengthUtil.inchNoRound(isIgnorePageMargins ? 0 : jasperPrint.getLeftMargin()));
    }

    if (jasperPrint.getRightMargin() != null) {
        sheet.setMargin((short) 1,
                LengthUtil.inchNoRound(isIgnorePageMargins ? 0 : jasperPrint.getRightMargin()));
    }

    if (jasperPrint.getTopMargin() != null) {
        sheet.setMargin((short) 2,
                LengthUtil.inchNoRound(isIgnorePageMargins ? 0 : jasperPrint.getTopMargin()));
    }

    if (jasperPrint.getBottomMargin() != null) {
        sheet.setMargin((short) 3,
                LengthUtil.inchNoRound(isIgnorePageMargins ? 0 : jasperPrint.getBottomMargin()));
    }

    Integer fitWidth = configuration.getFitWidth();
    if (!isValidScale(sheetInfo.sheetPageScale) && fitWidth != null) {
        printSetup.setFitWidth(fitWidth.shortValue());
        sheet.setAutobreaks(true);
    }

    Integer fitHeight = configuration.getFitHeight();
    if (!isValidScale(sheetInfo.sheetPageScale) && fitHeight != null) {
        printSetup.setFitHeight(fitHeight.shortValue());
        sheet.setAutobreaks(true);
    }

    String sheetHeaderLeft = configuration.getSheetHeaderLeft();
    if (sheetHeaderLeft != null) {
        sheet.getHeader().setLeft(sheetHeaderLeft);
    }

    String sheetHeaderCenter = configuration.getSheetHeaderCenter();
    if (sheetHeaderCenter != null) {
        sheet.getHeader().setCenter(sheetHeaderCenter);
    }

    String sheetHeaderRight = configuration.getSheetHeaderRight();
    if (sheetHeaderRight != null) {
        sheet.getHeader().setRight(sheetHeaderRight);
    }

    String sheetFooterLeft = configuration.getSheetFooterLeft();
    if (sheetFooterLeft != null) {
        sheet.getFooter().setLeft(sheetFooterLeft);
    }

    String sheetFooterCenter = configuration.getSheetFooterCenter();
    if (sheetFooterCenter != null) {
        sheet.getFooter().setCenter(sheetFooterCenter);
    }

    String sheetFooterRight = configuration.getSheetFooterRight();
    if (sheetFooterRight != null) {
        sheet.getFooter().setRight(sheetFooterRight);
    }

    RunDirectionEnum sheetDirection = configuration.getSheetDirection();
    if (sheetDirection != null) {
        printSetup.setLeftToRight(sheetDirection == RunDirectionEnum.LTR);
        sheet.setRightToLeft(sheetDirection == RunDirectionEnum.RTL);
    }

    if (sheetInfo.sheetFirstPageNumber != null && sheetInfo.sheetFirstPageNumber > 0) {
        printSetup.setPageStart((short) sheetInfo.sheetFirstPageNumber.intValue());
        printSetup.setUsePage(true);
        firstPageNotSet = false;
    } else {
        Integer documentFirstPageNumber = configuration.getFirstPageNumber();
        if (documentFirstPageNumber != null && documentFirstPageNumber > 0 && firstPageNotSet) {
            printSetup.setPageStart((short) documentFirstPageNumber.intValue());
            printSetup.setUsePage(true);
            firstPageNotSet = false;
        }
    }
    if (!firstPageNotSet
            && (sheet.getFooter().getCenter() == null || sheet.getFooter().getCenter().length() == 0)) {
        sheet.getFooter().setCenter("Page " + HeaderFooter.page());
    }

    boolean showGridlines = true;
    if (sheetInfo.sheetShowGridlines == null) {
        Boolean documentShowGridlines = configuration.isShowGridLines();
        if (documentShowGridlines != null) {
            showGridlines = documentShowGridlines;
        }
    } else {
        showGridlines = sheetInfo.sheetShowGridlines;
    }
    sheet.setDisplayGridlines(showGridlines);

    maxRowFreezeIndex = 0;
    maxColumnFreezeIndex = 0;

    onePagePerSheetMap.put(sheetIndex, configuration.isOnePagePerSheet());
    sheetsBeforeCurrentReportMap.put(sheetIndex, sheetsBeforeCurrentReport);
}

From source file:net.sf.jasperreports.engine.export.JRXlsMetadataExporter.java

License:Open Source License

protected void createSheet(SheetInfo sheetInfo) {
    sheet = workbook.createSheet(sheetInfo.sheetName);
    patriarch = sheet.createDrawingPatriarch();
    HSSFPrintSetup printSetup = sheet.getPrintSetup();
    printSetup.setLandscape(jasperPrint.getOrientationValue() == OrientationEnum.LANDSCAPE);
    short paperSize = getSuitablePaperSize(jasperPrint);

    XlsReportConfiguration configuration = getCurrentItemConfiguration();

    if (paperSize != -1) {
        printSetup.setPaperSize(paperSize);
    }/*  w  ww.  jav a2  s .  c o  m*/

    String password = configuration.getPassword();
    if (password != null) {
        sheet.protectSheet(password);
    }

    boolean isIgnorePageMargins = configuration.isIgnorePageMargins();

    if (jasperPrint.getLeftMargin() != null) {
        sheet.setMargin((short) 0,
                LengthUtil.inchNoRound(isIgnorePageMargins ? 0 : jasperPrint.getLeftMargin()));
    }

    if (jasperPrint.getRightMargin() != null) {
        sheet.setMargin((short) 1,
                LengthUtil.inchNoRound(isIgnorePageMargins ? 0 : jasperPrint.getRightMargin()));
    }

    if (jasperPrint.getTopMargin() != null) {
        sheet.setMargin((short) 2,
                LengthUtil.inchNoRound(isIgnorePageMargins ? 0 : jasperPrint.getTopMargin()));
    }

    if (jasperPrint.getBottomMargin() != null) {
        sheet.setMargin((short) 3,
                LengthUtil.inchNoRound(isIgnorePageMargins ? 0 : jasperPrint.getBottomMargin()));
    }

    Integer fitWidth = configuration.getFitWidth();
    if (!isValidScale(sheetInfo.sheetPageScale) && fitWidth != null) {
        printSetup.setFitWidth(fitWidth.shortValue());
        sheet.setAutobreaks(true);
    }

    Integer fitHeight = configuration.getFitHeight();
    if (!isValidScale(sheetInfo.sheetPageScale) && fitHeight != null) {
        printSetup.setFitHeight(fitHeight.shortValue());
        sheet.setAutobreaks(true);
    }

    String sheetHeaderLeft = configuration.getSheetHeaderLeft();
    if (sheetHeaderLeft != null) {
        sheet.getHeader().setLeft(sheetHeaderLeft);
    }

    String sheetHeaderCenter = configuration.getSheetHeaderCenter();
    if (sheetHeaderCenter != null) {
        sheet.getHeader().setCenter(sheetHeaderCenter);
    }

    String sheetHeaderRight = configuration.getSheetHeaderRight();
    if (sheetHeaderRight != null) {
        sheet.getHeader().setRight(sheetHeaderRight);
    }

    String sheetFooterLeft = configuration.getSheetFooterLeft();
    if (sheetFooterLeft != null) {
        sheet.getFooter().setLeft(sheetFooterLeft);
    }

    String sheetFooterCenter = configuration.getSheetFooterCenter();
    if (sheetFooterCenter != null) {
        sheet.getFooter().setCenter(sheetFooterCenter);
    }

    String sheetFooterRight = configuration.getSheetFooterRight();
    if (sheetFooterRight != null) {
        sheet.getFooter().setRight(sheetFooterRight);
    }

    RunDirectionEnum sheetDirection = configuration.getSheetDirection();
    if (sheetDirection != null) {
        printSetup.setLeftToRight(sheetDirection == RunDirectionEnum.LTR);
        sheet.setRightToLeft(sheetDirection == RunDirectionEnum.RTL);
    }

    if (sheetInfo.sheetFirstPageNumber != null && sheetInfo.sheetFirstPageNumber > 0) {
        printSetup.setPageStart((short) sheetInfo.sheetFirstPageNumber.intValue());
        printSetup.setUsePage(true);
        firstPageNotSet = false;
    } else {
        Integer documentFirstPageNumber = configuration.getFirstPageNumber();
        if (documentFirstPageNumber != null && documentFirstPageNumber > 0 && firstPageNotSet) {
            printSetup.setPageStart((short) documentFirstPageNumber.intValue());
            printSetup.setUsePage(true);
            firstPageNotSet = false;
        }
    }
    if (!firstPageNotSet
            && (sheet.getFooter().getCenter() == null || sheet.getFooter().getCenter().length() == 0)) {
        sheet.getFooter().setCenter("Page " + HeaderFooter.page());
    }

    boolean showGridlines = true;
    if (sheetInfo.sheetShowGridlines == null) {
        Boolean documentShowGridlines = configuration.isShowGridLines();
        if (documentShowGridlines != null) {
            showGridlines = documentShowGridlines;
        }
    } else {
        showGridlines = sheetInfo.sheetShowGridlines;
    }
    sheet.setDisplayGridlines(showGridlines);

    maxRowFreezeIndex = 0;
    maxColumnFreezeIndex = 0;

    onePagePerSheetMap.put(sheetIndex, configuration.isOnePagePerSheet());
    sheetsBeforeCurrentReportMap.put(sheetIndex, sheetsBeforeCurrentReport);
}

From source file:org.adempiere.impexp.AbstractExcelExporter.java

License:Open Source License

protected void formatPage(HSSFSheet sheet) {
    sheet.setFitToPage(true);/*from w w  w  . j ava  2 s.c  om*/
    // Print Setup
    HSSFPrintSetup ps = sheet.getPrintSetup();
    ps.setFitWidth((short) 1);
    ps.setNoColor(true);
    ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
    ps.setLandscape(false);
}

From source file:org.eclipse.scada.ae.ui.views.export.excel.impl.ExportEventsImpl.java

License:Open Source License

private HSSFSheet createSheet(final List<Event> events, final HSSFWorkbook workbook,
        final List<Field> columns) {
    final HSSFSheet sheet = workbook.createSheet(Messages.ExportImpl_ExcelSheet_Name);

    final HSSFHeader header = sheet.getHeader();
    header.setLeft(Messages.ExportImpl_ExcelSheet_Header);
    header.setRight(HeaderFooter.date() + " " + HeaderFooter.time());//$NON-NLS-1$

    final HSSFFooter footer = sheet.getFooter();
    footer.setLeft(String.format(Messages.ExportImpl_ExcelSheet_Footer_1, events.size()));

    footer.setRight(Messages.ExportImpl_ExcelSheet_Footer_2 + HeaderFooter.page()
            + Messages.ExportImpl_ExcelSheet_Footer_3 + HeaderFooter.numPages());

    makeHeader(columns, sheet);/*from w ww. ja v  a 2s .  c  o m*/

    final HSSFPrintSetup printSetup = sheet.getPrintSetup();
    printSetup.setLandscape(true);
    printSetup.setFitWidth((short) 1);
    printSetup.setFitHeight((short) 0);
    printSetup.setPaperSize(PrintSetup.A4_PAPERSIZE);

    sheet.setAutoFilter(new CellRangeAddress(0, 0, 0, columns.size() - 1));
    sheet.createFreezePane(0, 1);
    sheet.setFitToPage(true);
    sheet.setAutobreaks(true);

    printSetup.setFooterMargin(0.25);

    sheet.setMargin(Sheet.LeftMargin, 0.25);
    sheet.setMargin(Sheet.RightMargin, 0.25);
    sheet.setMargin(Sheet.TopMargin, 0.25);
    sheet.setMargin(Sheet.BottomMargin, 0.5);

    return sheet;
}

From source file:org.sharegov.cirm.utils.ExcelExportUtil.java

License:Apache License

public void exportData(OutputStream out, Json allData) throws IOException {
    //Set the filename
    Date dt = new Date();
    SimpleDateFormat fmt = new SimpleDateFormat("MM-dd-yyyy");
    String filename = fmt.format(dt);

    // Create Excel Workbook and Sheet
    HSSFWorkbook wb = new HSSFWorkbook();
    sheet = wb.createSheet(filename);// w  w w. j a v a  2  s.  c o m
    HSSFHeader header = sheet.getHeader();
    header.setCenter(filename);

    HSSFFont boldFont = wb.createFont();
    boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    HSSFCellStyle boldStyle = wb.createCellStyle();
    boldStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    boldStyle.setFont(boldFont);
    boldStyle.setWrapText(true);

    //Start : populate the spreadsheet
    int rowCounter = 0;
    rowCounter = searchCriteriaRows(allData, boldStyle);
    rowCounter = headerRow(allData, boldStyle, rowCounter);
    int headingsRowSplitter = rowCounter;
    rowCounter = dataRows(allData, rowCounter);
    //end : populate the spreadsheet

    // Freeze Panes on Header Row
    sheet.createFreezePane(0, headingsRowSplitter);
    // Row 1 Repeats on each page
    wb.setRepeatingRowsAndColumns(0, 0, 0, 0, headingsRowSplitter);

    // Set Print Area, Footer
    int colCount = allData.at("metaData").at("columns").asInteger();
    wb.setPrintArea(0, 0, colCount, 0, rowCounter);
    HSSFFooter footer = sheet.getFooter();
    footer.setCenter("Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages());
    // Fit Sheet to 1 page wide but very long
    sheet.setAutobreaks(true);
    HSSFPrintSetup ps = sheet.getPrintSetup();
    ps.setFitWidth((short) 1);
    ps.setFitHeight((short) 9999);
    sheet.setGridsPrinted(true);
    sheet.setHorizontallyCenter(true);
    ps.setPaperSize(HSSFPrintSetup.LETTER_PAPERSIZE);
    if (colCount > 5) {
        ps.setLandscape(true);
    }
    if (colCount > 10) {
        ps.setPaperSize(HSSFPrintSetup.LEGAL_PAPERSIZE);
    }
    if (colCount > 14) {
        ps.setPaperSize(HSSFPrintSetup.EXECUTIVE_PAPERSIZE);
    }
    // Set Margins
    ps.setHeaderMargin((double) .35);
    ps.setFooterMargin((double) .35);
    sheet.setMargin(HSSFSheet.TopMargin, (double) .50);
    sheet.setMargin(HSSFSheet.BottomMargin, (double) .50);
    sheet.setMargin(HSSFSheet.LeftMargin, (double) .50);
    sheet.setMargin(HSSFSheet.RightMargin, (double) .50);

    // Write out the spreadsheet
    wb.write(out);
    out.close();
}