从Excel读取数值但在Java中显示错误

时间:2014-03-30 12:39:50

标签: java excel

我希望能够将excel中的数值输出到java控制台。 (没有小数位)例如1,2,3,但我得到以下内容:1.0,2.0,3.0 我需要在代码中添加什么内容?

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 个答案:

答案 0 :(得分:0)

如果只需要整数部分,可以使用

System.out.print((int) cell.getNumericCellValue() + " ")