How to start on specific cell in a loop using apache poi? - java

I have an app that reads an excel and gets the information. This is my code that reads the information from column 0 and column 1. I want to know a way to start only on row number 34 and skip row 1 from 33.
File file = new File(inFileName);
Workbook workBook = WorkbookFactory.create(file);
Sheet sheet = workBook.getSheetAt(0);
Iterator<Row> rowIter = sheet.rowIterator();
while(rowIter.hasNext()){
Row myRow =rowIter.next();
Cell cell1 = myRow.getCell(0);
Cell cell2 = myRow.getCell(1);
...
Iterator<Cell> cellIter = myRow.cellIterator();
while(cellIter.hasNext()){
Cell myCell = cellIter.next();
}
//bd.addAnimalls(cell1.toString(),cell2.toString());
Toast.makeText(getApplicationContext(), "on inserting", Toast.LENGTH_SHORT).show();
}

You can get specific rows using sheet.getRow(rowNum).
So your code can be changed to this:
File file = new File(inFileName);
Workbook workBook = WorkbookFactory.create(file);
Sheet sheet = workBook.getSheetAt(0);
int rowCtr = 33;
Row myRow = sheet.getRow(rowCtr++);
while (myRow != null) {
Cell cell1 = myRow.getCell(0);
// rest of your loop code
myRow = sheet.getRow(rowCtr++);
}

Related

How to reduce code when create an cell in apache poi?

I am creating a sheet with apache poi, it have a lot of column and row name so I create a lot of code, I have a lot of code duplicate, How can I can create a function and reuse it for another?
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet1");
try (OutputStream os = new FileOutputStream("report.xls")) {
Row row;
Cell cell;
row = sheet.createRow(0);
cell = row.createCell(0);
cell.setCellValue("Shop:");
cell = row.createCell(1);
cell.setCellValue(4);
cell = row.createCell(6);
cell.setCellValue("Export Date:");
cell = row.createCell(7);
cell.setCellValue("14.06.2019");
// -----
row = sheet.createRow(2);
cell = row.createCell(0);
cell.setCellValue("Code Supervisor:");
cell = row.createCell(1);
cell.setCellValue(4);
cell = row.createCell(6);
cell.setCellValue("Audit Date:");
cell = row.createCell(7);
cell.setCellValue("dd.MM.yyyy");
//----
row = sheet.createRow(4);
cell = row.createCell(6);
cell.setCellValue("Shop Note:");
cell = row.createCell(7);
cell.setCellValue("Note something");
wb.write(os);
}catch(Exception e) {
System.out.println(e.getMessage());
}

Writing to excel using Apache poi(Error)

I'm writing data into excel file using Apache Poi In Java Eclipse,Problem is when i add column aligned with each row,it override the previous column...here is the code before i add anything.
public static void main(String[] args) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet spreadsheet = workbook.createSheet("cellstyle");
XSSFRow row=spreadsheet.createRow(1);
XSSFCell cell;
row = spreadsheet.createRow((short) 4);
row.setHeight((short) 800);
cell = (XSSFCell) row.createCell((short) 5);
cell.setCellValue("Total Alerts raised");
spreadsheet.addMergedRegion(new CellRangeAddress(4,4,5,6));
row = spreadsheet.createRow(5);
cell = (XSSFCell) row.createCell(5);
cell.setCellValue("Critical");
row = spreadsheet.createRow(6);
cell = (XSSFCell) row.createCell(5);
cell.setCellValue("Warning");
row = spreadsheet.createRow(7);
cell = (XSSFCell) row.createCell(5);
cell.setCellValue("Total");
FileOutputStream out = new FileOutputStream(
new File("C:\\Users\\tshivhaser\\Desktop\\cellstyle.xlsx"));
workbook.write(out);
out.close();
System.out.println("cellstyle.xlsx written successfully");
}
I then add this piece of code...Critical,Warning and Total column becomes empty
row = spreadsheet.createRow(5);
cell = row.createCell(6);
cell.setCellValue(0);
row = spreadsheet.createRow(6);
cell = row.createCell(6);
cell.setCellValue(0);
row = spreadsheet.createRow(7);
cell = row.createCell(6);
cell.setCellValue(10);
Please help on how i can have all cells filled...

Reading one additional row of excel file with POI

Using Apache POI, I am trying to read an excel file. The file has 1000 rows and 1 column. With this code:
XSSFSheet ws = workbook.getSheetAt(0);
Iterator< Row > rowIt = ws.iterator();
XSSFRow row;
int i = 0;
while ( rowIt.hasNext() ) {
row = (XSSFRow) rowIt.next();
Iterator< Cell > cellIt = row.cellIterator();
while ( cellIt.hasNext() ) {
Cell cell = cellIt.next();
my_array[ i ] = cell.getStringCellValue();
}
++i;
}
It seems that it reads 1001 rows and since the last row is "", my_array get invalid string. Is there any way to fix that? I expect rowIt.hasNext() is responsible for that but it doesn't work as expected.
The file has 1000 rows and 1 column : you must specify what column you are reading.
here an exemple that specify column with this excel file:
public class TestLecture {
public static void main(String[] args) throws IOException{
List<String> mys_list= new ArrayList<String>();
FileInputStream file = new FileInputStream(new File("test.xlsx"));
//Get the workbook instance for XLS file
XSSFWorkbook workbook = new XSSFWorkbook (file);
//Get first sheet from the workbook
XSSFSheet ws = workbook.getSheetAt(0);
//Get iterator to all the rows in current sheet
Iterator<Row> rowIt = ws.iterator();
while (rowIt.hasNext()) {
Row row = rowIt.next();
Iterator<Cell> cellIt = row.iterator();
while (cellIt.hasNext()) {
Cell cell = cellIt.next();
int columnIndex = cell.getColumnIndex();
switch (columnIndex) {
case 2:
mys_list.add(cell.getStringCellValue());
break;
}
}
}
System.out.println(mys_list.size());
for(String g :mys_list){
System.out.println(g);
}
}
}

getting null pointer exception when trying to read from a cell in excel using webdriver

XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet =wb.getSheet("TestData");
Row row = sheet.getRow(1);
Cell cell = row.getCell(1);
String x = "";
x =cell.getStringCellValue();
System.out.println(x);
I am getting a NullPointerException at Row row = sheet.getRow(1);

how to get data from 2nd row

i am trying to get data based on headers in the below first i am geting first row and storing it has keys in map . but the problem is i want to get the data from 2nd row but i got iterator.how can i get data from second row??
DataFormatter df=new DataFormatter();
Map<String,Integer> m=new HashMap<String,Integer>();
FileInputStream file = new FileInputStream(new File("/root/Documents/xyz.xlsx"));
//Get the workbook instance for XLS file
XSSFWorkbook workbook = new XSSFWorkbook(file);
//Get first sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
Row row=sheet.getRow(0);
XSSFRow rowe = (XSSFRow)row;
int s=row.getLastCellNum();
System.out.println(s);
for(int i=0;i<s-1;i++){
String data= df.formatCellValue(rowe.getCell(i));
System.out.println(data);
m.put(data,i);
}
Iterator<Row> rows = sheet.rowIterator();
while (rows.hasNext()) {
XSSFRow rowa = (XSSFRow) rows.next();
System.out.println(df.formatCellValue(rowa.getCell(m.get("Member.Member Card Number"))));
}
Is this what you are looking for ?
int count =0;
while (rows.hasNext()) {
XSSFRow rowa = (XSSFRow) rows.next();
if(count !=0){
System.out.println(df.formatCellValue(rowa.getCell(m.get("Member.Member Card Number"))));
}
count++;
}
Try something like this
Vector cellVectorHolder = new Vector();
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
Iterator rowIter = mySheet.rowIterator();
while(rowIter.hasNext()){
XSSFRow myRow = (XSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();
List list = new ArrayList();
while(cellIter.hasNext()){
XSSFCell myCell = (XSSFCell) cellIter.next();
list.add(myCell);
}
cellVectorHolder.addElement(list);
}

Categories

Resources