Search in sources :

Example 46 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project selenium_java by sergueik.

the class ExcelConfiguration method CreateHeader.

@SuppressWarnings("deprecation")
public static XSSFRow CreateHeader(XSSFWorkbook book, XSSFSheet sheet, String[] headers) {
    XSSFRow row = sheet.createRow(0);
    for (int column = 0; column < headers.length; column++) {
        XSSFCell headerCell = row.createCell(column);
        XSSFCellStyle headerStyle = book.createCellStyle();
        headerStyle.setAlignment(HorizontalAlignment.CENTER);
        headerStyle.setBorderBottom(BorderStyle.THIN);
        headerStyle.setBorderLeft(BorderStyle.THIN);
        headerStyle.setBorderRight(BorderStyle.THIN);
        headerStyle.setBorderTop(BorderStyle.THIN);
        // headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        headerStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
        headerStyle.setLocked(true);
        headerCell.setCellStyle(headerStyle);
        headerCell.setCellValue(headers[column]);
    }
    return row;
}
Also used : XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell)

Example 47 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project selenium_java by sergueik.

the class ExcelConfiguration method appendExcelDataToMap.

public static HashMap<String, ArrayList<String>> appendExcelDataToMap(XSSFSheet xlSheet) {
    XSSFRow row = null;
    Map<String, ArrayList<String>> sheetMap = new HashMap<>();
    int i = xlSheet.getFirstRowNum() + 1;
    for (; i <= xlSheet.getLastRowNum(); i++) {
        row = xlSheet.getRow(i);
        ArrayList<String> newData = addDataToMap(row, new ArrayList<String>());
        sheetMap.put(row.getCell(Configuration.testNameIndex).getStringCellValue(), newData);
    }
    return (HashMap<String, ArrayList<String>>) sheetMap;
}
Also used : XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 48 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project selenium_java by sergueik.

the class ExcelConfiguration method appendExcelData.

public static HashMap<String, ArrayList<String>> appendExcelData(XSSFSheet xlSheet, HashMap<String, ArrayList<String>> sheetMap) {
    XSSFRow row = null;
    int i = xlSheet.getFirstRowNum() + 1;
    for (; i <= xlSheet.getLastRowNum(); i++) {
        row = xlSheet.getRow(i);
        if (sheetMap.get(row.getCell(Configuration.testNameIndex).getStringCellValue()) == null) {
            ArrayList<String> newData = addDataToMap(row, new ArrayList<String>());
            sheetMap.put(row.getCell(Configuration.testNameIndex).getStringCellValue(), newData);
        }
    }
    return sheetMap;
}
Also used : XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow)

Example 49 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project selenium_java by sergueik.

the class ReportData method createExcelFile.

// @SuppressWarnings("deprecation")
public static XSSFWorkbook createExcelFile(HashMap<String, Map<String, ArrayList<String>>> data) {
    XSSFWorkbook book = new XSSFWorkbook();
    XSSFCellStyle failCelStyle = book.createCellStyle();
    XSSFCellStyle passCelStyle = book.createCellStyle();
    // http://stackoverflow.com/questions/19145628/auto-size-height-for-rows-in-apache-poi
    for (String sheetNameKey : data.keySet()) {
        XSSFSheet sheet = book.createSheet(sheetNameKey);
        XSSFRow row = ExcelConfiguration.CreateHeader(book, sheet, Configuration.header);
        HashMap<String, ArrayList<String>> testMethods = (HashMap<String, ArrayList<String>>) data.get(sheetNameKey);
        int l = 1;
        for (String testMethod : testMethods.keySet()) {
            passCelStyle.setFillForegroundColor(HSSFColor.BRIGHT_GREEN.index);
            failCelStyle.setFillForegroundColor(HSSFColor.RED.index);
            // XSSFColor myColor = new XSSFColor(XSSFColor.);
            // failCelStyle.setFillForegroundColor(myColor);
            // converting to poi 3.17
            // passCelStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            passCelStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            // failCelStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            failCelStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            row = sheet.createRow(l++);
            XSSFCell cellName = row.createCell(Configuration.testNameIndex);
            cellName.setCellValue(testMethod);
            sheet.autoSizeColumn(Configuration.testNameIndex);
            ArrayList<String> testData = testMethods.get(testMethod);
            XSSFCell cellStatus = row.createCell(Configuration.testStatusIndex);
            if ("fail".equalsIgnoreCase(testData.get(Configuration.testStatusIndex))) {
                cellStatus.setCellStyle(failCelStyle);
                cellStatus.setCellValue(testData.get(Configuration.testStatusIndex));
                XSSFCell expCell = row.createCell(Configuration.exceptionMsgIndex);
                expCell.setCellValue(testData.get(Configuration.exceptionMsgIndex));
                sheet.autoSizeColumn(Configuration.exceptionMsgIndex);
                XSSFCell exceptionTraceCell = row.createCell(Configuration.exceptionStackTrace);
                exceptionTraceCell.setCellValue(testData.get(Configuration.exceptionStackTrace).trim());
                sheet.autoSizeColumn(Configuration.exceptionStackTrace);
                XSSFCell locatorCell = row.createCell(Configuration.locatorIndex);
                String text = testData.get(Configuration.exceptionStackTrace).trim();
                String jsonString = null;
                Gson gson = new GsonBuilder().create();
                int sIndex = text.indexOf('{');
                int eIndex = text.indexOf('}');
                jsonString = (sIndex == -1 || eIndex == -1) ? "" : text.substring(sIndex, (eIndex + 1));
                ElementLocator locator = gson.fromJson(jsonString, ElementLocator.class);
                locatorCell.setCellValue((locator == null) ? "" : locator.toString());
            } else {
                cellStatus.setCellStyle(passCelStyle);
                cellStatus.setCellValue(testData.get(Configuration.testStatusIndex));
                sheet.autoSizeColumn(Configuration.testStatusIndex);
                XSSFCell expCell = row.createCell(Configuration.exceptionMsgIndex);
                expCell.setCellValue(testData.get(Configuration.exceptionMsgIndex));
                sheet.autoSizeColumn(Configuration.exceptionMsgIndex);
                XSSFCell exceptionTraceCell = row.createCell(Configuration.exceptionStackTrace);
                exceptionTraceCell.setCellValue(testData.get(Configuration.exceptionStackTrace).trim());
                sheet.autoSizeColumn(Configuration.exceptionStackTrace);
            }
        }
    }
    return book;
}
Also used : HashMap(java.util.HashMap) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) ElementLocator(com.xls.report.config.ElementLocator) Gson(com.google.gson.Gson) XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell)

Example 50 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project selenium_java by sergueik.

the class ExcelFileWriter method writeToFile.

@Override
public boolean writeToFile(String fileName, List<LocatorModel> dData) {
    int i = 0;
    try (XSSFWorkbook book = getWorkBook()) {
        XSSFSheet sheet = book.createSheet(SHEET_NAME);
        for (LocatorModel model : dData) {
            XSSFRow row = sheet.createRow(i++);
            row.createCell(0).setCellValue(model.getLocatorType());
            row.createCell(1).setCellValue(model.getLocatorValue());
        }
        book.write(new FileOutputStream(new File(ResourceHelper.getResourcePath("excel/") + fileName + ".xlsx")));
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) LocatorModel(com.driver.locator.model.LocatorModel) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) File(java.io.File) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) IOException(java.io.IOException)

Aggregations

XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)59 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)39 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)39 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)35 FileOutputStream (java.io.FileOutputStream)17 IOException (java.io.IOException)16 ArrayList (java.util.ArrayList)15 FileInputStream (java.io.FileInputStream)14 BufferedReader (java.io.BufferedReader)10 FileNotFoundException (java.io.FileNotFoundException)10 Test (org.junit.Test)10 InputStreamReader (java.io.InputStreamReader)9 Reader (java.io.Reader)9 File (java.io.File)8 HashMap (java.util.HashMap)8 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)8 BufferedWriter (java.io.BufferedWriter)6 FileWriter (java.io.FileWriter)6 HashSet (java.util.HashSet)5 Map (java.util.Map)5