Search in sources :

Example 91 with HSSFSheet

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

the class Launcher method main.

public static void main(String[] args) throws IOException {
    Map<String, String> propertiesMap = PropertiesParser.getProperties(String.format("%s/src/main/resources/%s", System.getProperty("user.dir"), propertiesFileName));
    statusColumn = Integer.parseInt(propertiesMap.get("statusColumn"));
    testCase = (propertiesMap.get("testCase") != null) ? propertiesMap.get("testCase") : getPropertyEnv("testCase", String.format("%s\\Desktop\\%s", System.getenv("USERPROFILE"), defaultTestCase));
    System.err.println("Loading test case from: " + testCase);
    FileInputStream file = new FileInputStream(testCase);
    HSSFWorkbook workbook = new HSSFWorkbook(file);
    HSSFSheet indexSheet = workbook.getSheet("Index");
    for (int row = 1; row <= indexSheet.getLastRowNum(); row++) {
        Row indexRow = indexSheet.getRow(row);
        if (safeCellToString(indexRow.getCell(1)).equalsIgnoreCase("Yes") && !safeCellToString(indexRow.getCell(0)).isEmpty()) {
            if (debug) {
                System.err.println("Reading suite: " + indexRow.getCell(0).getStringCellValue());
            }
            Map<Integer, Map<String, String>> steps = readSteps(indexRow.getCell(0).getStringCellValue());
            for (int step = 0; step < steps.size(); step++) {
                Map<String, String> data = steps.get(step);
                for (String param : new ArrayList<String>(data.keySet())) {
                    if (data.get(param) == null) {
                        data.remove(param);
                    }
                }
                String keyword = data.get("keyword");
                KeywordLibrary.callMethod(keyword, data);
                writeStatus(indexRow.getCell(0).getStringCellValue(), step + 1);
            }
        }
    }
    if (debug) {
        System.err.println("Done");
    }
    workbook.close();
}
Also used : ArrayList(java.util.ArrayList) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) Row(org.apache.poi.ss.usermodel.Row) HashMap(java.util.HashMap) Map(java.util.Map) FileInputStream(java.io.FileInputStream) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 92 with HSSFSheet

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

the class Launcher method readSteps.

// reads the spreadsheet into a hash of step keywords and parameters indexed
// by column number and step number
public static Map<Integer, Map<String, String>> readSteps(String sheetName) throws IOException {
    Map<String, String> data = new HashMap<>();
    Map<Integer, Map<String, String>> stepDataMap = new HashMap<>();
    FileInputStream file = new FileInputStream(testCase);
    HSSFWorkbook workbook = new HSSFWorkbook(file);
    HSSFSheet testcaseSheet = workbook.getSheet(sheetName);
    Row stepRow;
    Cell stepCell;
    for (int row = 1; row <= testcaseSheet.getLastRowNum(); row++) {
        if (debug) {
            System.err.println("Row: " + row);
        }
        data = new HashMap<>();
        stepRow = testcaseSheet.getRow(row);
        data.put("keyword", stepRow.getCell(0).getStringCellValue());
        for (int col = 1; col < statusColumn; col++) {
            stepCell = stepRow.getCell(col);
            String cellValue = null;
            try {
                cellValue = safeCellToString(stepCell);
            } catch (NullPointerException | IllegalStateException e) {
                System.err.println("Exception (ignored): " + e.toString());
                cellValue = "";
            }
            if (debug) {
                System.err.println("Column[" + col + "] = " + cellValue);
            }
            data.put(String.format("param%d", col), cellValue);
        }
        stepDataMap.put(row - 1, data);
    }
    workbook.close();
    return stepDataMap;
}
Also used : HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) Row(org.apache.poi.ss.usermodel.Row) HashMap(java.util.HashMap) Map(java.util.Map) Cell(org.apache.poi.ss.usermodel.Cell)

Example 93 with HSSFSheet

use of org.apache.poi.hssf.usermodel.HSSFSheet 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 94 with HSSFSheet

use of org.apache.poi.hssf.usermodel.HSSFSheet 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 95 with HSSFSheet

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

the class ExportedProblemMerger method collectData.

/**
 * Collect all data from the files given in the constructor.
 */
private void collectData() {
    boolean first = true;
    for (final File file : files) {
        HSSFWorkbook workbook = null;
        try {
            workbook = new HSSFWorkbook(new FileInputStream(file));
            final HSSFSheet sheet = workbook.getSheetAt(0);
            if (first) {
                project = getProjectName(sheet);
                first = false;
            } else {
                // check if it is the same project as the first
                if (!project.equals(getProjectName(sheet))) {
                    continue;
                }
            }
            collectSmellNames(sheet);
            collectDates(file, sheet);
        } catch (ArrayIndexOutOfBoundsException e) {
            // outside content boundaries
            ErrorReporter.logExceptionStackTrace("Possibly wrong structure of " + file.getName(), e);
            System.out.println("Possibly wrong structure of " + file.getName());
        } catch (IOException e) {
            ErrorReporter.logExceptionStackTrace("Error while opening " + file.getName(), e);
            System.out.println("Error opening " + file.getName());
        } finally {
            if (workbook != null) {
                workbook = null;
            }
        }
    }
}
Also used : HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) IOException(java.io.IOException) File(java.io.File) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) FileInputStream(java.io.FileInputStream)

Aggregations

HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)213 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)172 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)97 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)84 Test (org.junit.Test)67 FileOutputStream (java.io.FileOutputStream)34 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)27 IOException (java.io.IOException)25 HSSFCellStyle (org.apache.poi.hssf.usermodel.HSSFCellStyle)25 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)25 File (java.io.File)21 ArrayList (java.util.ArrayList)20 HSSFPatriarch (org.apache.poi.hssf.usermodel.HSSFPatriarch)20 Row (org.apache.poi.ss.usermodel.Row)18 FileInputStream (java.io.FileInputStream)17 HSSFClientAnchor (org.apache.poi.hssf.usermodel.HSSFClientAnchor)15 OutputStream (java.io.OutputStream)14 HashMap (java.util.HashMap)14 EscherAggregate (org.apache.poi.hssf.record.EscherAggregate)13 HSSFFont (org.apache.poi.hssf.usermodel.HSSFFont)12