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:TestUtil.java

License:BSD License

private void writeCSI(Node csiNode, HSSFWorkbook wb) {
    HSSFSheet sheet = wb.getSheet("tbl_CS_ITEMS");
    HSSFRow row = sheet.getRow(10);
    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;/* ww  w  .  ja v  a 2s. c  om*/

    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//  w  w w.  ja  va2  s. c o  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 ww w  . j ava 2  s  . com
        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

protected void writeHeader() throws IOException {
    HSSFRow row = sheet.createRow((short) 0);
    for (int i = 0; i < header.size(); i++) {
        row.createCell((short) (i + 1)).setCellValue(header.list.get(i).toString());
    }/* w  w w.  j  ava2  s .c  om*/

    logger.debug("\tHeader written\t", header);
}

From source file:ambit.io.XLSFileWriter.java

License:Open Source License

public void writeMolecule(IMolecule molecule) {

    Object value;/*from ww  w  .  j a va2s.c o m*/

    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:at.htlpinkafeld.beans.AlleAbwesenheitenBean.java

/**
 * xls post processing// www .  ja  va 2 s  . c o  m
 *
 * @param document xls document
 */
public void postProcessXLS(Object document) {
    HSSFWorkbook wb = (HSSFWorkbook) document;
    HSSFSheet sheet = wb.getSheetAt(0);

    HSSFRow header = sheet.getRow(0);

    HSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
    cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

    for (int i = 0; i < header.getPhysicalNumberOfCells(); i++) {
        HSSFCell cell = header.getCell(i);
        cell.setCellStyle(cellStyle);

        sheet.autoSizeColumn(i);
    }

    HSSFRow bottomRow = sheet.createRow(sheet.getLastRowNum() + 2);
    bottomRow.createCell(0)
            .setCellValue("Stand: " + LocalDate.now().format(DateTimeFormatter.ofPattern("dd.MM.yyyy")));

}

From source file:at.htlpinkafeld.beans.JahresuebersichtBean.java

/**
 * post processes the XLS for creating//from w ww  . jav  a2 s . co m
 *
 * @param document xls-doc
 */
public void postProcessXLS(Object document) {
    HSSFWorkbook wb = (HSSFWorkbook) document;
    HSSFSheet sheet = wb.getSheetAt(0);

    sheet.shiftRows(0, sheet.getLastRowNum(), 2);

    HSSFRow topRow = sheet.createRow(0);

    topRow.createCell(0).setCellValue("Jahresbersicht - " + selectedYear.getYear());
    topRow.createCell(3).setCellValue("von " + selectedUser.getPersName());
    sheet.createRow(1).createCell(0).setCellValue(" ");

    sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));

    HSSFRow header = sheet.getRow(2);
    HSSFRow footer = sheet.getRow(sheet.getLastRowNum());

    HSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
    cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

    for (int i = 0; i < header.getPhysicalNumberOfCells(); i++) {
        HSSFCell cell = header.getCell(i);
        cell.setCellStyle(cellStyle);

        cell = footer.getCell(i);
        cell.setCellStyle(cellStyle);

        sheet.autoSizeColumn(i);
    }

    HSSFRow bottomRow = sheet.createRow(sheet.getLastRowNum() + 2);
    bottomRow.createCell(0)
            .setCellValue("Stand: " + LocalDate.now().format(DateTimeFormatter.ofPattern("dd.MM.yyyy")));

}

From source file:at.htlpinkafeld.beans.UserDetailsBean.java

public void postProcessXLS(Object document) {
        HSSFWorkbook wb = (HSSFWorkbook) document;
        HSSFSheet sheet = wb.getSheetAt(0);

        sheet.shiftRows(0, sheet.getLastRowNum(), 2);

        HSSFRow topRow = sheet.createRow(0);

        topRow.createCell(0)
                .setCellValue("Monatsbersicht - " + selectedDate.format(DateTimeFormatter.ofPattern("MM.yyyy")));
        topRow.createCell(7).setCellValue("von " + selectedUser);
        sheet.createRow(1).createCell(0).setCellValue(" ");

        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));

        HSSFRow header = sheet.getRow(2);
        HSSFRow footer = sheet.getRow(sheet.getLastRowNum());

        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

        for (int i = 0; i < header.getPhysicalNumberOfCells(); i++) {
            HSSFCell cell = header.getCell(i);
            cell.setCellStyle(cellStyle);

            cell = footer.getCell(i);/*  w  w  w .ja va 2  s. co  m*/
            cell.setCellStyle(cellStyle);

            sheet.autoSizeColumn(i);
        }

        HSSFRow bottomRow = sheet.createRow(sheet.getLastRowNum() + 2);
        bottomRow.createCell(0)
                .setCellValue("Stand: " + LocalDate.now().format(DateTimeFormatter.ofPattern("dd.MM.yyyy")));

    }

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);// w  w w  .  ja  v a  2  s.c  om
    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();
    }
}