Search in sources :

Example 46 with HSSFRow

use of org.apache.poi.hssf.usermodel.HSSFRow in project selenium_java by sergueik.

the class ExcelUtils method fillContent.

/**
 * 根据一个对象填充一行数据
 *
 * @param sheet
 * @param rowNum 行号
 * @param data 数据对象
 */
private void fillContent(HSSFSheet sheet, int rowNum, Object data) {
    if (sheet == null || data == null) {
        return;
    }
    HSSFRow row = sheet.createRow(rowNum);
    int column = 0;
    Class<? extends Object> dataCls = data.getClass();
    for (String key : headerMap.keySet()) {
        String name = headerMap.get(key);
        try {
            Method method = dataCls.getMethod(String.format("get%s%s", name.substring(0, 1).toUpperCase(), name.substring(1)));
            Object value = method.invoke(data);
            if (value != null) {
                HSSFCell cell = row.createCell(column);
                cell.setCellValue(value.toString());
                if (value.toString().equals(ReportStatus.EXCEPTION.name())) {
                    HSSFFont font = workbook.createFont();
                    font.setColor(HSSFFont.COLOR_RED);
                    HSSFCellStyle style = workbook.createCellStyle();
                    style.setFont(font);
                    cell.setCellStyle(style);
                }
            }
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } finally {
            column++;
        }
    }
}
Also used : HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont)

Example 47 with HSSFRow

use of org.apache.poi.hssf.usermodel.HSSFRow in project tutorials by eugenp.

the class ExcelPOIHelper method readHSSFWorkbook.

private Map<Integer, List<MyCell>> readHSSFWorkbook(FileInputStream fis) throws IOException {
    Map<Integer, List<MyCell>> data = new HashMap<>();
    HSSFWorkbook workbook = null;
    try {
        workbook = new HSSFWorkbook(fis);
        HSSFSheet sheet = workbook.getSheetAt(0);
        for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
            HSSFRow row = sheet.getRow(i);
            data.put(i, new ArrayList<>());
            if (row != null) {
                for (int j = 0; j < row.getLastCellNum(); j++) {
                    HSSFCell cell = row.getCell(j);
                    if (cell != null) {
                        HSSFCellStyle cellStyle = cell.getCellStyle();
                        MyCell myCell = new MyCell();
                        HSSFColor bgColor = cellStyle.getFillForegroundColorColor();
                        if (bgColor != null) {
                            short[] rgbColor = bgColor.getTriplet();
                            myCell.setBgColor("rgb(" + rgbColor[0] + "," + rgbColor[1] + "," + rgbColor[2] + ")");
                        }
                        HSSFFont font = cell.getCellStyle().getFont(workbook);
                        myCell.setTextSize(font.getFontHeightInPoints() + "");
                        if (font.getBold()) {
                            myCell.setTextWeight("bold");
                        }
                        HSSFColor textColor = font.getHSSFColor(workbook);
                        if (textColor != null) {
                            short[] rgbColor = textColor.getTriplet();
                            myCell.setTextColor("rgb(" + rgbColor[0] + "," + rgbColor[1] + "," + rgbColor[2] + ")");
                        }
                        myCell.setContent(readCellContent(cell));
                        data.get(i).add(myCell);
                    } else {
                        data.get(i).add(new MyCell(""));
                    }
                }
            }
        }
    } finally {
        if (workbook != null) {
            workbook.close();
        }
    }
    return data;
}
Also used : HashMap(java.util.HashMap) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFCellStyle(org.apache.poi.hssf.usermodel.HSSFCellStyle) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFColor(org.apache.poi.hssf.util.HSSFColor) ArrayList(java.util.ArrayList) List(java.util.List) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFFont(org.apache.poi.hssf.usermodel.HSSFFont)

Example 48 with HSSFRow

use of org.apache.poi.hssf.usermodel.HSSFRow in project OpenOLAT by OpenOLAT.

the class ExcelDocument method readContent.

@Override
protected FileContent readContent(VFSLeaf leaf) throws IOException, DocumentException {
    int cellNullCounter = 0;
    int rowNullCounter = 0;
    int sheetNullCounter = 0;
    try (BufferedInputStream bis = new BufferedInputStream(leaf.getInputStream());
        HSSFWorkbook workbook = new HSSFWorkbook(new POIFSFileSystem(bis))) {
        LimitedContentWriter content = new LimitedContentWriter((int) leaf.getSize(), FileDocumentFactory.getMaxFileSize());
        for (int sheetNumber = 0; sheetNumber < workbook.getNumberOfSheets(); sheetNumber++) {
            HSSFSheet sheet = workbook.getSheetAt(sheetNumber);
            if (sheet != null) {
                for (int rowNumber = sheet.getFirstRowNum(); rowNumber <= sheet.getLastRowNum(); rowNumber++) {
                    HSSFRow row = sheet.getRow(rowNumber);
                    if (row != null) {
                        for (int cellNumber = row.getFirstCellNum(); cellNumber <= row.getLastCellNum(); cellNumber++) {
                            HSSFCell cell = row.getCell(cellNumber);
                            if (cell != null) {
                                if (cell.getCellTypeEnum() == CellType.STRING) {
                                    content.append(cell.getStringCellValue()).append(' ');
                                }
                            } else {
                                cellNullCounter++;
                            }
                        }
                    } else {
                        rowNullCounter++;
                    }
                }
            } else {
                sheetNullCounter++;
            }
        }
        if (log.isDebug()) {
            if ((cellNullCounter > 0) || (rowNullCounter > 0) || (sheetNullCounter > 0)) {
                log.debug("Read Excel content cell=null #:" + cellNullCounter + ", row=null #:" + rowNullCounter + ", sheet=null #:" + sheetNullCounter);
            }
        }
        content.close();
        return new FileContent(content.toString());
    } catch (Exception ex) {
        throw new DocumentException("Can not read XLS Content. File=" + leaf.getName(), ex);
    }
}
Also used : LimitedContentWriter(org.olat.core.util.io.LimitedContentWriter) BufferedInputStream(java.io.BufferedInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) IOException(java.io.IOException)

Example 49 with HSSFRow

use of org.apache.poi.hssf.usermodel.HSSFRow in project titan.EclipsePlug-ins by eclipse.

the class ExportedProblemMerger method collectSmellNames.

/**
 * Collect the smell names contained in a sheet.
 *
 * @param sheet
 *            The sheet to process
 */
private void collectSmellNames(final HSSFSheet sheet) {
    final int rows = sheet.getLastRowNum();
    int row = 2;
    while (row <= rows) {
        final HSSFRow actualRow = sheet.getRow(row);
        if (actualRow != null) {
            final Cell cell = actualRow.getCell(0);
            final String name = cell.getStringCellValue();
            // new smell found
            if (!smellrow.containsKey(name) && !name.isEmpty()) {
                smellrow.put(name, smellindex);
                smellindex += 1;
            }
        }
        row += 1;
    }
}
Also used : HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) Cell(org.apache.poi.ss.usermodel.Cell)

Example 50 with HSSFRow

use of org.apache.poi.hssf.usermodel.HSSFRow in project nextprot-api by calipho-sib.

the class EntryIsoformXLSWriterTest method assertXLSEquals.

private static void assertXLSEquals(ByteArrayOutputStream baos, String[] headers, Object[]... expectedRows) throws IOException {
    InputStream is = new ByteArrayInputStream(baos.toByteArray());
    HSSFWorkbook workbook = new HSSFWorkbook(is);
    HSSFSheet worksheet = workbook.getSheet("Isoforms");
    HSSFRow headerRow = worksheet.getRow(0);
    for (int rowIndex = 0; rowIndex < expectedRows.length; rowIndex++) {
        Object[] expectedRow = expectedRows[rowIndex];
        HSSFRow valuesRow = worksheet.getRow(rowIndex + 1);
        for (int i = 0; i < headers.length; i++) {
            Assert.assertEquals(headers[i], headerRow.getCell(i).getStringCellValue());
            if (i > 2)
                Assert.assertEquals(expectedRow[i], valuesRow.getCell(i).getNumericCellValue());
            else
                Assert.assertEquals(expectedRow[i], valuesRow.getCell(i).getStringCellValue());
        }
    }
    workbook.close();
}
Also used : HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Aggregations

HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)124 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)98 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)82 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)71 HSSFCellStyle (org.apache.poi.hssf.usermodel.HSSFCellStyle)29 FileOutputStream (java.io.FileOutputStream)24 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)24 Test (org.junit.Test)18 IOException (java.io.IOException)16 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)15 HSSFFont (org.apache.poi.hssf.usermodel.HSSFFont)14 File (java.io.File)13 ArrayList (java.util.ArrayList)12 CellValue (org.apache.poi.ss.usermodel.CellValue)10 OutputStream (java.io.OutputStream)8 HashMap (java.util.HashMap)8 Map (java.util.Map)7 AssertionFailedError (junit.framework.AssertionFailedError)7 FileInputStream (java.io.FileInputStream)6 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)6