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

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

Introduction

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

Prototype

@Override
public void setColumnWidth(int columnIndex, int width) 

Source Link

Document

Set the width (in units of 1/256th of a character width)

The maximum column width for an individual cell is 255 characters.

Usage

From source file:is.idega.idegaweb.egov.course.business.CourseWriter.java

License:Open Source License

public MemoryFileBuffer writeXLS(IWContext iwc, Collection courses) throws Exception {
    MemoryFileBuffer buffer = new MemoryFileBuffer();
    MemoryOutputStream mos = new MemoryOutputStream(buffer);
    if (!courses.isEmpty()) {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet(StringHandler.shortenToLength(this.schoolTypeName, 30));
        sheet.setColumnWidth((short) 0, (short) (24 * 256));
        sheet.setColumnWidth((short) 1, (short) (24 * 256));
        sheet.setColumnWidth((short) 2, (short) (8 * 256));
        sheet.setColumnWidth((short) 3, (short) (8 * 256));
        sheet.setColumnWidth((short) 4, (short) (12 * 256));
        sheet.setColumnWidth((short) 5, (short) (12 * 256));
        sheet.setColumnWidth((short) 6, (short) (8 * 256));
        sheet.setColumnWidth((short) 7, (short) (8 * 256));
        sheet.setColumnWidth((short) 8, (short) (20 * 256));
        HSSFFont font = wb.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        font.setFontHeightInPoints((short) 12);
        HSSFCellStyle style = wb.createCellStyle();
        style.setFont(font);//from www  .j a v a2s.c  o  m

        HSSFFont bigFont = wb.createFont();
        bigFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        bigFont.setFontHeightInPoints((short) 13);
        HSSFCellStyle bigStyle = wb.createCellStyle();
        bigStyle.setFont(bigFont);

        int cellRow = 0;
        HSSFRow row = sheet.createRow(cellRow++);
        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue(this.schoolTypeName);
        cell.setCellStyle(bigStyle);
        cell = row.createCell((short) 1);

        row = sheet.createRow(cellRow++);

        short iCell = 0;
        row = sheet.createRow(cellRow++);
        cell = row.createCell(iCell++);
        cell.setCellValue(this.iwrb.getLocalizedString("type", "Type"));
        cell.setCellStyle(style);
        cell = row.createCell(iCell++);
        cell.setCellValue(this.iwrb.getLocalizedString("course", "Course"));
        cell.setCellStyle(style);
        cell = row.createCell(iCell++);
        cell.setCellValue(this.iwrb.getLocalizedString("from", "From"));
        cell.setCellStyle(style);
        cell = row.createCell(iCell++);
        cell.setCellValue(this.iwrb.getLocalizedString("to", "To"));
        cell.setCellStyle(style);
        cell = row.createCell(iCell++);
        cell.setCellValue(this.iwrb.getLocalizedString("date_from", "Date from"));
        cell.setCellStyle(style);
        cell = row.createCell(iCell++);
        cell.setCellValue(this.iwrb.getLocalizedString("date_to", "Date to"));
        cell.setCellStyle(style);
        cell = row.createCell(iCell++);
        cell.setCellValue(this.iwrb.getLocalizedString("max", "Max"));
        cell.setCellStyle(style);
        cell = row.createCell(iCell++);
        cell.setCellValue(this.iwrb.getLocalizedString("free_places", "Free places"));
        cell.setCellStyle(style);
        cell = row.createCell(iCell++);
        cell.setCellValue(this.iwrb.getLocalizedString("employee", "Employee"));
        cell.setCellStyle(style);

        Iterator iter = courses.iterator();
        while (iter.hasNext()) {
            row = sheet.createRow(cellRow++);
            iCell = 0;

            Course course = (Course) iter.next();
            CourseType type = course.getCourseType();
            CoursePrice price = course.getPrice();
            IWTimestamp dateFrom = new IWTimestamp(course.getStartDate());
            IWTimestamp dateTo = new IWTimestamp(course.getStartDate());
            dateTo.addDays(price.getNumberOfDays());

            row.createCell(iCell++).setCellValue(type.getName());
            row.createCell(iCell++).setCellValue(course.getName());
            row.createCell(iCell++).setCellValue(String.valueOf(course.getBirthyearFrom()));
            row.createCell(iCell++).setCellValue(String.valueOf(course.getBirthyearTo()));
            row.createCell(iCell++)
                    .setCellValue(dateFrom.getLocaleDate(iwc.getCurrentLocale(), IWTimestamp.SHORT));
            row.createCell(iCell++)
                    .setCellValue(dateTo.getLocaleDate(iwc.getCurrentLocale(), IWTimestamp.SHORT));
            row.createCell(iCell++).setCellValue(String.valueOf(course.getMax()));
            row.createCell(iCell++).setCellValue(String.valueOf(business.getNumberOfFreePlaces(course)));
            row.createCell(iCell++).setCellValue("-");
        }
        wb.write(mos);
    }
    buffer.setMimeType(MimeTypeUtil.MIME_TYPE_EXCEL_2);
    return buffer;
}

From source file:ke.co.mspace.nonsmppmanager.service.SMSOutServiceImpl.java

@Override
public void generateXSL(String user, String startDate, String endDate) {
    try {/*w  w w.j av  a 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 Spent", "Last Update", "User",
                "Status", "No. 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"));
        }

        List<SMSOut> exportSMSOutReport = (List) userSMSOutReport(user, startDate, endDate).get("result");
        int rowNum = 2;

        for (SMSOut anSMS : exportSMSOutReport) {
            row = sheet.createRow(rowNum);
            row.createCell(0).setCellValue(anSMS.getDestinationAddr());
            row.createCell(1).setCellValue(anSMS.getSourceAddr());
            row.createCell(2).setCellValue(anSMS.getMessagePayload());
            row.createCell(3).setCellValue(anSMS.getTimeSubmitted());
            row.createCell(4).setCellValue(anSMS.getTimeProcessed());

            row.createCell(5).setCellValue(anSMS.getUser());
            row.createCell(6).setCellValue(anSMS.getRealStatus());
            row.createCell(7).setCellValue(anSMS.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.xlsx");

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

From source file:kitt.admin.controller.UserController.java

@RequestMapping(value = "/downloadData")
@Authority(role = AuthenticationRole.Service)
@Authority(role = AuthenticationRole.TraderAssistant)
@Authority(role = AuthenticationRole.LegalPersonnel)
@Authority(role = AuthenticationRole.Admin)
@Authority(role = AuthenticationRole.Operation)
public void downloadUserData(String status,
        @RequestParam(value = "securephone", required = false, defaultValue = "") String securephone,
        @RequestParam(value = "clienttype", required = false, defaultValue = "0") int clienttype,
        @RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
        @RequestParam(value = "endDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
        HttpServletRequest request, HttpServletResponse response) throws IOException, DocumentException {
    List<Map<String, Object>> users = userMapper.userExport(status, Where.$like$(securephone), clienttype,
            startDate, endDate);/*from  w  w  w.jav a  2  s . c  om*/
    String filename = status + "?";
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet(filename);
    HSSFRow row = sheet.createRow(0);
    CellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
    cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    sheet.setVerticallyCenter(true);
    sheet.setHorizontallyCenter(true);
    sheet.setColumnWidth(0, 1200);
    sheet.setColumnWidth(1, 3600);
    sheet.setColumnWidth(2, 8000);
    sheet.setColumnWidth(3, 4500);
    sheet.setColumnWidth(4, 4500);
    String[] excelHeader = { "??", "", "??", "??",
            "" };
    for (int i = 0; i < excelHeader.length; i++) {
        sheet.autoSizeColumn(i, true);
        HSSFCell cell = row.createCell(i);
        cell.setCellValue(excelHeader[i]);
        cell.setCellStyle(cellStyle);
    }
    for (int i = 0; i < users.size(); i++) {
        Map<String, Object> resultSet = users.get(i);
        sheet.autoSizeColumn(i, true);
        row = sheet.createRow(i + 1);
        row.setRowStyle(cellStyle);
        row.createCell(0).setCellValue(i + 1);
        row.createCell(1).setCellValue(String.valueOf(resultSet.get("tradername")).equals("null") ? ""
                : String.valueOf(resultSet.get("tradername")));
        row.createCell(2).setCellValue(String.valueOf(resultSet.get("companyname")));
        row.createCell(3).setCellValue(String.valueOf(resultSet.get("securephone")));
        row.createCell(4).setCellValue(String.valueOf(resultSet.get("verifytime")));
    }
    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/x-download");
    filename += LocalDate.now() + ".xls";
    if (request.getHeader("user-agent").toLowerCase().contains("firefox")) {
        filename = new String(filename.getBytes("UTF-8"), "ISO-8859-1");
    } else {
        filename = URLEncoder.encode(filename, "UTF-8");
    }
    response.addHeader("Content-Disposition", "attachment; filename=" + filename);
    OutputStream out = response.getOutputStream();
    wb.write(out);
    out.close();
}

From source file:kr.co.blackducksoftware.rg.displayexcel.Ex.java

License:Open Source License

/**
 * OSSW_BOM written by byunghoon/*  ww  w.  j a va2s  .  c om*/
 *  
 */
public static void ossw_BOM(HSSFWorkbook wb) {

    LogMaker.makelog("Ex.java Making OSSW_BOM Sheet");
    String Empty = null;

    try {
        HSSFSheet sheet = wb.createSheet("?  ?(Bill of Materials");

        HSSFRow row1 = sheet.createRow(1);
        row1.setHeight((short) 800);
        row1.createCell(0, HSSFCell.CELL_TYPE_STRING).setCellValue(" ");
        row1.getCell(0).setCellStyle(Style.osswBOMCellStyel_1);
        sheet.setColumnWidth(0, 7000);
        row1.createCell(1, HSSFCell.CELL_TYPE_STRING).setCellValue("?");
        row1.getCell(1).setCellStyle(Style.osswBOMCellStyel_1);
        sheet.setColumnWidth(1, 7000);
        row1.createCell(2, HSSFCell.CELL_TYPE_STRING).setCellValue("");
        row1.getCell(2).setCellStyle(Style.osswBOMCellStyel_1);
        sheet.setColumnWidth(2, 7000);
        row1.createCell(3, HSSFCell.CELL_TYPE_STRING).setCellValue("?");
        row1.getCell(3).setCellStyle(Style.osswBOMCellStyel_1);
        sheet.setColumnWidth(3, 15000);
        row1.createCell(4, HSSFCell.CELL_TYPE_STRING).setCellValue("??");
        row1.getCell(4).setCellStyle(Style.osswBOMCellStyel_1);
        sheet.setColumnWidth(4, 7000);
        row1.createCell(5, HSSFCell.CELL_TYPE_STRING).setCellValue("");
        row1.getCell(5).setCellStyle(Style.osswBOMCellStyel_1);
        sheet.setColumnWidth(5, 7000);
        row1.createCell(6, HSSFCell.CELL_TYPE_STRING).setCellValue("? ");
        row1.getCell(6).setCellStyle(Style.osswBOMCellStyel_1);
        sheet.setColumnWidth(6, 7000);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:mat.server.service.impl.XLSGenerator.java

/** Adds the disclaimer.
 * //from www.  j  ava  2s  .  c o  m
 * @param wkbk - HSSFWorkbook. * */
protected final void addDisclaimer(final HSSFWorkbook wkbk) {
    String disclaimerText = "The codes that you are exporting directly reflect the codes you entered into the "
            + "Measure Authoring Tool.  These codes may be owned by a third party and "
            + "subject to copyright or other intellectual property restrictions.  Use of these "
            + "codes may require permission from the code owner or agreement to a license.  "
            + "It is your responsibility to ensure that your use of any third party code is "
            + "permissible and that you have fulfilled any notice or license requirements "
            + "imposed by the code owner.  Use of the Measure Authoring Tool does not "
            + "confer any rights on you with respect to these codes other than those codes that may "
            + "be available from the code owner.";
    HSSFSheet wkst = wkbk.createSheet("Disclaimer");
    HSSFRow row = wkst.createRow(0);
    row.createCell(0, HSSFCell.CELL_TYPE_STRING).setCellValue(disclaimerText);
    wkst.setColumnWidth(0, (75 * 256));
    HSSFCell cell = row.getCell(0);
    HSSFCellStyle style = wkbk.createCellStyle();
    style.setWrapText(true);
    cell.setCellStyle(style);
}

From source file:mat.server.service.impl.XLSGenerator.java

/** Size column.
 * // ww w  . java  2  s  .co m
 * @param wkst - HSSFSheet.
 * @param col - Short. * */
private void sizeColumn(final HSSFSheet wkst, final short col) {
    try {
        wkst.autoSizeColumn(col);
    } catch (Exception e) {
        wkst.setColumnWidth(col, (256 * 255));
    }
}

From source file:matriz.core.GerarXLS.java

public static void expExcel(String nomeArquivo, String demanda, List<LineMatriz> linhastabela) {

    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet firstSheet = workbook.createSheet("Aba1");

    HSSFFont fontBranca = workbook.createFont();
    fontBranca.setFontHeightInPoints((short) 16);
    fontBranca.setColor(HSSFColor.WHITE.index);
    fontBranca.setBoldweight(Font.BOLDWEIGHT_BOLD);

    HSSFFont fontBrancaMenor = workbook.createFont();
    fontBrancaMenor.setFontHeightInPoints((short) 10);
    fontBrancaMenor.setColor(HSSFColor.WHITE.index);
    fontBrancaMenor.setBoldweight(Font.BOLDWEIGHT_BOLD);

    FileOutputStream fos = null;/*w ww  . j a v a  2  s . co  m*/
    nomeArquivo = nomeArquivo + "/Matriz de Rastreabilidade " + demanda + ".xls";

    try {
        fos = new FileOutputStream(new File(nomeArquivo));

        HSSFRow rowa = firstSheet.createRow(0);
        rowa.createCell(1);

        // criar titulo
        HSSFRow row0 = firstSheet.createRow(1);
        HSSFCellStyle style = workbook.createCellStyle();

        style.setFont(fontBranca);
        style.setFillForegroundColor(HSSFColor.DARK_BLUE.index);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);

        row0.createCell(0).setCellValue("Matriz de classes alteradas na " + demanda);
        row0.getCell(0).setCellStyle(style);

        //nome das colunas
        HSSFRow row1 = firstSheet.createRow(2);
        HSSFCellStyle style2 = workbook.createCellStyle();
        style2.setFillForegroundColor(HSSFColor.DARK_BLUE.index);
        style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style2.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);
        style2.setFont(fontBrancaMenor);

        style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style2.setBorderRight(HSSFCellStyle.BORDER_THIN);

        row1.createCell(0).setCellValue("Sistema");
        row1.createCell(1).setCellValue("Mdulo");
        row1.createCell(2).setCellValue("Diretrio");
        row1.createCell(3).setCellValue("Arquivo");
        row1.createCell(4).setCellValue("Ambiente");
        row1.createCell(5).setCellValue("Tipo do Arquivo");
        row1.createCell(6).setCellValue("Ao");
        row1.createCell(7).setCellValue("Data");
        row1.createCell(8).setCellValue("Reviso SVN");
        row1.createCell(9).setCellValue("Autor");
        row1.createCell(10).setCellValue("Motivo da alterao");

        row1.getCell(0).setCellStyle(style2);
        row1.getCell(1).setCellStyle(style2);
        row1.getCell(2).setCellStyle(style2);
        row1.getCell(3).setCellStyle(style2);
        row1.getCell(4).setCellStyle(style2);
        row1.getCell(5).setCellStyle(style2);
        row1.getCell(6).setCellStyle(style2);
        row1.getCell(7).setCellStyle(style2);
        row1.getCell(8).setCellStyle(style2);
        row1.getCell(9).setCellStyle(style2);
        row1.getCell(10).setCellStyle(style2);

        firstSheet.setColumnWidth(0, 10000);
        firstSheet.setColumnWidth(1, 9000);
        firstSheet.setColumnWidth(2, 10000);
        firstSheet.setColumnWidth(3, 20000);
        firstSheet.setColumnWidth(4, 10000);
        firstSheet.setColumnWidth(5, 4000);
        firstSheet.setColumnWidth(6, 4000);
        firstSheet.setColumnWidth(7, 4000);
        firstSheet.setColumnWidth(8, 4000);
        firstSheet.setColumnWidth(9, 6000);
        firstSheet.setColumnWidth(10, 10000);

        HSSFCellStyle style3 = workbook.createCellStyle();
        style3.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style3.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style3.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style3.setBorderRight(HSSFCellStyle.BORDER_THIN);

        int i = 3;

        for (LineMatriz linha : linhastabela) {
            HSSFRow row = firstSheet.createRow(i);

            row.createCell(0).setCellValue(linha.getSistema());
            row.createCell(1).setCellValue(linha.getModulo());
            row.createCell(2).setCellValue(linha.getDiretorio());
            row.createCell(3).setCellValue(linha.getArquivo());
            row.createCell(4).setCellValue(linha.getAmbiente());
            row.createCell(5).setCellValue(linha.getTipoArquivo());
            row.createCell(6).setCellValue(linha.getAcao());
            row.createCell(7).setCellValue(linha.getData());
            row.createCell(8).setCellValue(linha.getRevisao());
            row.createCell(9).setCellValue(linha.getAutor());
            row.createCell(10).setCellValue(linha.getMotivo());

            row.getCell(0).setCellStyle(style3);
            row.getCell(1).setCellStyle(style3);
            row.getCell(2).setCellStyle(style3);
            row.getCell(3).setCellStyle(style3);
            row.getCell(4).setCellStyle(style3);
            row.getCell(5).setCellStyle(style3);
            row.getCell(6).setCellStyle(style3);
            row.getCell(7).setCellStyle(style3);
            row.getCell(8).setCellStyle(style3);
            row.getCell(9).setCellStyle(style3);
            row.getCell(10).setCellStyle(style3);

            i++;

        } // fim do for

        firstSheet.addMergedRegion(new CellRangeAddress(1, //first row (0-based)
                1, //last row  (0-based)
                0, //first column (0-based)
                10 //last column  (0-based)
        ));

        workbook.write(fos);
        JOptionPane.showMessageDialog(null, "Arquivo criado em " + nomeArquivo);

    } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, e.getMessage());
        System.out.println("Erro ao exportar arquivo");
    } finally {
        try {
            fos.flush();
            fos.close();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }
}

From source file:model.Reports.java

public void generateXSL(List<SmsOutUserBean> smsOutUserBeans, int count) {
    try {/* w w w  .j  a v  a 2  s  .com*/

        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();
    }
}

From source file:neg.JRViewerComercial.java

License:Open Source License

private static void crearCabeceraHojaExcel(HSSFWorkbook libro, HSSFSheet hoja) {
    HSSFRow fila = null;//from   ww w  . j a v  a 2s .co  m
    HSSFCell celda = null;

    // Modificamos la fuente por defecto para que salga en negrita
    HSSFCellStyle cs = libro.createCellStyle();
    HSSFFont f = libro.createFont();
    f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    f.setColor(HSSFColor.WHITE.index);
    cs.setFont(f);
    //cs.setFillBackgroundColor(HSSFColor.GREEN.index);
    cs.setFillForegroundColor(HSSFColor.GREEN.index);
    cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    cs.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
    cs.setBottomBorderColor(HSSFColor.BLACK.index);
    cs.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
    cs.setLeftBorderColor(HSSFColor.BLACK.index);
    cs.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
    cs.setRightBorderColor(HSSFColor.BLACK.index);
    cs.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
    cs.setTopBorderColor(HSSFColor.BLACK.index);

    cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    // Creamos la cabecera de las columnas
    fila = hoja.createRow(0);

    celda = fila.createCell((short) 0);
    celda.setCellStyle(cs);
    HSSFRichTextString texto = new HSSFRichTextString("CLIENTE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 0, (short) ((220 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 1);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("ENERO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 1, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 2);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("FEBRERO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 2, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 3);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("MARZO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 3, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 4);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("ABRIL");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 4, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 5);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("MAYO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 5, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 6);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("JUNIO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 6, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 7);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("JULIO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 7, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 8);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("AGOSTO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 8, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 9);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("SEPTIEMBRE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 9, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 10);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("OCTUBRE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 10, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 11);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("NOVIEMBRE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 11, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 12);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("DICIEMBRE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 12, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 13);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("IMPORTE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 13, (short) ((90 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 14);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("MARGEN");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 14, (short) ((90 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 15);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("MARGEN %");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 15, (short) ((90 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 16);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("M.G. PEDIDO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 16, (short) ((90 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 17);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("NM.PEDIDOS");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 17, (short) ((90 * 2) / ((double) 1 / 20)));

}

From source file:neg.JRViewerComercialProveedor.java

License:Open Source License

private static void crearCabeceraHojaExcel(HSSFWorkbook libro, HSSFSheet hoja) {
    HSSFRow fila = null;/*  w w w . j  av a 2 s .  c o  m*/
    HSSFCell celda = null;

    // Modificamos la fuente por defecto para que salga en negrita
    HSSFCellStyle cs = libro.createCellStyle();
    HSSFFont f = libro.createFont();
    f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    f.setColor(HSSFColor.WHITE.index);
    cs.setFont(f);
    //cs.setFillBackgroundColor(HSSFColor.GREEN.index);
    cs.setFillForegroundColor(HSSFColor.GREEN.index);
    cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    cs.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
    cs.setBottomBorderColor(HSSFColor.BLACK.index);
    cs.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
    cs.setLeftBorderColor(HSSFColor.BLACK.index);
    cs.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
    cs.setRightBorderColor(HSSFColor.BLACK.index);
    cs.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
    cs.setTopBorderColor(HSSFColor.BLACK.index);

    cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    // Creamos la cabecera de las columnas
    fila = hoja.createRow(0);

    celda = fila.createCell((short) 0);
    celda.setCellStyle(cs);
    HSSFRichTextString texto = new HSSFRichTextString("PROVEEDOR");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 0, (short) ((220 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 1);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("ENERO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 1, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 2);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("FEBRERO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 2, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 3);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("MARZO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 3, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 4);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("ABRIL");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 4, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 5);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("MAYO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 5, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 6);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("JUNIO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 6, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 7);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("JULIO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 7, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 8);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("AGOSTO");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 8, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 9);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("SEPTIEMBRE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 9, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 10);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("OCTUBRE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 10, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 11);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("NOVIEMBRE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 11, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 12);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("DICIEMBRE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 12, (short) ((70 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 13);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("IMPORTE");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 13, (short) ((90 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 14);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("% TURISMOS");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 14, (short) ((90 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 15);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("% FUNCIONAN");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 15, (short) ((90 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 16);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("%GRA UNITARIA");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 16, (short) ((90 * 2) / ((double) 1 / 20)));

    celda = fila.createCell((short) 17);
    celda.setCellStyle(cs);
    texto = new HSSFRichTextString("NM.PEDIDOS");
    celda.setCellValue(texto);
    hoja.setColumnWidth((short) 17, (short) ((90 * 2) / ((double) 1 / 20)));

}