Example usage for org.apache.poi.hssf.usermodel HSSFRow createCell

List of usage examples for org.apache.poi.hssf.usermodel HSSFRow createCell

Introduction

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

Prototype

@Override
public HSSFCell createCell(int column) 

Source Link

Document

Use this to create new cells within the row and return it.

Usage

From source file:controller.VisitasController.java

public void exportExcel() {

    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Open Resource File");
    fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
    fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("XLS", "*.xls"));
    File f = fileChooser.showSaveDialog(null);

    try {//from   w  w  w  . jav a 2 s.c o m
        String filename = f.getAbsolutePath();
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("FirstSheet");
        int bool = 1;

        InputStream inputStream = getClass().getResourceAsStream("/images/excel-logo.jpg");

        byte[] imageBytes = IOUtils.toByteArray(inputStream);

        int pictureureIdx = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_JPEG);

        inputStream.close();

        CreationHelper helper = workbook.getCreationHelper();

        Drawing drawing = sheet.createDrawingPatriarch();

        ClientAnchor anchor = helper.createClientAnchor();

        anchor.setCol1(1);
        anchor.setRow1(0);

        Picture pict = drawing.createPicture(anchor, pictureureIdx);

        HSSFRow rowhead = sheet.createRow(8);

        rowhead.createCell(0);
        rowhead.createCell(1).setCellValue("Cedula");
        rowhead.createCell(2).setCellValue("Cliente");
        rowhead.createCell(3).setCellValue("Contrato");
        rowhead.createCell(4).setCellValue("Plan");
        rowhead.createCell(5).setCellValue("Fecha");
        rowhead.createCell(6).setCellValue("Hora");
        rowhead.createCell(7).setCellValue("Invitados");
        makeRowBold(workbook, rowhead);

        for (int i = 0; i < table.getItems().size(); i++) {
            HSSFRow row = sheet.createRow(i + 9);
            Asistencia a = (Asistencia) table.getItems().get(i);
            row.createCell(1).setCellValue(a.getCedula());
            row.createCell(2).setCellValue(a.getNombre());
            row.createCell(3).setCellValue(a.getContrato());
            row.createCell(4).setCellValue(a.getPlan());
            row.createCell(5).setCellValue(a.getFecha());
            row.createCell(6).setCellValue(a.getHora());
            row.createCell(7).setCellValue(Integer.parseInt(a.getInvitados()));
            centerRow(workbook, row);
        }
        autoSizeColumns(workbook);
        pict.resize();
        FileOutputStream fileOut = new FileOutputStream(filename);
        workbook.write(fileOut);
        fileOut.close();

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

}

From source file:controllers.transformer.ExcelTransformer.java

License:Open Source License

public byte[] getBytes() {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("Sheet1");

    if (results == null) {
        results = survey.resultCollection;
    }/*w  ww .  j a  v a  2s  . c  o m*/

    /** Header **/
    HSSFRow row = sheet.createRow(0);
    int fieldcounter = 0;
    row.createCell(fieldcounter++).setCellValue("ResultId");
    row.createCell(fieldcounter++).setCellValue("SurveyId");
    row.createCell(fieldcounter++).setCellValue("Title");
    row.createCell(fieldcounter++).setCellValue("Start time");
    row.createCell(fieldcounter++).setCellValue("End time");
    row.createCell(fieldcounter++).setCellValue("Date Sent");
    row.createCell(fieldcounter++).setCellValue("User");
    row.createCell(fieldcounter++).setCellValue("Phone Number");
    row.createCell(fieldcounter++).setCellValue("Lat");
    row.createCell(fieldcounter++).setCellValue("Lon");

    /** Header Fields**/
    for (Question question : survey.getQuestions()) {
        row.createCell(fieldcounter++).setCellValue(question.label);
    }

    int countrow = 0;
    row = sheet.createRow(++countrow);

    //SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss Z");

    for (NdgResult result : results) {
        fieldcounter = 0;
        row.createCell(fieldcounter++).setCellValue(result.resultId);
        row.createCell(fieldcounter++).setCellValue(result.survey.surveyId);
        row.createCell(fieldcounter++).setCellValue(result.title);
        row.createCell(fieldcounter++).setCellValue(dateFormat.format(result.startTime));
        row.createCell(fieldcounter++).setCellValue(dateFormat.format(result.endTime));

        if (result.dateSent != null) {
            row.createCell(fieldcounter++).setCellValue(dateFormat.format(result.dateSent));
        } else {
            row.createCell(fieldcounter++).setCellValue("");
        }

        row.createCell(fieldcounter++).setCellValue(result.ndgUser.username);
        row.createCell(fieldcounter++).setCellValue(result.ndgUser.phoneNumber);
        row.createCell(fieldcounter++).setCellValue(result.latitude);
        row.createCell(fieldcounter++).setCellValue(result.longitude);

        for (Question question : survey.getQuestions()) {//to ensure right answer order
            Collection<Answer> answers = CollectionUtils.intersection(question.answerCollection,
                    result.answerCollection);//only one should left, hope that it does not modify results
            if (answers.isEmpty()) {
                row.createCell(fieldcounter++).setCellValue("");
            } else if (answers.size() == 1) {
                Answer answer = answers.iterator().next();
                if (answer.question.questionType.typeName.equalsIgnoreCase(QuestionTypesConsts.IMAGE)) {//TODO handle other binary data
                    row.createCell(fieldcounter++).setCellValue(storeImagesAndGetValueToExport(survey.surveyId,
                            result.resultId, answer.id, answer.binaryData));
                } else if (answer.question.questionType.typeName.equalsIgnoreCase(QuestionTypesConsts.INT)) {
                    Integer value = Integer.valueOf(answer.textData);
                    row.createCell(fieldcounter++).setCellValue(value);
                } else if (answer.question.questionType.typeName
                        .equalsIgnoreCase(QuestionTypesConsts.DECIMAL)) {
                    Float value = Float.valueOf(answer.textData);
                    row.createCell(fieldcounter++).setCellValue(value);
                } else {
                    String value = answer.textData;
                    value = value.trim().replaceAll("\n", "");
                    row.createCell(fieldcounter++).setCellValue(value);
                }
            } else {
                Logger.getAnonymousLogger().log(Level.WARNING,
                        "to many answers. ResID={0}questioId={1}answerCount={2}",
                        new Object[] { result.resultId, question.id, question.answerCollection.size() });
                break;
            }
        }
        row = sheet.createRow(++countrow);
    }
    try {
        wb.write(out);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return out.toByteArray();
}

From source file:corner.orm.tapestry.service.excel.ExcelService.java

License:Apache License

/**
 * exceltitle/*from  w  w w  .ja v  a2 s .c o m*/
 * 
 * @param wb
 * @param row
 * @param index
 * @param cellValue
 */
protected void createTitleCell(HSSFWorkbook wb, HSSFRow row, int index, String cellValue) {
    HSSFCell titlecell = row.createCell((short) index);
    if (titlecell.getCellStyle() == null) //??????
        titlecell.setCellStyle(this.getTitleStyle(wb));
    titlecell.setCellValue(new HSSFRichTextString(cellValue));
}

From source file:corner.orm.tapestry.service.excel.ExcelService.java

License:Apache License

/**
 * excelcontent//  ww w . j  ava2s  .c  om
 * 
 * @param wb
 * @param row
 * @param index
 * @param cellValue
 */
protected void createContentCell(HSSFWorkbook wb, HSSFRow row, int index, String cellValue) {
    HSSFCell contentcell = row.createCell((short) index);
    if (contentcell.getCellStyle() == null) //??????
        contentcell.setCellStyle(this.getContentCellStyle(wb));
    contentcell.setCellValue(new HSSFRichTextString(cellValue));
}

From source file:cuenta.CuentaAhorros.java

@Override
public void toExcel() {
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet();
    HSSFRow row = sheet.createRow(0);
    HSSFCell cell = row.createCell(0);
    cell.setCellValue(this.toString());
    try {/*from w  w  w .j  a  v  a 2 s .  c o  m*/
        OutputStream out = new FileOutputStream("CuentaAhorros.xls");
        workbook.write(out);
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("Se gener el documento EXCEL de la CUENTA DE AHORROS en la raz del proyecto");
}

From source file:cuenta.CuentaCorriente.java

@Override
public void toExcel() {
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet();
    HSSFRow row = sheet.createRow(0);
    HSSFCell cell = row.createCell(0);
    cell.setCellValue(this.toString());
    try {/*ww  w.  j av  a2 s.co m*/
        OutputStream out = new FileOutputStream("CuentaCorriente.xls");
        workbook.write(out);
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("Se gener el documento EXCEL de la CUENTA CORRIENTE en la raz del proyecto");
}

From source file:cz.lbenda.dataman.db.ExportTableData.java

License:Apache License

/** Write rows to XLS file
 * @param sqlQueryRows rows// w  ww. j a va 2  s .  c o m
 * @param sheetName name of sheet where is data write
 * @param outputStream stream where are data write */
public static void writeSqlQueryRowsToXLS(SQLQueryRows sqlQueryRows, String sheetName,
        OutputStream outputStream) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet(sheetName);
    HSSFRow headerRow = sheet.createRow(0);
    int c = 0;
    for (ColumnDesc columnDesc : sqlQueryRows.getMetaData().getColumns()) {
        HSSFCell cell = headerRow.createCell(c);
        cell.setCellValue(columnDesc.getName());
        c++;
    }
    int r = 1;
    for (RowDesc row : sqlQueryRows.getRows()) {
        HSSFRow xlsxRow = sheet.createRow(r);
        c = 0;
        for (ColumnDesc columnDesc : sqlQueryRows.getMetaData().getColumns()) {
            HSSFCell cell = xlsxRow.createCell(c);
            cell.setCellValue(row.getColumnValueStr(columnDesc));
            c++;
        }
        r++;
    }
    wb.write(outputStream);
}

From source file:dao.ExportacaoParaExcel.java

private void AdicionaLinha(HSSFRow row, int coluna, double valor) {

    //Linha alimenta novo valor
    row.createCell(coluna).setCellValue(valor);

    firstSheet.autoSizeColumn(coluna);/* www  . j av  a 2  s  .c  om*/

}

From source file:dao.ExportacaoParaExcel.java

private void AdicionaLinha(HSSFRow row, int coluna, String valor) {

    //Linha alimenta novo valor
    row.createCell(coluna).setCellValue(valor);

    firstSheet.autoSizeColumn(coluna);// ww  w. j a va2 s .  c o  m

}

From source file:dao.ExportacaoParaExcel.java

private void AdicionaLinhaBold(HSSFRow row, HSSFCellStyle style, int coluna, String valor) {

    //Linha alimenta novo valor
    row.createCell(coluna).setCellValue(valor);
    row.getCell(coluna).setCellStyle(style);

    firstSheet.autoSizeColumn(coluna);//  www  . ja  v  a 2 s. c  om

}