从Excel读取数值,但在Java中显示错误
我希望能够将excel中的数值输出到java控制台。(没有小数点)例如1,2,3,但是我得到了以下结果:1.0,2.0,3.0 我需要在代码中添加什么 一揽子fyp.fyp从Excel读取数值,但在Java中显示错误,java,excel,Java,Excel,我希望能够将excel中的数值输出到java控制台。(没有小数点)例如1,2,3,但是我得到了以下结果:1.0,2.0,3.0 我需要在代码中添加什么 一揽子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.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();
}
}
}
}
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.Collection;
导入java.util.Iterator;
导入java.util.List;
导入org.apache.poi.xssf.usermodel.XSSFCell;
导入org.apache.poi.xssf.usermodel.XSSFRow;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
公共类读写{
公共静态void main(字符串[]args)引发IOException{
//源文件的位置
字符串sourceFilePath=“C:\\Users\\Daveycol\\Desktop\\test.xlsx”;
FileInputStream FileInputStream=null;
//用于存储excel工作表数据的数组列表
List excelSheetData=new ArrayList();
试一试{
//FileInputStream以读取excel文件
fileInputStream=新的fileInputStream(sourceFilePath);
//创建excel工作簿
XSSF工作簿Excel工作簿=新XSSF工作簿(fileInputStream);
//检索工作簿的第一张工作表。
XSSFSheet excelSheet=excelWorkBook.getSheetAt(0);
//遍历图纸行和单元格。
//将检索到的数据存储在arrayList中
迭代器行=excelSheet.rowIterator();
while(rows.hasNext()){
XSSFRow行=(XSSFRow)行。下一步();
迭代器单元格=行。单元格迭代器();
List cellData=newarraylist();
while(cells.hasNext()){
XSSFCell cell=(XSSFCell)cells.next();
cellData.add(cell);
}
excelSheetData.add(cellData);
}
//将检索到的数据打印到控制台
对于(int-rowNum=0;rowNum
如果只需要整数部分,则可以使用
System.out.print((int) cell.getNumericCellValue() + " ")
您的excel可能有类似于1.0、2.0 Hanks的字段,它们是否也会以这种方式存储在ArrayList中?如何在excel中排除某些单元格的读取。例如,如果我有一个表,但我不想包含头,那么就在ArrayList中存储XSSFCell的代码实例。getNumericCellValue()返回double。每次需要XSSFCell的int值时,都可以使用以下表达式将double转换为int:“(int)cell.getNumericCellValue()”这对我来说太棒了!不从excel中读取标题会怎么样?只有数据?如果知道只有一个标题行,则可以从第一行开始迭代:for(int rowNum=1;rowNum