作者:渴望飞的鱼
原文地址:https://blog.csdn.net/qq_36411874/article/details/69062002
原因:
虽然excel中设置的都是文本,但是数字文本还被读错,如“1”取成“1.0”, 加上下面这句,临时把它当做文本来读取。
解决方法:
user= new User();
HSSFCell userid = hssfRow.getCell(0);
加上这句话转换: userid.setCellType(userid.CELL_TYPE_STRING);
打印出来测试已经没有了小数点:System.out.println(getValue(userid));
贴代码:
package com.hmy.ssh.myMethod;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.hmy.ssh.bean.User;
//import com.b510.common.Common;
//import com.b510.excel.vo.Student;
/*
* 1.通过java读取excel文档存储信息,必须先写好行数、对应的列。
* 2.用户选择路径就可以
*
* */
public class ReadUserExcel {
public static void main(String[] args){
ReadUserExcel readExcel=new ReadUserExcel();
try {
List exam=readExcel.readXls("F:\\a1项目设计(IT科技节)\\课程网在线中心\\读取文档\\userText0404.xls");
for(User myexam:exam){
System.out.println(myexam);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 通过路径存储用户信息
* */
public List readXls(String path) throws IOException {
// InputStream is = new FileInputStream(Common.EXCEL_PATH);
// InputStream is = new FileInputStream("F:\\a1项目设计(IT科技节)\\课程网在线中心\\读取文档\\userText0404.xls");
InputStream is = new FileInputStream(path);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
User user = null;
List list = new ArrayList();
// 循环工作表Sheet
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 循环行Row
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow != null) {
user= new User();
HSSFCell userid = hssfRow.getCell(0);
userid.setCellType(userid.CELL_TYPE_STRING);
HSSFCell userName = hssfRow.getCell(1);
userName.setCellType(userid.CELL_TYPE_STRING);
HSSFCell userPwd = hssfRow.getCell(2);
userPwd.setCellType(userid.CELL_TYPE_STRING);
HSSFCell userEmail = hssfRow.getCell(3);
userEmail.setCellType(userid.CELL_TYPE_STRING);
HSSFCell userPhone = hssfRow.getCell(4);
userPhone.setCellType(userid.CELL_TYPE_STRING);
HSSFCell userType = hssfRow.getCell(5);
userType.setCellType(userid.CELL_TYPE_STRING);
HSSFCell trueName = hssfRow.getCell(6);
trueName.setCellType(userid.CELL_TYPE_STRING);
HSSFCell authority = hssfRow.getCell(7);
authority.setCellType(userid.CELL_TYPE_STRING);
// 虽然excel中设置的都是文本,但是数字文本还被读错,如“1”取成“1.0”
// 加上下面这句,临时把它当做文本来读取
// userid.setCellType(userid.CELL_TYPE_STRING);
System.out.println(getValue(userid));
//------测试新加入
user.setUserid(Integer.parseInt(getValue(userid)));
user.setUserName(getValue(userName));
user.setUserPwd(getValue(userPwd));
user.setUserEmail(getValue(userEmail));
user.setUserPhone(getValue(userPhone));
user.setUserType(getValue(userType));
user.setTrueName(getValue(trueName));
// user.setRightResult(getValue(authority));
user.setAuthority(Integer.valueOf(getValue(authority)));
list.add(user);
// System.out.println("myExam:"+user);
// student.setNo(getValue(no));
// student.setName(getValue(name));
// student.setAge(getValue(age));
// student.setScore(Float.valueOf(getValue(score)));
// list.add(student);
}
}
}
return list;
}
@SuppressWarnings("static-access")
private String getValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
// 返回布尔类型的值
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
// 返回数值类型的值
return String.valueOf(hssfCell.getNumericCellValue());
} else {
// 返回字符串类型的值
return String.valueOf(hssfCell.getStringCellValue());
}
}
}