Created
April 3, 2021 19:46
-
-
Save PavelCp/4b617d4910ba4b29eb3a312223afd1ff to your computer and use it in GitHub Desktop.
Package related to .xlsx reading.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.read.excel; | |
import org.apache.poi.ss.usermodel.*; | |
import java.io.*; | |
import java.util.HashMap; | |
import java.util.LinkedHashMap; | |
import java.util.LinkedList; | |
import java.util.List; | |
public class ExcelReader { | |
private static String filepath; | |
public static void main(String[] args) throws IOException { | |
new ExcelReader("C:\\input.xlsx").getData(); | |
} | |
public List<HashMap<String, Object>> getData() throws IOException { | |
List<HashMap<String, Object>> listOfMaps = new LinkedList<>(); | |
String[] header; | |
// InputStream inp = new FileInputStream(setFilePath("C:\\input.xlsx")); | |
InputStream inp = new FileInputStream(new File(filepath)); | |
//InputStream inp = new FileInputStream("workbook.xlsx"); | |
Workbook wb = WorkbookFactory.create(inp); | |
Sheet sheet = wb.getSheetAt(0); | |
Row rowKeys = sheet.getRow(0); | |
header = new String[rowKeys.getLastCellNum()]; | |
for (int i = 0; i < header.length; i++) { | |
header[i] = rowKeys.getCell(i).getStringCellValue(); | |
System.out.println("header[" + i + "]: " + header[i]); | |
} | |
for (int i = 1; i < sheet.getLastRowNum() + 1; i++) { | |
LinkedHashMap<String, Object> map = new LinkedHashMap<>(); | |
for (int j = 0; j < rowKeys.getLastCellNum(); j++) { | |
map.put(header[j], sheet.getRow(i).getCell(j).getStringCellValue()); | |
} | |
listOfMaps.add(map); | |
} | |
// System.out.println(listOfMaps.get(0).keySet()); | |
return listOfMaps; | |
} | |
// public static File setFilePath(String filePath){ | |
// return new File(filePath); | |
// } | |
public ExcelReader(String filepath){ | |
ExcelReader.filepath = filepath; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.read.excel; | |
import java.util.*; | |
public class MapsTuts { | |
static String[][] values = {{"Check google", "https://www.google.com/", "cat"}, | |
{"Check google", "https://www.google.com/","dog"}, | |
{"Check google", "https://www.google.com/", "bird"}, | |
{"Check yahoo", "https://www.yahoo.com", "car"}, | |
{"Check yahoo", "https://www.yahoo.com", "train"}}; | |
static List<HashMap<String, String>> listOfMaps = new LinkedList<>(); | |
public static void main(String[] args) { | |
String[] header = {"Test case name", "URL", "Keyword"}; | |
for (int i = 0; i < values.length; i++) { | |
HashMap<String, String> map = new HashMap<>(); | |
for (int j = 0; j < values[i].length; j++) { | |
map.put(header[j], values[i][j]); | |
} | |
listOfMaps.add(map); | |
} | |
System.out.println("\nlistOfMaps.size()" + listOfMaps.size()); | |
for (int i = 0; i < listOfMaps.size(); i++) { | |
System.out.println("listOfMaps.get(" + i + "): " | |
+ header[0] + ": " + listOfMaps.get(i).get(header[0]) + ", " | |
+ header[1] + ": " + listOfMaps.get(i).get(header[1]) + ", " | |
+ header[2] + ": " + listOfMaps.get(i).get(header[2])); | |
} | |
// for (int i = 0; i < metaMap.size(); i++) { | |
// System.out.println("meta vals:\nTest case name: " + metaMap.get("Test case name") + | |
// ", URL: " + metaMap.get("URL") + | |
// ", Keyword: " + metaMap.get("Keyword")); | |
// } | |
} | |
private static List<Integer> getRowNumber(String testCaseName){ | |
List<Integer> listOfEntries = new LinkedList<>(); | |
for (int i = 0; i < values.length; i++) { | |
if (testCaseName.equals(values[i][0])) | |
listOfEntries.add(i); | |
} | |
return listOfEntries; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment