1

I want to be able to output the numeric values as they are in excel to the java console. (with no decimal places) e.g 1, 2, 3, but i'm getting the following: 1.0, 2.0, 3.0 What do I need to add into my code?

package fyp.fyp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Read_Write {

    public static void main(String[] args) throws IOException {

        // Location of the source file
        String sourceFilePath = "C:\\Users\\Daveycol\\Desktop\\test.xlsx";

        FileInputStream fileInputStream = null;

        // Array List to store the excel sheet data
        List excelSheetData = new ArrayList();

        try {

            // FileInputStream to read the excel file
            fileInputStream = new FileInputStream(sourceFilePath);

            // Create an excel workbook
            XSSFWorkbook excelWorkBook = new XSSFWorkbook(fileInputStream);

            // Retrieve the first sheet of the workbook.
            XSSFSheet excelSheet = excelWorkBook.getSheetAt(0);

            // Iterate through the sheet rows and cells. 
            // Store the retrieved data in an arrayList
            Iterator rows = excelSheet.rowIterator();
            while (rows.hasNext()) {
                XSSFRow row = (XSSFRow) rows.next();
                Iterator cells = row.cellIterator();

                List cellData = new ArrayList();
                while (cells.hasNext()) {
                    XSSFCell cell = (XSSFCell) cells.next();
                    cellData.add(cell);
                }

                excelSheetData.add(cellData);
            }

            // Print retrieved data to the console
            for (int rowNum = 0; rowNum < excelSheetData.size(); rowNum++) {

                List list = (List) excelSheetData.get(rowNum);

                for (int cellNum = 0; cellNum < list.size(); cellNum++) {

                    XSSFCell cell = (XSSFCell) list.get(cellNum);

                    if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
                        System.out.print(cell.getRichStringCellValue().getString() + " ");
                    } else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
                        System.out.print(cell.getNumericCellValue() + " ");
                    } else if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
                        System.out.println(cell.getBooleanCellValue() + " ");
                    }
                }
                System.out.println("");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                fileInputStream.close();
            }
        }
    }
}
1
  • your excel might have fields like 1.0,2.0 Commented Mar 30, 2014 at 12:47

1 Answer 1

1

If you need only integer part you can use

System.out.print((int) cell.getNumericCellValue() + " ")
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, will they also be stored in the ArrayList this way? How would i also exclude certain cells from being read in excel. So for example if I had a table and i didn't want to include the headers?
your code store instances of XSSFCell in the ArrayList. cell.getNumericCellValue() returns double. Every time you need an int value from a XSSFCell you can use the following expression to convert double to int: "(int) cell.getNumericCellValue()"
Brilliant that works great for me! what about not reading in headers from excel? only the data?
If you know that there is only one header row you can start iterating from the first row: for (int rowNum = 1; rowNum < excelSheetData.size(); rowNum++) { .... }

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.