0

I am trying to read an excel file , which contains only one column , and its an array of json , I am not able to read it by apache-poi in the form of string .

XSSFWorkbook workbook = new XSSFWorkbook(file); 
XSSFSheet worksheet = workbook.getSheetAt(0);
int index = 0;

for (index = 0; index < worksheet.getPhysicalNumberOfRows(); index++) {
    if (index > 0) {
        XSSFRow row = worksheet.getRow(index);
        String b = row.getCell(0).getStringCellValue();
        System.out.println(b);
    }
}

This code is not returning the value of the columns in string form. Please tell me - how can I read the col values in string as it is?

Sample of Excel file

1
  • 1
    What output does it give? Commented Oct 10, 2022 at 10:39

1 Answer 1

1

I solved your problem once try this

HSSFWorkbook wb=new HSSFWorkbook(file);
        HSSFSheet sheet=wb.getSheetAt(0);
        FormulaEvaluator formulaEvaluator=wb.getCreationHelper().createFormulaEvaluator();
        for(Row row: sheet)
        {
            for(Cell cell: row)
            {
                switch(formulaEvaluator.evaluateInCell(cell).getCellType())
                {
                    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();
        }

I took the input in .xls file like this

enter image description here

and this is the output

enter image description here

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

Comments

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.