Example usage for org.apache.poi.hssf.usermodel HSSFSheet setHorizontallyCenter

List of usage examples for org.apache.poi.hssf.usermodel HSSFSheet setHorizontallyCenter

Introduction

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

Prototype

@Override
public void setHorizontallyCenter(boolean value) 

Source Link

Document

determines whether the output is horizontally centered on the page.

Usage

From source file:model.Reports.java

public void generateXSL(List<SmsOutUserBean> smsOutUserBeans, int count) {
    try {//from   w  w w .  j a  va  2 s.c o  m

        HSSFWorkbook wb = new HSSFWorkbook();
        Map<String, CellStyle> styles = createStyles(wb);
        HSSFSheet sheet = wb.createSheet("Users_Sheet1");

        PrintSetup printSetup = sheet.getPrintSetup();
        printSetup.setLandscape(true);
        sheet.setFitToPage(true);
        sheet.setHorizontallyCenter(true);

        //title row
        Row titleRow = sheet.createRow(0);
        titleRow.setHeightInPoints(45);
        Cell titleCell = titleRow.createCell(0);
        titleCell.setCellValue("SMS OUT REPORT");
        titleCell.setCellStyle(styles.get("title"));
        sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$H$1"));

        String[] titles = { "Mobile", "Source Address", "Message", "Time Sent", "Last Update", "User", "Status",
                "Number of SMS", };

        HSSFRow row = sheet.createRow(1);
        row.setHeightInPoints(40);

        Cell headerCell;
        for (int i = 0; i < titles.length; i++) {
            headerCell = row.createCell(i);
            headerCell.setCellValue(titles[i]);
            headerCell.setCellStyle(styles.get("header"));
        }

        int rowNum = 2;
        for (SmsOutUserBean bean : smsOutUserBeans) {
            row = sheet.createRow(rowNum);
            row.createCell(0).setCellValue(bean.getSmsOutModel().getDestinationAddress());
            row.createCell(1).setCellValue(bean.getSmsOutModel().getSourceAddress());
            row.createCell(2).setCellValue(bean.getSmsOutModel().getMessagePayload());

            row.createCell(3)
                    .setCellValue(HelperUtil.conDateToString(bean.getSmsOutModel().getTimeSubmitted()));
            row.createCell(4)
                    .setCellValue(HelperUtil.conDateToString(bean.getSmsOutModel().getTimeProcessed()));

            row.createCell(5).setCellValue(bean.getUserBean().getUsername());
            row.createCell(6).setCellValue(bean.getSmsOutModel().getRealStatus());
            row.createCell(7).setCellValue(bean.getSmsOutModel().getSmsCount());

            rowNum++;
        }

        sheet.setColumnWidth(0, 20 * 256); //30 characters wide
        sheet.setColumnWidth(1, 15 * 256);
        for (int i = 2; i < 5; i++) {
            sheet.setColumnWidth(i, 20 * 256); //6 characters wide
        }
        sheet.setColumnWidth(5, 10 * 256);

        sheet.setColumnWidth(6, 20 * 256);
        sheet.setColumnWidth(7, 10 * 256); //10 characters wide

        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse res = (HttpServletResponse) context.getExternalContext().getResponse();
        res.setContentType("application/vnd.ms-excel");
        res.setHeader("Content-disposition", "attachment;filename=mydata.xls");

        ServletOutputStream out = res.getOutputStream();
        wb.write(out);
        out.flush();
        out.close();
        FacesContext.getCurrentInstance().responseComplete();
    } catch (Exception e) {
        e.printStackTrace();
    }
}