Example usage for org.apache.poi.hssf.usermodel HSSFCell setCellValue

List of usage examples for org.apache.poi.hssf.usermodel HSSFCell setCellValue

Introduction

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

Prototype

@SuppressWarnings("fallthrough")
public void setCellValue(boolean value) 

Source Link

Document

set a boolean value for the cell

Usage

From source file:TestUtil.java

License:BSD License

private void writeCSI(Node csiNode, HSSFWorkbook wb) {
    HSSFSheet sheet = wb.getSheet("tbl_CS_ITEMS");
    HSSFRow row = sheet.getRow(10);/*from   w w w  .j  av a 2 s. com*/
    HSSFCell prefNameCell = row.createCell(6);
    HSSFCell longNameCell = row.createCell(7);
    HSSFCell prefDefCell = row.createCell(8);

    prefNameCell.setCellValue(new HSSFRichTextString(csiNode.getTextContent()));
    longNameCell.setCellValue(new HSSFRichTextString(csiNode.getTextContent()));
    prefDefCell.setCellValue(new HSSFRichTextString(csiNode.getTextContent()));
}

From source file:TestUtil.java

License:BSD License

private void writeCon(List<Node[]> conIdNodes, HSSFWorkbook wb) {
    HSSFSheet sheet = wb.getSheet("tbl_CONCEPTS_EXT");
    int conRowNum = 9;
    int id = 1110;

    int j = -1;/* www  . j ava2 s.com*/

    for (int i = 0; i < conIdNodes.size(); i++) {
        Node[] conIdRow = conIdNodes.get(i);
        Node conIdNode = conIdRow[0];

        String conIdRep = conIdNode.getTextContent();
        String[] concepts = conIdRep.split(";");

        for (String concept : concepts) {
            j++;
            String[] cdeIdParts = concept.split(":");

            if (cdeIdParts != null && cdeIdParts.length >= 2 && !conIds.contains(cdeIdParts[0])) {
                String cdeId = cdeIdParts[0].trim();
                String longName = cdeIdParts[1].trim();
                conIds.add(cdeId);
                conRowNum++;
                id++;

                HSSFRow row = sheet.createRow(conRowNum);

                HSSFCell seqIdCell = row.createCell(1);
                HSSFCell versionCell = row.createCell(6);
                HSSFCell ctxIdCell = row.createCell(5);
                HSSFCell prefNameCell = row.createCell(2);
                HSSFCell longNameCell = row.createCell(3);
                HSSFCell prefDefCell = row.createCell(4);
                HSSFCell aslNameCell = row.createCell(7);
                HSSFCell idCell = row.createCell(8);
                HSSFCell defSourceCell = row.createCell(9);

                StringBuffer conId = new StringBuffer("CON" + j);

                while (conId.length() < 36) {
                    conId.append("x");
                }

                seqIdCell.setCellValue(new HSSFRichTextString(conId.toString()));
                ctxIdCell.setCellValue(new HSSFRichTextString("6BF1D8AD-29FB-6CF3-E040-A8C0955834A9"));
                idCell.setCellValue(Double.parseDouble(id + ""));
                versionCell.setCellValue(Double.parseDouble("1.0"));

                prefNameCell.setCellValue(new HSSFRichTextString(cdeId));
                longNameCell.setCellValue(new HSSFRichTextString(longName));
                prefDefCell.setCellValue(new HSSFRichTextString("DEC Pref Def" + i));
                aslNameCell.setCellValue(new HSSFRichTextString("RELEASED"));
                defSourceCell.setCellValue(new HSSFRichTextString("NCI"));
            }
        }
    }
}

From source file:Console.java

static public void exportToExcel(String sheetName, ArrayList headers, ArrayList data, File outputFile)
        throws HPSFException {

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet(sheetName);

    int rowIdx = 0;
    short cellIdx = 0;

    // Header/*from  ww  w.jav  a  2 s  .co m*/
    HSSFRow hssfHeader = sheet.createRow(rowIdx);
    HSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    for (Iterator cells = headers.iterator(); cells.hasNext();) {
        HSSFCell hssfCell = hssfHeader.createCell(cellIdx++);
        hssfCell.setCellStyle(cellStyle);
        hssfCell.setCellValue((String) cells.next());
    }
    // Data
    rowIdx = 1;
    for (Iterator rows = data.iterator(); rows.hasNext();) {
        ArrayList row = (ArrayList) rows.next();
        HSSFRow hssfRow = sheet.createRow(rowIdx++);
        cellIdx = 0;
        for (Iterator cells = row.iterator(); cells.hasNext();) {
            HSSFCell hssfCell = hssfRow.createCell(cellIdx++);
            Object o = cells.next();
            if ("class java.lang.Double".equals(o.getClass().toString())) {
                hssfCell.setCellValue((Double) o);
            } else {
                hssfCell.setCellValue((String) o);
            }

        }
    }

    wb.setSheetName(0, sheetName);
    try {
        FileOutputStream outs = new FileOutputStream(outputFile);
        wb.write(outs);
        outs.close();
        //            System.out.println("Archivo creado correctamente en " + outputFile.getAbsolutePath());
    } catch (IOException e) {
        e.printStackTrace();
        throw new HPSFException(e.getMessage());
    }
}

From source file:Adicionales.Abrir_xml.java

private void ExportarEtiquetasXml(HSSFWorkbook workbook, Document document, String nombre) {
    System.out.println("Exportando : " + nombre);
    try {/*from  w  w w.  ja  va 2 s  .c o  m*/
        HSSFSheet sheet = workbook.createSheet(tipo_comprobante + " " + (nombres.indexOf(nombre) + 1));
        System.out.println("Primera vez creada");
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        HSSFRow rowTag = sheet.createRow(0);
        HSSFRow RowData = sheet.createRow(1);
        NodeList nodeList = document.getElementsByTagName("*");
        for (int i = 0; i < nodeList.getLength(); i++) {
            Element element = (Element) nodeList.item(i);
            if (element.getChildNodes().getLength() == 1 && !element.getNodeName().contains(":")) {
                if (element.getFirstChild().getNodeType() == Node.TEXT_NODE
                        && !element.getNodeName().equals("comprobante")) {
                    HSSFCell cellTag = rowTag.createCell(i);
                    cellTag.setCellValue(element.getNodeName());
                    HSSFCell cellData = RowData.createCell(i);
                    cellData.setCellValue(element.getFirstChild().getNodeValue());
                    sheet.autoSizeColumn((short) i);
                    cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                    cellTag.setCellStyle(cellStyle);
                } else {
                    DocumentBuilderFactory sub_factory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder sub_builder = sub_factory.newDocumentBuilder();
                    Document sub_document = sub_builder
                            .parse(new InputSource(new StringReader(element.getFirstChild().getNodeValue())));
                    NodeList sub_nodeList = sub_document.getElementsByTagName("*");
                    for (int j = 0; j < sub_nodeList.getLength(); j++) {
                        Element sub_element = (Element) sub_nodeList.item(j);
                        if (sub_element.getNodeName().equals("Signature"))
                            break;
                        if (sub_element.getChildNodes().getLength() == 1
                                && !sub_element.getNodeName().contains(":")) {
                            if (sub_element.getFirstChild().getNodeType() == Node.TEXT_NODE) {
                                HSSFCell cellTag = rowTag.createCell(j);
                                cellTag.setCellValue(sub_element.getNodeName());
                                HSSFCell cellData = RowData.createCell(j);
                                cellData.setCellValue(sub_element.getFirstChild().getNodeValue());
                                sheet.autoSizeColumn((short) j);
                                cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
                                cellTag.setCellStyle(cellStyle);
                            }
                        }
                    }
                }
            }
        }
    } catch (IOException e) {
        System.out.println(e.getMessage());
    } catch (IllegalArgumentException e) {
        JOptionPane.showMessageDialog(null, "<html>Error al exportar archivo " + nombre
                + "<br>Verificar maximo de etiquetas soportadas [255]</html>");
    } catch (ParserConfigurationException e) {
        System.out.println("ParserConfigurationException " + e.getMessage());
    } catch (SAXException e) {
        System.out.println("SAXException " + e.getMessage());
    }
}

From source file:ambit.io.XLSFileWriter.java

License:Open Source License

public void writeMolecule(IMolecule molecule) {

    Object value;/* ww  w  .ja va  2  s.c  om*/

    try {
        //give it a chance to create a header just before the first write
        if (!writingStarted) {
            if (header == null)
                setHeader(molecule.getProperties());
            writeHeader();
            writingStarted = true;
        }
        HSSFRow row = sheet.createRow((short) (sheet.getLastRowNum() + 1));
        String s;
        for (int i = 0; i < header.size(); i++) {
            value = molecule.getProperty(header.list.get(i));
            if (i == smilesIndex) {

                if (value == null) //no SMILES available
                    try {
                        value = sg.createSMILES(molecule);
                    } catch (Exception x) {
                        logger.error("Error while createSMILES\t", x.getMessage());
                        value = "";
                    }
            }

            if (value != null) {
                HSSFCell cell = row.createCell((short) (i + 1));

                if (value instanceof Number) {
                    cell.setCellStyle(style);
                    cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                    cell.setCellValue(((Number) value).doubleValue());
                } else {
                    try {
                        double d = Double.parseDouble(value.toString());
                        cell.setCellStyle(style);
                        cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                        cell.setCellValue(d);
                    } catch (Exception x) {
                        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                        cell.setCellValue(value.toString());
                    }
                }

            }
        }
    } catch (Exception x) {
        logger.error("ERROR while writing Molecule: ", x.getMessage());
        logger.debug(x);
        x.printStackTrace();
    }
}

From source file:angiotool.SaveToExcel.java

License:Open Source License

private HSSFCell createCell(int column, Object obj, HSSFCellStyle cellStyle) {
    if (column < 0)
        column = 0;//from w  w  w . j a v  a 2 s  .  c o  m
    HSSFCell cell = r.createCell(column);
    if (obj instanceof String)
        cell.setCellValue(new HSSFRichTextString((String) obj));
    else if (obj instanceof Double)
        cell.setCellValue((Double) obj);
    else if (obj instanceof Integer)
        cell.setCellValue((Integer) obj);
    else if (obj instanceof Long)
        cell.setCellValue((Long) obj);
    cell.setCellStyle(cellStyle);

    return cell;
}

From source file:attandance.standalone.manager.AttandanceManager.java

private void outputAttandance(List<LateRecord> lates, List<AbsenceRecord> absences) {
    // webbookExcel  
    HSSFWorkbook wb = new HSSFWorkbook();
    // webbooksheet,Excelsheet  
    HSSFSheet sheet = wb.createSheet("");
    int width = ((int) (20 * 1.14388)) * 256;
    sheet.setColumnWidth(0, width);//from   w w  w  .  ja va 2s  .  c o  m
    sheet.setColumnWidth(1, width);
    sheet.setColumnWidth(2, width);
    sheet.setColumnWidth(3, width);
    // sheet0,??poiExcel?short  
    HSSFRow row = sheet.createRow((int) 0);
    // ?   
    HSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ?  

    HSSFCell cell = row.createCell((short) 0);
    cell.setCellValue("??");
    cell.setCellStyle(style);
    cell = row.createCell((short) 1);
    cell.setCellValue("");
    cell.setCellStyle(style);
    cell = row.createCell((short) 2);
    cell.setCellValue("?");
    cell.setCellStyle(style);
    cell = row.createCell((short) 3);
    cell.setCellValue("?");
    cell.setCellStyle(style);

    // ? ??   
    int i = 0;
    for (; i < lates.size(); i++) {
        row = sheet.createRow((int) i + 1);
        LateRecord lateStaff = lates.get(i);
        // ?  
        row.createCell((short) 0).setCellValue(lateStaff.getStaffName());
        row.createCell((short) 1).setCellValue(lateStaff.getCaculateDateString());
        cell = row.createCell((short) 2);
        cell.setCellValue(new SimpleDateFormat(DateHelper.DATE_FORMAT).format(lateStaff.getLateDate()));
        row.createCell((short) 3).setCellValue(lateStaff.getLateTimeDesc());
    }
    for (int j = 0; j < absences.size(); j++) {
        row = sheet.createRow((int) i + j + 1);
        AbsenceRecord absenceStaff = absences.get(j);
        // ?  
        row.createCell((short) 0).setCellValue(absenceStaff.getStaffName());
        row.createCell((short) 1).setCellValue(absenceStaff.getCaculateDateString());
        cell = row.createCell((short) 2);
        cell.setCellValue(
                new SimpleDateFormat(DateHelper.ONLY_DATE_FORMAT).format(absenceStaff.getAbsenceDate()));
        row.createCell((short) 3).setCellValue("?");
    }
    // ?  
    try {
        String fileName = "C:/xhuxing-private/" + new Date(System.currentTimeMillis()).getMonth()
                + ".xls";
        FileOutputStream fout = new FileOutputStream(fileName);
        wb.write(fout);
        fout.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:BaseDatos.ExportarDatos.java

public void CrearExel() throws IOException {

    FileOutputStream archivoSalida = null;

    try {/*from  w w w . j  a v a 2 s . c  o  m*/
        Statement conexion = ConectarMysql.obtenerConexion().createStatement(ResultSet.CONCUR_READ_ONLY,
                ResultSet.TYPE_FORWARD_ONLY);
        HSSFWorkbook libro = new HSSFWorkbook();
        HSSFSheet hoja = libro.createSheet("reporte");
        String SqlVentas = "select * from ventas_realizadas";
        ResultSet resultado = conexion.executeQuery(SqlVentas);

        HSSFRow fila = hoja.createRow((short) 0);
        HSSFCell celda = fila.createCell(0);
        celda.setCellValue("nombre del producto");
        celda = fila.createCell(1);
        celda.setCellValue("fabricante");
        celda = fila.createCell(2);
        celda.setCellValue("contenedor");
        celda = fila.createCell(3);
        celda.setCellValue("cantidad");
        celda = fila.createCell(4);
        celda.setCellValue("precio Compra");
        celda = fila.createCell(5);
        celda.setCellValue("precio Venta");
        celda = fila.createCell(6);
        celda.setCellValue("fecha");
        if (resultado.first()) {
            int contador = 1;

            do {
                filaExel.clear();
                fila = hoja.createRow((short) contador);

                obtenerDatos(resultado.getInt(2));
                cantidad = resultado.getString(3);
                precioCompra = resultado.getString(4);
                precioVenta = resultado.getString(5);
                fecha = resultado.getString(6);
                filaExel.add(nombreProducto);
                filaExel.add(fabricante);
                filaExel.add(contenedor);
                filaExel.add(cantidad);
                filaExel.add(precioCompra);
                filaExel.add(precioVenta);
                filaExel.add(fecha);
                for (int a = 0; a < filaExel.size(); a++) {
                    celda = fila.createCell(a);
                    if (a == 3) {
                        celda.setCellValue(Integer.parseInt(filaExel.get(a)));
                    } else if (a == 4 || a == 5) {
                        celda.setCellValue(Double.parseDouble(filaExel.get(a)));
                    } else {
                        celda.setCellValue(filaExel.get(a));
                    }
                }
                this.operacion.setText("operando : " + contador);
                contador++;
            } while (resultado.next());

        }
        String direccionCompleta = "C:\\Users\\zombozo\\Documents\\ventas.xls";
        File archivo = new File(direccionCompleta);
        archivoSalida = new FileOutputStream(archivo);
        libro.write(archivoSalida);
        archivoSalida.close();
        conexion.close();
        JOptionPane.showMessageDialog(rootPane,
                "Creado Correctamente, busque el archivo en la carpeta documentos");
    } catch (Exception e) {
        JOptionPane.showMessageDialog(rootPane, "Ocurrio un error 1: " + e);
    }
}

From source file:BaseDatos.ExportarDatos.java

private void CrearExelCompras() {
    FileOutputStream archivoSalida = null;

    try {/*from  w  ww.j ava  2  s . c o m*/
        Statement conexion = ConectarMysql.obtenerConexion().createStatement(ResultSet.CONCUR_READ_ONLY,
                ResultSet.TYPE_FORWARD_ONLY);
        HSSFWorkbook libro = new HSSFWorkbook();
        HSSFSheet hoja = libro.createSheet("reporte");
        String SqlVentas = "select * from logCompras";
        ResultSet resultado = conexion.executeQuery(SqlVentas);

        HSSFRow fila = hoja.createRow((short) 0);
        HSSFCell celda = fila.createCell(0);
        celda.setCellValue("nombre del producto");
        celda = fila.createCell(1);
        celda.setCellValue("fabricante");
        celda = fila.createCell(2);
        celda.setCellValue("contenedor");
        celda = fila.createCell(3);
        celda.setCellValue("cantidad");
        celda = fila.createCell(4);
        celda.setCellValue("precio Compra");
        celda = fila.createCell(5);
        celda.setCellValue("precio Venta");
        celda = fila.createCell(6);
        celda.setCellValue("fecha");
        if (resultado.first()) {
            int contador = 1;

            do {
                filaExel.clear();
                fila = hoja.createRow((short) contador);

                obtenerDatos(resultado.getInt(2));
                cantidad = resultado.getString(3);
                precioCompra = resultado.getString(4);
                precioVenta = resultado.getString(5);
                fecha = resultado.getString(6);
                filaExel.add(nombreProducto);
                filaExel.add(fabricante);
                filaExel.add(contenedor);
                filaExel.add(cantidad);
                filaExel.add(precioCompra);
                filaExel.add(precioVenta);
                filaExel.add(fecha);
                for (int a = 0; a < filaExel.size(); a++) {
                    celda = fila.createCell(a);
                    if (a == 3) {
                        celda.setCellValue(Integer.parseInt(filaExel.get(a)));
                    } else if (a == 4 || a == 5) {
                        celda.setCellValue(Double.parseDouble(filaExel.get(a)));
                    } else {
                        celda.setCellValue(filaExel.get(a));
                    }
                }
                this.operacion.setText("operando : " + contador);
                contador++;
            } while (resultado.next());

        }
        String direccionCompleta = "C:\\Users\\zombozo\\Documents\\compras.xls";
        File archivo = new File(direccionCompleta);
        archivoSalida = new FileOutputStream(archivo);
        libro.write(archivoSalida);
        archivoSalida.close();
        JOptionPane.showMessageDialog(rootPane,
                "Creado Correctamente, busque el archivo en la carpeta documentos");
        conexion.close();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(rootPane, "Ocurrio un error 1: " + e);
    }
}

From source file:bd.gov.forms.web.FormBuilder.java

License:Open Source License

@RequestMapping(value = "/excelExport", method = RequestMethod.GET)
public String excelExport(@RequestParam(value = "formId", required = true) String formId,
        @RequestParam(value = "page", required = false) Integer page,
        @RequestParam(value = "colName", required = false) String colName,
        @RequestParam(value = "colVal", required = false) String colVal,
        @RequestParam(value = "sortCol", required = false) String sortCol,
        @RequestParam(value = "sortDir", required = false) String sortDir, ModelMap model,
        HttpServletResponse response, HttpServletRequest request) throws IOException {

    String access = UserAccessChecker.check(request);
    if (access != null) {
        return access;
    }/*from  ww  w  .  j  av a2s . co m*/

    if (page == null) {
        page = 1;
    }

    Form form = formDao.getFormWithFields(formId);

    List<HashMap> list = formDao.getEntryList(form, page, colName, colVal, sortCol, sortDir, false);
    List<String> headers = getEntryListHeaders(form);

    response.setContentType("application/vnd.ms-excel");
    // TODO: file name

    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet("Excel Report");

    int r = 0;
    HSSFRow row = sheet.createRow((short) r++);
    int count = 0;
    for (String header : headers) {
        HSSFCell cell = row.createCell(count++);
        cell.setCellValue(header);
    }

    for (HashMap hashmap : list) {
        row = sheet.createRow((short) r++);
        count = 0;

        HSSFCell cell = row.createCell(count++);
        cell.setCellValue((String) hashmap.get("entry_date"));

        cell = row.createCell(count++);
        cell.setCellValue((String) hashmap.get("entry_time"));

        cell = row.createCell(count++);
        cell.setCellValue((String) hashmap.get("entry_status"));

        for (Field field : form.getFields()) {
            cell = row.createCell(count++);
            cell.setCellValue((String) hashmap.get(field.getColName()));
        }
    }

    String fileName = "Report-" + formId + ".xls";
    response.setHeader("Content-Disposition", "inline; filename=" + fileName);
    response.setContentType("application/vnd.ms-excel");

    ServletOutputStream outputStream = response.getOutputStream();
    sheet.getWorkbook().write(outputStream);
    outputStream.flush();

    return null;
}