今天在写代码的时候发现,无论怎么读取excel时候,自带加上了小数点。
原因:
虽然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;
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.setAuthority(Integer.valueOf(getValue(authority)));
list.add(user);
}
}
}
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());
}
}
}
当然,通过springmvc控制,可以对数据库进行用户在网页添加了路径,然后读取本地文件到服务器的数据库中。代码如下:
在controller层中添加这个方法,接受地址即可读取文档。
@ResponseBody
@RequestMapping("/addUserList")
//---------------根据地址读取文档
//public List addUser(@RequestParam("addressName")String
addressName) {
//http://localhost:8888/MyCourseOnlineProject///addUserList?addressName=F:\\a1项目设计(IT科技节)\\课程网在线中心\\读取文档\\userText0404.xls
public boolean addUser(@RequestParam("addressName")String
addressName) {
//8.传用户文档:扫描插入用户
//boolean flag=userService.addUser(usersList.getUsersList());
List examlist = null;
ReadUserExcel readExcel=new
ReadUserExcel();
try {
examlist=readExcel.readXls(addressName);
for(User myexam:examlist){
System.out.println(myexam);
}
flag=userService.addUser(examlist);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// return examlist;
return flag;
}
接着,编写jsp:
喜欢
0
赠金笔
加载中,请稍候......