2

My Java

public class Readexcel {
public void readfile(String filepath, String filename, String sheetname) throws IOException
{
    File file = new File(filepath+"\\"+filename);
    FileInputStream fis = new FileInputStream(file);

    // for creating .xlsx workbook
    Workbook wb = new XSSFWorkbook(fis);

    // for reading the sheet by its name
    Sheet sh = wb.getSheet(sheetname);

    //find the total rows in sheet

    int rowcount = sh.getLastRowNum()-sh.getFirstRowNum();

    // create a loop to create 

    for(int i=0;i<rowcount+1;i++)
    {
        Row row= sh.getRow(i);

        // create a loop to print cell values

        for(int j=0;j<row.getLastCellNum();j++)
        {
            Cell cell= row.getCell(j);
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                    System.out.print(row.getCell(j).getStringCellValue() + " ");
                    break;

            case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(row.getCell(j).getNumericCellValue() + " ");
                    break;
            }
            System.out.print(row.getCell(j).getStringCellValue()+"||");

        }


    System.out.println();
    }


    }

   public static void main(String...strings) throws IOException
     {
         Readexcel re = new Readexcel();
         String filepath = "F://Excelsheet";
         re.readfile(filepath,"Book1.xlsx","Sheet1");
      }

}

By using above code I am getting an error "cannot get text value from numeric cell". Any Help? Also My Output is not properly alligned. All the String are showing one under one. output should be like

Username Password
john     123
rambo    456    

but i am getting output like

Username
password
john

1 Answer 1

2

Change your for loop after // create a loop to print cell values comment for this:

for (int j = 0; j < row.getLastCellNum(); j++) {
    Cell cell = row.getCell(j);
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
            System.out.print(row.getCell(j).getStringCellValue() + " ");
            break;

    case Cell.CELL_TYPE_NUMERIC:
            System.out.print((int)row.getCell(j).getNumericCellValue() + " ");
            break;

            }

}

Switch is to recognise type of cell. For numeric cells you have to use getNumericCellValue() instead of getStringCellValue()

For the second problem use System.out.print() instead System.out.println() which is used to print what is between the double quotes and move the printing cursor to the next line.

EDIT:

This how my readFile() function looks:

public void readfile(String filepath, String filename, String sheetname)    throws IOException {


    File file = new File(filepath+"\\"+filename);
    FileInputStream fis = new FileInputStream(file);

    // for creating .xlsx workbook
    Workbook wb = new XSSFWorkbook(fis);

    // for reading the sheet by its name
    Sheet sh = wb.getSheet(sheetname);

    // find the total rows in sheet

    int rowcount = sh.getLastRowNum() - sh.getFirstRowNum();

    // create a loop to create

    for (int i = 0; i < rowcount + 1; i++) {
        Row row = sh.getRow(i);

        // create a loop to print cell values

        for (int j = 0; j < row.getLastCellNum(); j++) {
            Cell cell = row.getCell(j);
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                System.out.print(row.getCell(j).getStringCellValue() + " ");
                break;

            case Cell.CELL_TYPE_NUMERIC:
                System.out.print((int)row.getCell(j).getNumericCellValue() + " ");
                break;

            }

        }

        System.out.println();
    }

}

EDIT 2

Changed System.out.print(row.getCell(j).getNumericCellValue() + " "); for System.out.print((int)row.getCell(j).getNumericCellValue() + " "); in case Cell.CELL_TYPE_NUMERIC

Sign up to request clarification or add additional context in comments.

6 Comments

Username Username|| password password|| john john|| 12345.0 getting output like this(rambo is unable to print)
I pasted my readfile function in my answer. My output looks like this:
Delete that line: System.out.print(row.getCell(j).getStringCellValue()+"||");
I have pasted how my readfile function looks like in my answer
it is very near, the numeric values are 123 but it is showing 123.0
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.