Java Poi 在SSM框架中的应用(由Excel 导入到Mysql数据库)


   最近在跟着组长做项目,之前学了javamail发送邮件,也成功了,等有空了再写一篇博客,昨天学了一下 java poi (没有在框架中应用),今天自己搭建了SSM框架, 手写了一下这个应用,虽然页面比较简陋,但是成功实现了功能,在最后,我会贴出代码,保证大家搭建了数据库就能成功跑起来这个程序。

第一步 :数据库搭建

创建数据库与搭建数据库(查询成功)

create database how2java;

use how2java;
  
CREATE TABLE category_ (
  id int(11) NOT NULL AUTO_INCREMENT,
  name varchar(30) ,
  password varchar(30),
  score varchar(30),
 PRIMARY KEY (id)
) DEFAULT CHARSET=UTF8;
use how2java;
   
insert into category_ values(null,"category1",1,2);
insert into category_ values(null,"category2",2,1);
insert into category_ values(null,"category3",2,3);
insert into category_ values(null,"category4",3,4);
insert into category_ values(null,"category5",5,6);
第二步 SSM搭建 这一步不写了,可以看我的博客 SSM搭建
Java Poi 在SSM框架中的应用(由Excel 导入到Mysql数据库)_第1张图片

第三步 service层

public List javaPoi()throws IOException;
    public List readXls()throws  IOException;
    public String getValue(HSSFCell hssfCell);


第四步 serviceImpl层

 @Override
    public List javaPoi()throws  IOException{
        List list = readXls();
        System.out.print(list);
        for(int i=0;i < list.size();i++){
            Category category = new Category();
            category = list.get(i);
          categoryMapper.insertSelective(category);
    }
      return list;
    }
     @Override
        public List readXls() throws IOException {
        InputStream is = new FileInputStream("E:\\IDEA\\0706_SSM\\lib\\category.xls");
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
        Category category = null;
        List list = new ArrayList<>();

        for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
            HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
            if (hssfSheet == null) {
                continue;
            }

            for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                HSSFRow hssfRow = hssfSheet.getRow(rowNum);
                if (hssfRow != null) {
                   category = new Category();
                    HSSFCell id = hssfRow.getCell(0);
                    HSSFCell name = hssfRow.getCell(1);
                    HSSFCell password = hssfRow.getCell(2);
                    HSSFCell score = hssfRow.getCell(3);
                    category.setId(Integer.valueOf( getValue(id)));
                    category.setName(getValue(name));
                    category.setPassword(getValue(password));
                    category.setScore(getValue(score));
                    list.add(category);
                }
            }
        }
        return list;
    }
    @Override
    public 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());
        }
    }


第五步 全部代码  

github地址   xls文件存在于lib文件夹里面

https://github.com/boheningmeng/170807_JavaPOISSM


你可能感兴趣的:(javaweb知识,spring)