java apache poi (part 2) - java

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();
}
}

Related

How does an Excel cell handle null data using Apache POI?

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();

How to read contents of Excel sheet and storing it in a map with column1 as key, and the remaining columns as values

I have an excel sheet which looks like this :
Col_1 | Col_2| Col_3 | Col_4
A | B | C | D
A1 | B1 | C1 | D1
How to read this excel and store the values in a map with A as key, and B,C,D as values for that key.
Please find my codes below, which is not working as expected.
while (rowIterator.hasNext())
{
ArrayList<String> columnlist = new ArrayList<String>();
List excelValues = new ArrayList();
String key = StringUtils.EMPTY;
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
if(row.getRowNum() > 0)
{
if(cell.getColumnIndex()==0){
key = StringUtils.upperCase(cell.getStringCellValue());
}
else if(cell.getColumnIndex()==1)
{
tempPar = cell.getStringCellValue();
excelValues.add(tempPar);
}
if(tempPar.equalsIgnoreCase("Include")){
if(cell.getColumnIndex()==2)
{
// Responsible for reading comma separated values from cell in excel sheet.
StringTokenizer str= new StringTokenizer(cell.getStringCellValue(),COMMA);
while (str.hasMoreElements()) {
columnlist.add(StringUtils.upperCase(str.nextElement().toString()));
}
excelValues.add(columnlist.equals("") ? "" : columnlist);
}
else if(cell.getColumnIndex()==3){
ignorePK = cell.getStringCellValue();
ignorePK.add(cell.getStringCellValue());
excelValues.add(ignorePK);
}
}
map.put(key, excelValues);
}
I have written the code to read excel and store it to database using Apache Poi . You can refer it out and modify your own code.
try {
ExcelController ex = new ExcelController();
File f1 = ex.convert(file);
// FileInputStream file = new FileInputStream(new File("E://Imp/Details.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(f1);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
rowIterator.next();
// while (rowIterator.hasNext()) {
for(int i=1;i<=51;i++){
Row row = rowIterator.next();
//For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
// System.out.println("cell.getStringCellValue().length() "+cell.getStringCellValue().length());
// if( cell.getStringCellValue().length()>0){
List list = new ArrayList();
// This will change all Cell Types to String
cell.setCellType(Cell.CELL_TYPE_STRING);
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
String bstring = "" + cell.getBooleanCellValue();
list.add(cell.getBooleanCellValue());
System.out.println("boolean===>>>" + cell.getBooleanCellValue() + "\t");
break;
case Cell.CELL_TYPE_NUMERIC:
String Nstring = "" + cell.getNumericCellValue();
list.add(Nstring);
break;
case Cell.CELL_TYPE_STRING:
list.add(cell.getStringCellValue());
break;
}
// }else {
// break;
// }
}
After you get the values over there just push it to the Map you have created.

Get the no of columns in excel [duplicate]

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;
}
}
}

Using Apache POI how to read a specific excel column

I'm having a problem in excel while using Apache POI. I can read across rows, but sometimes I'm in a situation where I would like to read a particular column only.
So is it possible to read any particular column like only the 'A' column only or the column 'C' only.
I'm using the Java language for this.
heikkim is right, here is some sample code adapted from some code I have:
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
...
for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
row = sheet.getRow(rowIndex);
if (row != null) {
Cell cell = row.getCell(colIndex);
if (cell != null) {
// Found column and there is value in the cell.
cellValueMaybeNull = cell.getStringCellValue();
// Do something with the cellValueMaybeNull here ...
// break; ???
}
}
}
For the colCount use something like row.getPhysicalNumberOfCells()
Sheet sheet = workBook.getSheetAt(0); // Get Your Sheet.
for (Row row : sheet) { // For each Row.
Cell cell = row.getCell(0); // Get the Cell at the Index / Column you want.
}
My solution, a bit simpler code wise.
Okay, from your question, you just simply want to read a particular column. So, while iterating over a row and then on its cells, your can simply check the index of the column.
Iterator<Row> rowIterator = mySheet.iterator(); // Traversing over each row of XLSX file
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();
println "column index"+cell.getColumnIndex()//You will have your columns fixed in Excel file
if(cell.getColumnIndex()==3)//for example of c
{
print "done"
}
}
}
I am using POI 3.12-- 'org.apache.poi:poi:3.12'
Hope it helps. Cheers!
You could just loop the rows and read the same cell from each row (doesn't this comprise a column?).
import java.io.*;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.ss.usermodel.*;
import java.text.*;
public class XSLXReader {
static DecimalFormat df = new DecimalFormat("#####0");
public static void main(String[] args) {
FileWriter fostream;
PrintWriter out = null;
String strOutputPath = "H:\\BLR_Team\\Kavitha\\Excel-to-xml\\";
String strFilePrefix = "Master_5.2-B";
try {
InputStream inputStream = new FileInputStream(new File("H:\\BLR_Team\\Kavitha\\Excel-to-xml\\Stack-up 20L pure storage 11-0039-01 ISU_USA-A 1-30-17-Rev_exm.xls"));
Workbook wb = WorkbookFactory.create(inputStream);
// Sheet sheet = wb.getSheet(0);
Sheet sheet =null;
Integer noOfSheets= wb.getNumberOfSheets();
for(int i=0;i<noOfSheets;i++){
sheet = wb.getSheetAt(i);
System.out.println("Sheet : "+i + " " + sheet.getSheetName());
System.out.println("Sheet : "+i + " " + sheet.getFirstRowNum());
System.out.println("Sheet : "+i + " " + sheet.getLastRowNum());
//Column 29
fostream = new FileWriter(strOutputPath + "\\" + strFilePrefix+i+ ".xml");
out = new PrintWriter(new BufferedWriter(fostream));
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.println("<Bin-code>");
boolean firstRow = true;
for (Row row : sheet) {
if (firstRow == true) {
firstRow = false;
continue;
}
out.println("\t<DCT>");
out.println(formatElement("\t\t", "ID", formatCell(row.getCell(0))));
out.println(formatElement("\t\t", "Table_name", formatCell(row.getCell(1))));
out.println(formatElement("\t\t", "isProddaten", formatCell(row.getCell(2))));
out.println(formatElement("\t\t", "isR3P01Data", formatCell(row.getCell(3))));
out.println(formatElement("\t\t", "LayerNo", formatCell(row.getCell(29))));
out.println("\t</DCT>");
}
CellReference ref = new CellReference("A13");
Row r = sheet.getRow(ref.getRow());
if (r != null) {
Cell c = r.getCell(ref.getCol());
System.out.println(c.getRichStringCellValue().getString());
}
for (Row row : sheet) {
for (Cell cell : row) {
CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.println(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.println(cell.getDateCellValue());
} else {
System.out.println(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
System.out.println(cell.getCellFormula());
break;
case Cell.CELL_TYPE_BLANK:
System.out.println();
break;
default:
System.out.println();
}
}
}
out.write("</Bin-code>");
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String formatCell(Cell cell)
{
if (cell == null) {
return "";
}
switch(cell.getCellType()) {
case Cell.CELL_TYPE_BLANK:
return "";
case Cell.CELL_TYPE_BOOLEAN:
return Boolean.toString(cell.getBooleanCellValue());
case Cell.CELL_TYPE_ERROR:
return "*error*";
case Cell.CELL_TYPE_NUMERIC:
return XSLXReader.df.format(cell.getNumericCellValue());
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
default:
return "<unknown value>";
}
}
private static String formatElement(String prefix, String tag, String value) {
StringBuilder sb = new StringBuilder(prefix);
sb.append("<");
sb.append(tag);
if (value != null && value.length() > 0) {
sb.append(">");
sb.append(value);
sb.append("</");
sb.append(tag);
sb.append(">");
} else {
sb.append("/>");
}
return sb.toString();
}
}
This code does 3 things:
Excel to XML file generation. Eng. Name Dong Kim
Prints the content of a particular cell : A13
Also print the excel content into normal text format. Jars to be imported: poi-3.9.jar,poi-ooxml-3.9.jar,poi-ooxml-schemas-3.9.jar,xbea‌​n-2.3.0.jar,xmlbeans‌​-xmlpublic-2.4.0.jar‌​,dom4j-1.5.jar
Here is the code to read the excel data by column.
public ArrayList<String> extractExcelContentByColumnIndex(int columnIndex){
ArrayList<String> columndata = null;
try {
File f = new File("sample.xlsx")
FileInputStream ios = new FileInputStream(f);
XSSFWorkbook workbook = new XSSFWorkbook(ios);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
columndata = new ArrayList<>();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if(row.getRowNum() > 0){ //To filter column headings
if(cell.getColumnIndex() == columnIndex){// To match column index
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
columndata.add(cell.getNumericCellValue()+"");
break;
case Cell.CELL_TYPE_STRING:
columndata.add(cell.getStringCellValue());
break;
}
}
}
}
}
ios.close();
System.out.println(columndata);
} catch (Exception e) {
e.printStackTrace();
}
return columndata;
}
Please be aware, that iterating through the columns using row cell iterator ( Iterator<Cell> cellIterator = row.cellIterator();) may lead to silent skipping columns. I have just encountered a document that was exposing such behaviour.
Iterating using indexes in a for loop and using row.getCell(i) was not skipping columns and was returning values at the correct column indexes.

How to get an Excel Blank Cell Value in Apache POI?

I have a huge excel file with tons of columns which looks like this :-
Column1 Column2 Column3 Column4 Column5
abc def ghi
mno pqr
......
This is the code that I wrote to print these values:
try {
FileInputStream inputStr = new FileInputStream(fileName);
XSSFWorkbook xssfWork = new XSSFWorkbook(inputStr) ;
XSSFSheet sheet1 = xssfWork.getSheetAt(0);
Iterator rowItr = sheet1.rowIterator();
while ( rowItr.hasNext() ) {
XSSFRow row = (XSSFRow) rowItr.next();
System.out.println("ROW:-->");
Iterator cellItr = row.cellIterator();
while ( cellItr.hasNext() ) {
XSSFCell cell = (XSSFCell) cellItr.next();
System.out.println("CELL:-->"+cell.toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
The output generated by this code is :-
ROW:-->
CELL:-->Column1
CELL:-->Column2
CELL:-->Column3
CELL:-->Column4
CELL:-->Column5
ROW:-->
CELL:-->abc
CELL:-->def
CELL:-->ghi
ROW:-->
CELL:-->mno
CELL:-->pqr
So, If we look at the output above we can note that the cells where I left blank values was not picked up by the POI library , is there a way in which I can get these values as null. or a way to recognize that the values presented skipped blank cells?
Thanks.
If you want to get all cells, no matter if they exist or not, then the iterator isn't for you. Instead, you need to manually fetch the appropriate cells, likely with a missing cell policy
for(Row row : sheet) {
for(int cn=0; cn<row.getLastCellNum(); cn++) {
// If the cell is missing from the file, generate a blank one
// (Works by specifying a MissingCellPolicy)
Cell cell = row.getCell(cn, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
// Print the cell for debugging
System.out.println("CELL: " + cn + " --> " + cell.toString());
}
}
There's more details on all of this in the Apache POI documentation on iterating over cells
I have been frustrated by this same problem. Here is what I found with poi-3.7-20101029 and poi-3.8.
RowIterator and CellIterator do not support iterating over NULL cells or rows -- only physically defined cells (which can be BLANK).
The solution that returns what I expect requires using the 0-based Row.getCell([int], Row.CREATE_NULL_AS_BLANK), much like Chavira's answer alludes to (assuming 8 cell rows). Or you can use the Cell.columnIndex value while iterating to check for jumping numbers...
Annoyingly, after creating blank cells using method #1, the iterators will return the now created BLANK cells. I consider it a bug that MissingCellPolicy is ignored by CellIterator.
The reason is quite simple: Excel files can contain as many rows and as many columns as possibles, so returning all available blank rows and columns will render the cells huge and memory intensive.
Assuming you have a 10x10 sheet, in Excel, it's not "exactly" 10x10 since you can add 11x10 very easily with blank cell, so should POI return the 11th column?
One way to do what you're requesting is to use HSSFCell.getColumnIndex().
Example:
//Assuming your have a 2 dimensional array.
String[][] values = ......;// It is assigned
POIFSFileSystem fileSystem = new POIFSFileSystem(new FileInputStream(fileName));
HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);
//Going through every worksheet.
for (int sheetPos = 0; sheetPos < workbook.getNumberOfSheets(); sheetPos++) {
HSSFSheet sheet = workbook.getSheetAt(sheetPos);
int rowPos = 0;
Iterator<Row> rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
Iterator<Cell> cells = row.cellIterator();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
String value = "";
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
value = BigDecimal.valueOf(cell.getNumericCellValue()).toPlainString();
break;
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
value = "";
break;
case HSSFCell.CELL_TYPE_FORMULA:
value = cell.getCellFormula();
break;
default:
break;
}
values[rowPos][cell.getColumnIndex()] = value;
}
rowPos++;
}
}
Below is what worked for me. The "row.CREATE_NULL_AS_BLANK" did not appear to be valid but that could be lack of NPOI knowledge.
HSSFCell dataCell= (HSSFCell)row.GetCell(column, NPOI.SS.UserModel.MissingCellPolicy.CREATE_NULL_AS_BLANK);
for(org.apache.poi.ss.usermodel.Row tmp : hssfSheet){
for(int i = 0; i<8;i++){
System.out.println(tmp.getCell(i));
}
}
This worked for me....
int rowNumber;
int previousCell;
int currentCell;
int currentRowNumber;
HSSFCell cell;
while (rows.hasNext()) {
previousCell = -1;
currentCell = 0;
while (cellIterator.hasNext()) {
cell = (HSSFCell) cellIterator.next();
currentCell = cell.getColumnIndex();
if (previousCell == currentCell-1) {
//...
}
else {
System.out.println("Blank cell found");
}
previousCell = currentCell;
}
}
List cellDataList = new ArrayList();
int lineNumber = 0;
while (rowIterator.hasNext()) {
HSSFRow hssfRow = (HSSFRow) rowIterator.next();
//System.out.println("Befor If");
lineNumber++;
if(lineNumber==1){continue;}
//System.out.println("Out side if ");
Iterator<Cell> iterator = hssfRow.cellIterator();
List<Cell> cellTempList = new ArrayList();
int current = 0, next = 1;
while (iterator.hasNext()) {
Cell hssfCell = iterator.next();
current = hssfCell.getColumnIndex();
if(current<next){
System.out.println("Condition Satisfied");
}
else{
int loop = current-next;
System.out.println("inside else Loop value : "+(loop));
for(int k=0;k<loop+1;k++){
System.out.println("Adding nulls");
cellTempList.add(null);
next = next + 1;
}
}
cellTempList.add(hssfCell);
next = next + 1;
System.out.println("At End next value is : "+next);
}
cellDataList.add(cellTempList);
}
public String[] rowToString(Row row)
{
Iterator<Cell> cells = row.cellIterator() ;
String[] data = new String[row.getLastCellNum()] ;
int previousCell = 0 ;
Cell cell = cells.next() ;
int currentCell = cell.getColumnIndex();
while (true)
{
if (previousCell == currentCell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
data[previousCell] = cell.getNumericCellValue()+"" ;
break;
case Cell.CELL_TYPE_STRING:
data[previousCell] = cell.getStringCellValue() ;
break;
/* // there could be other cases here.
case Cell.CELL_TYPE_FORMULA:
data[previousCell] =eval.evaluateFormulaCell(cell);
break;
case Cell.CELL_TYPE_BOOLEAN:
data[previousCell] = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_BLANK:
data[previousCell] = "";
break;
case Cell.CELL_TYPE_ERROR:
data[previousCell] = "ERROR";
break;
*/
}
if(cells.hasNext()){
cell = cells.next() ;
currentCell = cell.getColumnIndex();
} else {
break ;
}
} else {
data[previousCell] = "";
}
previousCell++ ;
}
return data ;
}
for (Row row: sheet){
// This will return null if cell is empty / blank
Cell cell = row.getCell(columnNumber);
}

Categories

Resources