UtkarshBhavsar
UtkarshBhavsar

Reputation: 259

How to get value from excel using apache poi

I just tried to get each cell values from excel file but i get each cell already merged but i want each cell different

import java.io.File;import java.io.FileInputStream;import java.util.Iterator;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class Read {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

            try
            {
                FileInputStream file = new FileInputStream(new File("D://new/excelnew/student_usr_mst_dtls.xlsx"));

                //Create Workbook instance holding reference to .xlsx file
                XSSFWorkbook workbook = new XSSFWorkbook(file);

                //Get first/desired sheet from the workbook
                XSSFSheet sheet = workbook.getSheetAt(0);

                //Iterate through each rows one by one
                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();

                    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());
                                break;
                        }
                    }
                    System.out.println("");
                }
                file.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }

}

and OutPut is like as string

IDNAMELASTNAME 1.0tAmitShukla 2.0tLokeshGupta

I want each cell uniquely so how to do it. if possible give an example please.

Upvotes: 0

Views: 1150

Answers (1)

Eran
Eran

Reputation: 393771

Put a comma after each cell value you print.

System.out.print(cell.getNumericCellValue() + "t,");

and

System.out.print(cell.getStringCellValue() + ",");

Upvotes: 2

Related Questions