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.mechatronika.trackmchtr.ExportToExcel.java

private static void writeToExcel(String path) throws FileNotFoundException, IOException {
    new WorkbookFactory();
    Workbook wb = new XSSFWorkbook(); //Excell workbook
    Sheet sheet = wb.createSheet(); //WorkSheet //wb.createSheet("sheetName");
    Row row = sheet.createRow(2); //Row created at line 3
    TableModel model = table.getModel(); //Table model

    Row headerRow = sheet.createRow(0); //Create row at line 0
    for (int headings = 0; headings < model.getColumnCount(); headings++) { //For each column
        headerRow.createCell(headings).setCellValue(model.getColumnName(headings));//Write column name
    }/* w w  w .j  a v  a 2s.c o  m*/

    for (int rows = 0; rows < model.getRowCount(); rows++) { //For each table row
        for (int cols = 0; cols < table.getColumnCount(); cols++) { //For each table column
            row.createCell(cols).setCellValue(model.getValueAt(rows, cols).toString()); //Write value
        }

        //Set the row to the next one in the sequence 
        row = sheet.createRow((rows + 3));
    }
    wb.write(new FileOutputStream(path));//Save the file  
    IJ.showMessage("Excel file created!");
}

From source file:com.mimp.controllers.reporte.java

@RequestMapping("/Reportes/OrganismosAcreditados")
    public void ReporteOrganismo(ModelMap map, HttpSession session, HttpServletResponse response) {
        Personal usuario = (Personal) session.getAttribute("usuario");
        Workbook wb = new XSSFWorkbook();
        try {//from   w w w.j a va2s  .c o  m
            //Se llama a la plantilla localizada en la ruta

            //            InputStream inp = new FileInputStream("C:\\Plantillas\\OrgAcred.xlsx");
            InputStream inp = new FileInputStream("/opt/Plantillas/OrgAcred.xlsx");

            wb = WorkbookFactory.create(inp);
            Sheet sheet = wb.getSheetAt(0);

            //Aqu va el query que consigue los datos de la tabla
            //ArrayList<Organismo> listaorg = ServicioPersonal.ListaOrganismos();
            ArrayList<Organismo> listaorg = ServicioReporte.ReporteOrganismo2();

            int i = 1;
            for (Organismo org : listaorg) {
                Row row = sheet.createRow(i);

                Cell cell = row.createCell(0);
                cell.setCellValue(i);
                cell = row.createCell(1);
                cell.setCellValue(org.getEntidad().getNombre());
                cell = row.createCell(2);
                cell.setCellValue(org.getCompetencia());
                cell = row.createCell(3);
                cell.setCellValue(org.getEntidad().getResolAuto());
                cell = row.createCell(4);
                String fechaVenc = "";
                try {
                    fechaVenc = format.dateToString(org.getEntidad().getFechaVenc());
                } catch (Exception ex) {
                }
                cell.setCellValue(fechaVenc);
                cell = row.createCell(5);
                for (Iterator iter = org.getRepresentantes().iterator(); iter.hasNext();) {
                    Representante rep = (Representante) iter.next();
                    cell.setCellValue(rep.getNombre() + " " + rep.getApellidoP());
                }
                cell = row.createCell(6);
                cell.setCellValue(org.getEntidad().getObs());

                i++;
            }
        } catch (Exception e) {
            //e.printStackTrace();
        }

        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition",
                    "attachment; filename=Registro del nmero Organismos Acreditados.xlsx");
            OutputStream fileOut = response.getOutputStream();
            wb.write(fileOut);
            fileOut.flush();
            fileOut.close();
        } catch (Exception ex) {
            //ex.printStackTrace();
        }

        String mensaje_log = "El usuario: " + usuario.getNombre() + " " + usuario.getApellidoP() + " con ID: "
                + usuario.getIdpersonal() + ". Descarg el Reporte 'Organismos Acreditados' ";

        String Tipo_registro = "Personal";

        try {
            String Numero_registro = String.valueOf(usuario.getIdpersonal());

            ServicioPersonal.InsertLog(usuario, Tipo_registro, Numero_registro, mensaje_log);
        } catch (Exception ex) {
        }
    }

From source file:com.mimp.controllers.reporte.java

private static void copyRowStyle(Sheet worksheet, int sourceRowNum, int destinationRowNum) {
        // Coge la fila antigua y nueva
        Row newRow = worksheet.getRow(destinationRowNum);
        Row sourceRow = worksheet.getRow(sourceRowNum);

        //Si existe una fila en el detino, pasa todas las filas 1 ms abajo antes de crear la nueva columna
        if (newRow != null) {
            worksheet.shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1);
        } else {/*www  .j  a va2s.  c om*/
            newRow = worksheet.createRow(destinationRowNum);
        }

        // Hace un loop entre las celdas de cada columna para aadir una por una a la nueva
        for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
            // Copia la antigua y nueva celda
            Cell oldCell = sourceRow.getCell(i);
            Cell newCell = newRow.createCell(i);

            // Si la anterior celda es null, evalua la siguiente celda defrente
            if (oldCell == null) {
                newCell = null;
                continue;
            }

            // Usa el estilo de la celda antigua
            newCell.setCellStyle(oldCell.getCellStyle());

            // Establece el tipo de valor de la celda
            newCell.setCellType(oldCell.getCellType());

            // Establece el valor de la celda
            //            switch (oldCell.getCellType()) {
            //                case Cell.CELL_TYPE_BLANK:
            //                    break;
            //                case Cell.CELL_TYPE_BOOLEAN:
            //                    newCell.setCellValue(oldCell.getBooleanCellValue());
            //                    break;
            //                case Cell.CELL_TYPE_ERROR:
            //                    newCell.setCellErrorValue(oldCell.getErrorCellValue());
            //                    break;
            //                case Cell.CELL_TYPE_FORMULA:
            //                    newCell.setCellFormula(oldCell.getCellFormula());
            //                    break;
            //                case Cell.CELL_TYPE_NUMERIC:
            //                    newCell.setCellValue(oldCell.getNumericCellValue());
            //                    break;
            //                case Cell.CELL_TYPE_STRING:
            //                    newCell.setCellValue(oldCell.getRichStringCellValue());
            //                    break;
            //            }
        }
    }

From source file:com.mimp.controllers.reporte.java

private static void copyRow(Sheet worksheet, int sourceRowNum, int destinationRowNum) {
        // Coge la fila antigua y nueva
        Row newRow = worksheet.getRow(destinationRowNum);
        Row sourceRow = worksheet.getRow(sourceRowNum);

        //Si existe una fila en el detino, pasa todas las filas 1 ms abajo antes de crear la nueva columna
        if (newRow != null) {
            worksheet.shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1);
        } else {/*from w  w  w .  j a v a  2  s. co m*/
            newRow = worksheet.createRow(destinationRowNum);
        }

        // Hace un loop entre las celdas de cada columna para aadir una por una a la nueva
        for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
            // Copia la antigua y nueva celda
            Cell oldCell = sourceRow.getCell(i);
            Cell newCell = newRow.createCell(i);

            // Si la anterior celda es null, evalua la siguiente celda defrente
            if (oldCell == null) {
                newCell = null;
                continue;
            }

            // Usa el estilo de la celda antigua
            newCell.setCellStyle(oldCell.getCellStyle());

            // Establece el tipo de valor de la celda
            newCell.setCellType(oldCell.getCellType());

            // Establece el valor de la celda
            switch (oldCell.getCellType()) {
            case Cell.CELL_TYPE_BLANK:
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                newCell.setCellValue(oldCell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_ERROR:
                newCell.setCellErrorValue(oldCell.getErrorCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                newCell.setCellFormula(oldCell.getCellFormula());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                newCell.setCellValue(oldCell.getNumericCellValue());
                break;
            case Cell.CELL_TYPE_STRING:
                newCell.setCellValue(oldCell.getRichStringCellValue());
                break;
            }
        }
    }

From source file:com.miraisolutions.xlconnect.Workbook.java

License:Open Source License

private Cell getCell(Sheet sheet, int rowIndex, int colIndex, boolean create) {
    // Get or create row
    Row row = sheet.getRow(rowIndex);
    if (row == null) {
        if (create) {
            row = sheet.createRow(rowIndex);
        } else/*from  w w  w. j  a  v a 2s  . c o  m*/
            return null;
    }
    // Get or create cell
    Cell cell = row.getCell(colIndex);
    if (cell == null) {
        if (create) {
            cell = row.createCell(colIndex);
        } else
            return null;
    }

    return cell;
}

From source file:com.movielabs.availslib.AvailXML.java

License:Open Source License

protected void initMovieSheet() {
    if (movieSheetInitialized)
        return;/* w  ww . j  a v  a2  s  . c om*/
    movieSheet = workbook.createSheet("Movie");
    currentMovieRow = 0;
    for (String[] sa : movieRows) {
        int j = 0;
        Row row = movieSheet.createRow(currentMovieRow++);
        for (String s : sa) {
            Cell cell = row.createCell(j++);
            cell.setCellValue(s);
        }
    }
    movieSheetInitialized = true;
}

From source file:com.movielabs.availslib.AvailXML.java

License:Open Source License

protected void initEpisodeSheet() {
    if (episodeSheetInitialized)
        return;/* ww  w.  j a v  a2 s . c om*/
    episodeSheet = workbook.createSheet("Episode");
    currentEpisodeRow = 0;
    for (String[] sa : episodeRows) {
        int j = 0;
        Row row = episodeSheet.createRow(currentEpisodeRow++);
        for (String s : sa) {
            Cell cell = row.createCell(j++);
            cell.setCellValue(s);
        }
    }
    episodeSheetInitialized = true;
}

From source file:com.movielabs.availslib.AvailXML.java

License:Open Source License

protected int createAtoE(int j, Row row, AvailType avail, AvailAssetType asset, AvailTransType trans) {
    Cell cell;//from  w  ww  .j a v a 2 s.co  m

    // Display Name
    cell = row.createCell(j++);
    cell.setCellValue(avail.getLicensor().getDisplayName());

    // Store Language
    List<String> storeLang = trans.getStoreLanguage();
    if (storeLang.size() != 1)
        logger.warn("more than one store language");
    cell = row.createCell(j++);
    cell.setCellValue(storeLang.get(0));

    // Territory
    List<RegionType> territory = trans.getTerritory();
    if (territory.size() != 1)
        logger.warn("more than one territory");
    cell = row.createCell(j++);
    cell.setCellValue(territory.get(0).getCountry());

    // WorkType
    cell = row.createCell(j++);
    cell.setCellValue(asset.getWorkType());

    // EntryType
    cell = row.createCell(j++);
    cell.setCellValue(avail.getDisposition().getEntryType());

    return j;
}

From source file:com.movielabs.availslib.AvailXML.java

License:Open Source License

protected int createFtoH(int j, Row row, AvailSeriesMetadataType seriesMetadata,
        AvailSeasonMetadataType seasonMetadata) {
    Cell cell;// w  w w  . j  ava2 s  .com

    // SeriesTitleInternalAlias (F)
    cell = row.createCell(j++);
    cell.setCellValue(seriesMetadata.getSeriesTitleInternalAlias());

    // SeriesTitleDisplayUnlimited (G)
    cell = row.createCell(j++);
    cell.setCellValue(seriesMetadata.getSeriesTitleDisplayUnlimited());

    // SeasonNumber (H)
    cell = row.createCell(j++);
    cell.setCellValue(seasonMetadata.getSeasonNumber().getNumber());

    return j;
}

From source file:com.movielabs.availslib.AvailXML.java

License:Open Source License

protected int createMtoR(int j, Row row, AvailSeriesMetadataType seriesMetadata,
        AvailSeasonMetadataType seasonMetadata) {
    Cell cell;//  w  ww.  j a va 2 s  .c  o  m

    // SeasonTitleInternalAlias (M)
    cell = row.createCell(j++);
    cell.setCellValue(seasonMetadata.getSeasonTitleInternalAlias());

    // SeasonTitleDisplayUnlimited (N)
    cell = row.createCell(j++);
    cell.setCellValue(seasonMetadata.getSeasonTitleDisplayUnlimited());

    // EpisodeCount (O)
    AvailSeasonMetadataType.NumberOfEpisodes nOE = seasonMetadata.getNumberOfEpisodes();
    if (nOE != null) {
        cell = row.createCell(j++);
        cell.setCellValue(nOE.getValue().intValue());
    } else {
        j++;
    }

    // SeasonCount (P)
    AvailSeriesMetadataType.NumberOfSeasons nOS = seriesMetadata.getNumberOfSeasons();
    if (nOS != null) {
        cell = row.createCell(j++);
        cell.setCellValue(nOS.getValue().intValue());
    } else {
        j++;
    }

    // SeriesAltID (Q)
    List<AvailSeriesMetadataType.SeriesAltIdentifier> seriesAltId = seriesMetadata.getSeriesAltIdentifier();
    if (seriesAltId != null && seriesAltId.size() == 1) {
        cell = row.createCell(j++);
        cell.setCellValue(seriesAltId.get(0).getIdentifier());
    } else {
        j++;
    }

    // SeasonAltID (R)
    List<AvailSeasonMetadataType.SeasonAltIdentifier> seasonAltId = seasonMetadata.getSeasonAltIdentifier();
    if (seasonAltId != null && seasonAltId.size() == 1) {
        cell = row.createCell(j++);
        cell.setCellValue(seasonAltId.get(0).getIdentifier());
    } else {
        j++;
    }

    return j;
}