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++;
}
}
}
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;
}
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);
}
}
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;
}
}
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();
}
Aggregations