加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

解决java读取excel文件有小数点的问题

(2017-04-04 13:52:02)
标签:

javaweb

java读取excel文件

java

分类: javaweb

今天在写代码的时候发现,无论怎么读取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

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有