Checking if an arralist contains an excel's cell value - java

I have an ArrayList<Cell> RM = new ArrayList<Cell>() ;
where RM=[aaa,bbb] and where aaa and bbb are some cells from an excel file
I use this code to get the cells values into the arraylist.
Row row = sheet.getRow(1);
Iterator<Cell> cells1 = row.cellIterator();
while( cells1.hasNext() ) {
Cell cell = cells1.next();
if (Cell.CELL_TYPE_STRING==cell.getCellType()) {
RM.add(cell);
}
}
Then I am trying to see if the arraylist contains aaa or bbb, but it doesnt recognise the command. System.out.println(RM) shows the proper results [aaa,bbb] but if add an if(contains) condition it wont:
if (RM.contains("aaa")) {
System.out.println("ok"); }

Changed it to:
ArrayList<String> RM = new ArrayList<String>() ;
RM.add(cell.getStringCellValue());
and contains() works properly

Related

Java: How to push the excel data in to java map(<string,ArrayList<Strings>)

I am facing a weird problem here, i need to read the excel data which starts after some rows.
My excel input is something like below
Row1 : This report is to display all the user details
Row2 : Kindly find the below details
Row3 : TABLE1 (This is the identifier after this row my table data is available).
Row4 : ID Name DOB
Row5 : 101 RAM 10-07-1986
Row6 : 102 Sita 24-08-1989
Row6 : Table2
note:i need to read only row4 to row6jav
I need the output like below in the map,
mymap [ID =[101,102],[Name = RAM,Sita],[DOB = 10-07-1986,24-08-1989]]
I have tried the below code which is working absolutely fine if my first 3 rows are not there, only creating issue if i give first 3 rows. your help is much appreciated.
public static void main(String[] args) {
try {
File file = new File("C:\\demo\\employee.xlsx"); //creating a new file instance
FileInputStream fis = new FileInputStream(file); //obtaining bytes from the file
//creating Workbook instance that refers to .xlsx file
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0); //creating a Sheet object to retrieve object
Iterator<Row> itr = sheet.iterator(); //iterating over excel file
// CAREFUL HERE! use LinkedHashMap to guarantee the insertion order!
Map<String, List<String>> myMap = new LinkedHashMap<>();
// populate map with headers and empty list
if (itr.hasNext()) {
Row row = itr.next();
Iterator<Cell> headerIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
myMap.put(getCellValue(cell), new ArrayList<>());
}
}
Iterator<List<String>> columnsIterator;
// populate lists
while (itr.hasNext()) {
// get the list iterator every row to start from first list
columnsIterator = myMap.values().iterator();
Row row = itr.next();
Iterator<Cell> cellIterator = row.cellIterator(); //iterating over each column
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
// here don't check hasNext() because if the file not contains problems
// the # of columns is same as # of headers
columnsIterator.next().add(getCellValue(cell));
}
}
// here your map should be filled with data as expected
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getCellValue(Cell cell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING: //field that represents string cell type
return cell.getStringCellValue() + "\t\t\t";
case Cell.CELL_TYPE_NUMERIC: //field that represents number cell type
return cell.getNumericCellValue() + "\t\t\t";
case Cell.CELL_TYPE_Date: //field that represents Date cell type
return cell.getDateCellValue() + "\t\t\t";
default:
return "";
}
}

How to display an Object in a column with apache-poi

I have a big DTO with exactly 234 fields, and I have to display values of each fields of this DTO in a column of an Excel file created with apache-poi.
This is my code :
// Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Export values");
// Get the Entity
Simfoot simEntity = simService.findById(simId).get();
Row row = sheet.createRow(0);
row.createCell(1).setCellValue("Consult our values");
// and after this I want to convert my Simfoot object to a column in the third column ( so creteCell(2) ..... ).
I want to have in my first column : nothing , in my second only the String display ( "Consult our values" ) and in my third column I need to have my 234 fields. With an field ( the value of the field ) in one cell. So, 234 rows displaying one value in the third column.
I hope that it is clear.
Thanks a lot for your help.
Using some reflection:
// Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
final Sheet sheet = workbook.createSheet("Export values");
// Get the Entity
final Simfoot simEntity = simService.findById(simId).get();
Row row = sheet.createRow(0);
row.createCell(1).setCellValue("Consult our values");
// and after this I want to convert my Simfoot object to a column in the third column ( so creteCell(2) ..... ).
Arrays.stream(simEntity.getClass().getDeclaredMethods())
.filter(m -> m.getName().startsWith("get") && m.getParameterTypes().length == 0 && !void.class.equals(m.getReturnType()))
.forEach(m -> {
try {
Object value = m.invoke(simEntity, null);
Row r = sheet.createRow(sheet.getLastRowNum()+1);
r.createCell(2).setCellValue(value == null ? "" : value.toString());
}
catch (Exception ex) {
// Manage Exception....
}
});
I'll add a method on Simfoot to return all the values:
public List<String> getAllValues() {
return Arrays.asList(getAtt1(), getAtt2(), .. , getAtt234());
}
Then create a row per attribute, and then you can merge the rows of the first 2 columns. Example here with 6 attributes:
int n = 6; // would be 234 for you
XSSFCellStyle styleAlignTop = workbook.createCellStyle();
styleAlignTop.setVerticalAlignment(VerticalAlignment.TOP);
Row row;
for(int i=0; i<n; i++) {
row = sheet.createRow(i);
if(i==0) {
Cell cell = row.createCell(1);
cell.setCellStyle(styleAlignTop);
cell.setCellValue("Consult our values");
}
row.createCell(2).setCellValue(simEntity.getAllValues().get(i));
}
sheet.addMergedRegion(new CellRangeAddress(0, n-1, 0, 0));
sheet.addMergedRegion(new CellRangeAddress(0, n-1, 1, 1));
It shows like this:
Another way to list your attributes would be to use Reflection but I find it very clunky:
Simfoot simEntity = new Simfoot("pap", "pep", "pip", "pop", "pup", "pyp");
for(PropertyDescriptor propertyDescriptor :
Introspector.getBeanInfo(Simfoot.class).getPropertyDescriptors()) {
System.out.println(propertyDescriptor.getReadMethod().invoke(simEntity));
}
Outputs:
pap
pep
pip
pop
pup
pyp
class Simfoot
so you have to filter out getClass and any other unwanted methods and getters

How do i get visible row count using apache POI?

I have used autofilter to filter that data in excel and i want number of visible rows excluding hidden rows. I have tried searching for this, but no particular method found. I even tried using excel formula and getting row count, but this only gives cached value.
outputSheet.setAutoFilter(CellRangeAddress.valueOf("A1:C491"));
CTAutoFilter sheetFilter=outputSheet.getCTWorksheet().getAutoFilter();
CTFilterColumn myFilterColumn=sheetFilter.insertNewFilterColumn(0);
myFilterColumn.setColId(1);
CTFilterColumn myFilterColumn2=sheetFilter.insertNewFilterColumn(1);
myFilterColumn2.setColId(2);
CTFilters firstColumnFilter=myFilterColumn.addNewFilters();
CTFilter myFilter1=firstColumnFilter.addNewFilter();
CTFilters secondColumnFilter=myFilterColumn2.addNewFilters();
CTFilter myFilter2=secondColumnFilter.addNewFilter();
myFilter1.setVal("New Account");
myFilter2.setVal("Hedge Fund");
List<String> list1 = new ArrayList<String>();
list1.add("Ad Cloud");
List<String> list2 = new ArrayList<String>();
list2.add("US");
XSSFRow r1;
for(Row r : my_sheet) {
for (Cell c : r) {
c.setCellType(Cell.CELL_TYPE_STRING);
if ( (c.getColumnIndex()==1 && !list2.contains(c.getStringCellValue())) || (c.getColumnIndex()==2 && !list1.contains(c.getStringCellValue())) ){
r1=(XSSFRow) c.getRow();
if (r1.getRowNum()!=0) { /* Ignore top row */
/* Hide Row that does not meet Filter Criteria */
r1.getCTRow().setHidden(true);
}
}
}
}
outputworkbook.write(fos);
fos.close();
//format(outputFile);

Read Excel file containing multiple values in single column -Java

I'm reading Excel file using Apache POI.
My Excel table structure is like this
|2000s| 2001, 2003, 2008, 2009|
so for right hand side data, I require it to assign to 2000s
Till now I've implemented this way:
List<Class> list = new ArrayList<Class>();
File file = new File(file_path);
FileInputStream fis = new FileInputStream(file);
//Create an instance of workbook which refers to an excel file
XSSFWorkbook wb = new XSSFWorkbook(fis);
//This selects the 1st sheet
XSSFSheet sheet = wb.getSheetAt(0);
//Iterate through each row one by one
Iterator<Row> itr = sheet.iterator();
String newName = null;
String oldName = null;
while(itr.hasNext()){
Row nextRow = itr.next();
// For each row, iterate through all the columns
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
newName = nextRow.getCell(0).toString();
if(nextRow.getCell(1).toString().contains(",")){
StringTokenizer st = new StringTokenizer(nextRow.getCell(1).toString(),",");
while(st.hasMoreTokens()){
oldName = st.nextToken();
}
}
else{
oldName = nextRow.getCell(1).toString();
}
}
System.out.println();
}
When I compile, it throws me "Null pointer Exception" at nextRow.getCell(1) line.
I don't understand how do I map all comma values to 2000s.
This is working perfectly fine for normal data(without comma).
Comma values have been handled
I'm posting answer so somebody can get help from here.
What I've done is- added String Tokenizer class and if there's comma in the cell, it breaks the value with the comma delimiter.
Lets have a look at the code below
while(itr.hasNext()){
Row nextRow = itr.next();
// For each row, iterate through all the columns
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
newName = nextRow.getCell(0).toString();
if(nextRow.getCell(1).toString().contains(",")){
StringTokenizer st = new StringTokenizer(nextRow.getCell(1).toString(),",");
while(st.hasMoreTokens()){
oldName = st.nextToken();
}
}
else{
oldName = nextRow.getCell(1).toString();
}
}
System.out.println();
}
Here newName gets the value of 1st col.(2000s)
and oldName gets the tokens based on ',' delimiter- In this case 2001, 2003, 2008, 2009
for all these values of oldName, newName 2000s would be mapped.
UPDATE: Reason I was getting 'Null Pointer Exception' there, because some cells at 2nd column(nextRow.getCell(1)) are null.
So whenever iterator reaches to the null cell, it throws Null Pointer Exception.
Here you need to assign Missing Cell Policy
by
Cell cell2 = row.getCell(j,org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK);
(It just treats null values as blank)
This way you can also resolve Null pointer exception in Excel while reading from Null values

Retrieve values from excel using poi

I am trying to get the column values for a specific row in a excel using poi methods.
I am able to get the values but the problem is I want the values only from second column.
public static ArrayList<String> GetBusinessComponentList() throws IOException{
String Tcname = "TC02_AggregateAutoByPassRO_CT";
ArrayList<String> arrayListBusinessFlow ;
arrayListBusinessFlow = new ArrayList<String>();
FileInputStream fileInput = new FileInputStream(oFile);
wb = new HSSFWorkbook(fileInput);
sheet = wb.getSheet("Business Flow");
int rownr = findRow(sheet, Tcname);
row = sheet.getRow(rownr);
for (Cell cell : row) {
String arr = cell.getStringCellValue();
arrayListBusinessFlow.add(arr);
}
return arrayListBusinessFlow;
}
private static int findRow(HSSFSheet sheet, String cellContent){
for (Row row : sheet) {
for (Cell cell : row) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
if (cell.getRichStringCellValue().getString().trim().equals(cellContent)) {
return row.getRowNum();
}
}
}
}
return 0;
}
}
OUTPUT:
[TC02_AggregateAutoByPassRO_CT,
StrategicUINewBusiness.Login,
StrategicUINewBusiness.CustomerSearch,
StrategicUINewBusiness.NamedInsured,
StrategicUINewBusiness.InsuranceScoreByPass,
StrategicUINewBusiness.VehiclePage,
StrategicUINewBusiness.DriverPage,
StrategicUINewBusiness.ViolationPage,
StrategicUINewBusiness.UnderwritingPage,
StrategicUINewBusiness.CoveragePage,
StrategicUINewBusiness.Portfolio,
StrategicUINewBusiness.BillingPage,
StrategicUINewBusiness.FinalSalePage,
StrategicUINewBusiness.PolicyConfirmation, , , ]
But I do not want my test case name when I am getting.
Please help me what changes i needed to do. thanks!
Currently, the code you're using to iterate over cells only returns cells with content or styling, and skips totally empty ones. You need to change to one of the other ways of iterating over cells, so you can control it to read from the second column onwards.
If you look at the Apache POI Documentation on iterating over rows and cells, you'll see a lot more details on the two main ways to iterate.
For your case, you'll want something like:
// We want to read from the 2nd column onwards, zero based
int firstColumn = 1;
// Always fetch at least 4 columns
int MY_MINIMUM_COLUMN_COUNT = 5;
// Work out the last column to go to
int lastColumn = Math.max(r.getLastCellNum(), MY_MINIMUM_COLUMN_COUNT);
// To format cells into strings
DataFormatter df = new DataFormatter();
// Iterate over the cells
for (int cn = firstColumn; cn < lastColumn; cn++) {
Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
if (c == null) {
// The spreadsheet is empty in this cell
} else {
// Do something useful with the cell's contents
// eg get the cells value as a string
String cellAsString = df.formatCellValue(c);
}
}
Use Cell cell=row.getCell(1); and also you can use sheet.getLastRowNum() to get the number last row on the sheet.
for (int i=0;i<=row.getLastCellNum();i++) {
if (i!=1){
//your stuff
}
}

Categories