今天在写代码的时候发现,无论怎么读取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; /* * 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()); } } }
当然,通过springmvc控制,可以对数据库进行用户在网页添加了路径,然后读取本地文件到服务器的数据库中。代码如下:
在controller层中添加这个方法,接受地址即可读取文档。
@ResponseBody @RequestMapping("/addUserList") //---------------根据地址读取文档 //public List<User> 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<User> 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:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> This is my JSP page. <br> <form action="<%=basePath %>/addUserList" method="post" enctype="multipart/form-data"> <input type="file" name="addressName" /> <input type="submit" value="Submit" /> </form> </body> </html>
发布上服务器,配置好hibernate,即可成功读取,免去一个个添加的烦恼了