Example usage for org.apache.poi.ss.usermodel Row createCell

List of usage examples for org.apache.poi.ss.usermodel Row createCell

Introduction

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

Prototype

Cell createCell(int column);

Source Link

Document

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

Usage

From source file:com.github.luischavez.lat.excel.Excel.java

License:Open Source License

protected void createFormula(int index, Row row, CellStyle style, String formula) {
    Cell cell = row.createCell(index);
    if (null != style) {
        cell.setCellStyle(style);/*from   w  ww  .j a v  a 2s .c o m*/
    }
    cell.setCellType(Cell.CELL_TYPE_FORMULA);
    cell.setCellFormula(formula);
}

From source file:com.github.mutationmapper.MutationMapperResultViewController.java

License:Open Source License

private void writeResultsToExcel(final File f) throws IOException {
    final Service<Void> service = new Service<Void>() {
        @Override//  w ww.  j  a  v  a 2s  . c  o m
        protected Task<Void> createTask() {
            return new Task<Void>() {
                @Override
                protected Void call() throws IOException {
                    BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream(f));
                    Workbook wb = new XSSFWorkbook();
                    //CellStyle hlink_style = wb.createCellStyle();
                    //Font hlink_font = wb.createFont();
                    //hlink_font.setUnderline(Font.U_SINGLE);
                    //hlink_font.setColor(IndexedColors.BLUE.getIndex());
                    //hlink_style.setFont(hlink_font);
                    //CreationHelper createHelper = wb.getCreationHelper();
                    Sheet sheet = wb.createSheet();
                    Row row = null;
                    int rowNo = 0;
                    row = sheet.createRow(rowNo++);
                    String header[] = { "#", "Symbol", "Gene", "Transcript", "Genomic Coordinate", "Ref", "Var",
                            "CDS", "Consequence", "CDS Consequence", "Protein Consequence", "Exon/Intron",
                            "Colocated Variants", "Polyphen", "Sift", "Seq Input" };
                    for (int col = 0; col < header.length; col++) {
                        Cell cell = row.createCell(col);
                        cell.setCellValue(header[col]);
                    }

                    updateMessage("Writing results . . .");
                    updateProgress(0, displayData.size());
                    int n = 0;
                    for (MutationMapperResult r : displayData) {
                        n++;
                        updateMessage("Writing result " + n + " . . .");
                        row = sheet.createRow(rowNo++);
                        int col = 0;
                        ArrayList<String> resultsToWrite = getResultArray(r);
                        for (String s : resultsToWrite) {
                            Cell cell = row.createCell(col++);
                            cell.setCellValue(s);
                        }
                        updateProgress(n, displayData.size());
                    }

                    updateMessage("Wrote " + displayData.size() + " results to file.");
                    wb.write(bo);
                    bo.close();
                    return null;
                }
            };
        }

    };

    service.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent e) {
            Alert alert = new Alert(AlertType.CONFIRMATION);
            alert.setTitle("File Saved");
            alert.setHeaderText("Displayed results saved to " + f.getName());
            alert.setContentText("Open this file now?");
            ButtonType yesButton = ButtonType.YES;
            ButtonType noButton = ButtonType.NO;
            alert.getButtonTypes().setAll(yesButton, noButton);
            Optional<ButtonType> response = alert.showAndWait();
            if (response.get() == yesButton) {
                try {
                    openFile(f);
                } catch (Exception ex) {
                    Alert openError = getExceptionDialog(ex);
                    openError.setTitle("Open Failed");
                    openError.setHeaderText("Could not open ouput file.");
                    openError.setContentText(
                            "Exception encountered when attempting to open " + "the saved file. See below:");
                    openError.showAndWait();
                }
            }
        }
    });
    service.setOnCancelled(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent e) {
            Alert writeCancelled = new Alert(AlertType.INFORMATION);
            writeCancelled.setTitle("Cancelled writing to file");
            writeCancelled.setHeaderText("User cancelled writing primers to file.");
            writeCancelled.showAndWait();
        }
    });
    service.setOnFailed(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent e) {
            Alert writeFailed = getExceptionDialog(e.getSource().getException());
            writeFailed.setTitle("Write Failed");
            writeFailed.setHeaderText("Could not write ouput file.");
            writeFailed.setContentText(
                    "Exception encountered when attempting to write " + "results to file. See below:");
            writeFailed.showAndWait();

        }
    });
    service.start();
}

From source file:com.github.svrtm.xlreport.Cell.java

License:Apache License

Cell(final TR row, final int i, final CellOperation cellOperation) {
    super(row);//from   w w w  .  j  a  v  a 2  s .  c o m
    creationHelper = builder.sheet.getWorkbook().getCreationHelper();

    final org.apache.poi.ss.usermodel.Row poiRow = row.poiRow;
    if (CREATE == cellOperation)
        poiCell = poiRow.createCell(i);
    else
        poiCell = CREATE_and_GET == cellOperation && poiRow.getCell(i) == null ? poiRow.createCell(i)
                : poiRow.getCell(i);
}

From source file:com.github.ukase.toolkit.xlsx.CellMerge.java

License:Open Source License

void fillRow(Row row) {
    int rowNumber;
    while ((rowNumber = row.getPhysicalNumberOfCells()) <= cellEnd) {
        row.createCell(rowNumber).setCellStyle(style);
    }/*  ww  w  .  ja v  a 2  s. co m*/
}

From source file:com.github.ukase.toolkit.xlsx.RenderingTable.java

License:Open Source License

private void processCell(Row row, Element td) {
    TableCellBox cellBox = (TableCellBox) (box.getElementBoxes(td)).get(0);
    CellStyle style = prepareCellStyle(cellBox.getStyle());

    mergedCells.stream().filter(merge -> merge.isApplicable(row)).forEach(merge -> merge.fillRow(row));

    int cellNumber = row.getPhysicalNumberOfCells();
    Cell cell = row.createCell(cellNumber);
    cell.setCellValue(td.getTextContent());
    cell.setCellStyle(style);//from w  ww .  j  a va2s .c  o m

    mergeCells(row, td, cellNumber, style);
    calculateColumnWidth(cellNumber, cellBox.getStyle());
}

From source file:com.github.wnameless.workbookaccessor.WorkbookWriter.java

License:Apache License

/**
 * Adds a row to the sheet.//from  w ww .  j a va 2 s. co  m
 * 
 * @param fields
 *          an Iterable of Object
 * @return this {@link WorkbookWriter}
 */
public WorkbookWriter addRow(Iterable<? extends Object> fields) {
    Row row;
    if (sheet.getLastRowNum() == 0 && sheet.getPhysicalNumberOfRows() == 0)
        row = sheet.createRow(0);
    else
        row = sheet.createRow(sheet.getLastRowNum() + 1);

    int i = 0;
    for (Object o : fields) {
        Cell cell = row.createCell(i);
        if (o != null) {
            if (o instanceof Boolean)
                cell.setCellValue((Boolean) o);
            else if (o instanceof Calendar)
                cell.setCellValue((Calendar) o);
            else if (o instanceof Date)
                cell.setCellValue((Date) o);
            else if (o instanceof Double)
                cell.setCellValue((Double) o);
            else if (o instanceof RichTextString)
                if ((o instanceof HSSFRichTextString && workbook instanceof HSSFWorkbook)
                        || (o instanceof XSSFRichTextString && workbook instanceof XSSFWorkbook)) {
                    cell.setCellValue((RichTextString) o);
                } else {
                    cell.setCellValue(o.toString());
                }
            else if (o instanceof Hyperlink)
                cell.setHyperlink((Hyperlink) o);
            else if (o instanceof Number)
                cell.setCellValue(((Number) o).doubleValue());
            else
                cell.setCellValue(o.toString());
        }
        i++;
    }
    return this;
}

From source file:com.globalsight.everest.qachecks.DITAQAChecker.java

License:Apache License

private Cell getCell(Row p_row, int index) {
    Cell cell = p_row.getCell(index);/*w  w w. jav  a 2  s.  c  o m*/
    if (cell == null)
        cell = p_row.createCell(index);
    return cell;
}

From source file:com.globalsight.everest.qachecks.QAChecker.java

License:Apache License

private Cell getCell(Row p_row, int columnIndex) {
    Cell cell = p_row.getCell(columnIndex);
    if (cell == null) {
        cell = p_row.createCell(columnIndex);
    }/*from   ww  w .j  a  v  a 2s .c o  m*/
    return cell;
}

From source file:com.grant.report.StockOut.java

public static void main(String[] args) throws SQLException {
    ItemDAO d = new ItemDAO();
    Map<String, Object[]> data = new TreeMap<String, Object[]>();
    //  data =  d.getAllItemOutReport();

    //Blank workbook
    XSSFWorkbook workbook = new XSSFWorkbook();

    //Create a blank sheet
    XSSFSheet sheet = workbook.createSheet("Employee Data");

    //This data needs to be written (Object[])
    /*/*  w  ww  .j  ava  2  s .c  om*/
    Map<String, Object[]> data = new TreeMap<String, Object[]>();
    data.put("1", new Object[] {"ID", "NAME", "LASTNAME"});
    data.put("2", new Object[] {1, "Amit", "Shukla"});
    data.put("3", new Object[] {2, "Lokesh", "Gupta"});
    data.put("4", new Object[] {3, "John", "Adwards"});
    data.put("5", new Object[] {4, "Brian", "Schultz"});
            
     */
    //Iterate over data and write to sheet
    Set<String> keyset = data.keySet();
    int rownum = 0;
    for (String key : keyset) {
        Row row = sheet.createRow(rownum++);
        Object[] objArr = data.get(key);
        int cellnum = 0;
        for (Object obj : objArr) {
            Cell cell = row.createCell(cellnum++);
            if (obj instanceof String) {
                cell.setCellValue((String) obj);
            } else if (obj instanceof Integer) {
                cell.setCellValue((Integer) obj);
            }
        }
    }
    try {
        //Write the workbook in file system
        FileOutputStream out = new FileOutputStream(new File("howtodoinjava_demo.xlsx"));
        workbook.write(out);
        out.close();
        System.out.println("howtodoinjava_demo.xlsx written successfully on disk.");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.griffinslogistics.document.excel.BDLGenerator.java

private static void insertDate(Sheet sheet, CellStyle style) {
    Row dateRow = sheet.createRow(6);

    sheet.addMergedRegion(CellRangeAddress.valueOf("$B$7:$C$7"));

    Locale frenchLocale = new Locale("fr", "FR");
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMMM yyyy", frenchLocale);

    String dateString = dateFormat.format(new Date());
    Cell dateCell = dateRow.createCell(1);
    dateCell.setCellStyle(style);/*www  .j  a va  2s.  c  o m*/
    dateCell.setCellValue(dateString);
}