Using apache poi, I make Excel data reading logic.
but, the output data comes out incorrectly because the null value is not processed.
What should I do here?
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case NUMERIC:
System.out.print((int) cell.getNumericCellValue() + "\t");
break;
case STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
}
}
System.out.println();
You need to do the check of null value in the cell.
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if(cell==null) {
System.out.print("nothing"+ "\t");
continue;
}
switch (cell.getCellType()) {
case NUMERIC:
System.out.print((int) cell.getNumericCellValue() + "\t");
break;
case STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
}
}
System.out.println();
Related
Is there any way we can covert the XSSFSheet to the list of POJO.
The number of rows will result in the size of ArrayList.
Here is the below sheet I have.
And here is the POJO -
public class Sprint {
String name;
Integer age;
String phone;
// getter and setters
}
And here is function -
public String singleFileUpload(FileBucket fileBucket) throws IOException {
MultipartFile multipartFile = fileBucket.getFile();
FileCopyUtils.copy(fileBucket.getFile().getBytes(), new File(UPLOAD_LOCATION + fileBucket.getFile().getOriginalFilename()));
XSSFWorkbook workbook = new XSSFWorkbook(multipartFile.getInputStream());
XSSFSheet sheet = workbook.getSheetAt(0);
List<Sprint> sprint= new ArrayList();
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
// here I want to convert sheet to list of POJO
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "t");
break;
}
}
}
return "success";
}
Please help!.
Thanks in advance.
cont. on java apache poi (part 1)
Code
...
while(rowIterator.hasNext()){
List<String> record = new ArrayList<String>();
row = (XSSFRow)rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()){
cell = cellIterator.next();
cell.setCellType(Cell.CELL_TYPE_STRING);
switch(cell.getCellType()){
case Cell.CELL_TYPE_STRING:
record.add(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
record.add(Double.toString
(cell.getNumericCellValue()));
break;
}
}
readFile();
}
public void readFile(){
String ID = record.get(0);
System.out.println(ID);
}
...
From above code, my output is like below:
ID
1
2
3
My expected output should like this:
1
2
3
My question is how to remove the first row from excel (ID) from the above code?
To skip the first row:
while(rowIterator.hasNext()){
row = (XSSFRow)rowIterator.next();
if(row.getRowNum()==0) {
continue;
}
List<String> record = new ArrayList<String>();
Iterator<Cell> cellIterator = row.cellIterator();
...
readFile();
}
Add rowIterator.next(); above the While loop in the program which ignore the first row.So simple.Hope this helps you.
**rowIterator.next();**
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
//Check the cell type and format accordingly
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "t");
break;
}
}
System.out.println("");
}
file.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
This question already has answers here:
get number of columns of a particular row in given excel using Java
(3 answers)
Closed 8 years ago.
In my java application I am trying to convert excel to pdf using apache-poi.In my excel file,the number of columns are different in some rows.First and second rows contain one column,remaining rows contains 8 columns.Here is my code
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
int cellNumber = 0;
if (flag) {
table = new PdfPTable(row.getLastCellNum());
flag = false;
}
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
if (temp == 0) {
numberOfColumns = row.getLastCellNum();
PdfPCell c1 = new PdfPCell(new Phrase(
cell.getStringCellValue()));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);
}else{
cellNumber =checkEmptyCellAndAddCellContentToPDFTable(cellNumber,cell,table);
}
cellNumber++;
break;
case Cell.CELL_TYPE_NUMERIC:
cellNumber =checkEmptyCellAndAddCellContentToPDFTable(cellNumber,cell,table);
cellNumber++;
break;
}
}
temp = 1;
if(numberOfColumns != cellNumber){
for(int i=0;i<(numberOfColumns-cellNumber);i++){
table.addCell(" ");
}
}
}
So when I execute this,I will get the pdf file with only one column.But I want same as my excel format.How can I achieve this in my code dynamically(I have to apply this logic for other excel files also)?
You can use this approach to iterate each cell of every row .
Sheet sheet = workbook.getSheetAt(0);
//Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
if(isRowEmpty(row)){
System.out.println("Row "+row.getRowNum()+"is a empty row");
continue;
}
Iterator<Cell> cellIterator = row.cellIterator();
Cell cell =null;
while (cellIterator.hasNext()) {
cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
String value = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
Double d = cell.getNumericCellValue();
break;
case Cell.CELL_TYPE_STRING:
String value = cell.getStringCellValue().trim();
break;
case Cell.CELL_TYPE_BLANK:
break;
}
}
}
I need to read excel file which more than 300 rows. I need to extract value from particular cell only.
Code
Workbook workbook = Workbook.getWorkbook(new File(excelFile));
Sheet sheet = workbook.getSheet(0);
Cell emp_name = sheet.getCell(1,2);
Cell emp_dpt = sheet.getCell(2,2);
Cell emp_pdpt = sheet.getCell(3,2);
Cell emp_no = sheet.getCell(4,2);
Cell emp_desn = sheet.getCell(5,2);
Cell emp_dj = sheet.getCell(6,2);
Cell emp_lvl = sheet.getCell(7,2);
Cell emp_eval = sheet.getCell(8,2);
String name = emp_name.getContents();
String dpartment = emp_dpt.getContents();
String pre_department = emp_pdpt.getContents();
String employee_no = emp_no.getContents();
String designation = emp_desn.getContents();
String datejoined = emp_dj.getContents();
String evalution = emp_eval.getContents();
System.out.println(name);
System.out.println(dpartment);
System.out.println(pre_department);
System.out.println(employee_no);
System.out.println(designation);
System.out.println(datejoined);
System.out.println(evalution);
Above code helps me to fetch data from the excel but only one value extracted. How do I fetch all the data from excel.
Do it in a loop
When you say , sheet.getCell(1,2), you read the cell with column 1 and row 2.
Support if you want to read , column 1 and row 3, then do this sheet.getCell(1,3);
sheet.getRows() ->Returns the number of rows in this sheet
pseudo code:
for (int rowNum = 5; rowNum < sheet.getRows(); rowNum++) {
int column = 4;
sheet.getCell(column ++, rowNum).getContents(); //read 4th column and 5th row into a variable or object as per your logic;
sheet.getCell(column ++, rowNum).getContents(); //read 5th column and 5th row;
......
}
you can use iterator that allows you to read each and every cell
see example here
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch(cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t\t");
break;
}
}
im developing one tool for uploading excel and validate it, but excel file is large and the row count is about 65536.
here is my code used for uploading and reading the excel sheet values
int firstColNo = 1;
int rowcount = sheet.getRows();
int colCount = sheet.getColumns();
int row = 0;
String comp = "";
for (row = 1; row < rowcount; row++) {
if (labelCell != null) {
cell = sheet.getCell(firstColNo, row);
if (cell.getContents() != null && cell.getContents().length() > 0){
String compoundId = cell.getContents();
System.out.println(compoundId);
} else {
System.out.println("-");
}
}
}
by reading the row values it takes more to time to read, is there any way to make it faster else any code modifications need to be done in my code?
can anybody help me to overcome this issue.
Here is an example for you
HSSFWorkbook workbook = new HSSFWorkbook(file);
//Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch(cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t\t");
break;
}
}
System.out.println("");
}