网站建设费用表格,河北邯郸做网站的公司哪家好,wordpress一定是主页吗,在北京建设教育协会的网站在企业级项目开发中#xff0c;要经常涉及excel文件和程序之间导入导出的业务要求#xff0c;那么今天来讲一讲excel文件导入的实现。java实现对excel的操作有很多种方式#xff0c;例如EasyExcel等#xff0c;今天我们使用的是POI技术实现excel文件的导入。POI技术简介1.P…在企业级项目开发中要经常涉及excel文件和程序之间导入导出的业务要求那么今天来讲一讲excel文件导入的实现。java实现对excel的操作有很多种方式例如EasyExcel等今天我们使用的是POI技术实现excel文件的导入。POI技术简介1.POI概念Apache POI 是用Java编写的免费开源的跨平台的Java APIApache POI提供API给Java程序对Microsoft Office格式档案读和写的功能其中使用最多的就是使用POI操作Excel文件。POI为“Poor Obfuscation Implementation”的首字母缩写意为“简洁版的模糊实现”。官网地址https://poi.apache.org/components/index.html2.POI坐标依赖
dependencygroupIdorg.apache.poi/groupIdartifactIdpoi-ooxml/artifactIdversion4.1.2/version
/dependency3.POI核心API概述3.1 创建工作簿对象Workbook workbooknew XSSFWorkbook(path)3.2 获取execl表中的sheet对象Sheet sheet workbook.getSheetAt(0);3.3 获取excel文件中所有物理数据的有效行数int rows sheet.getPhysicalNumberOfRows()3.4 获取行对象Row row sheet.getRow(i)3.5 获取行中的列对象Cell cellrow.getCell(0)3.6 获取列的字符串类型数据cell.getStringCellValue()3.7 获取列的数字类型字段数据cell.getNumericCellValue()POI技术使用1.需求分析从一个准备好的Excel表格文件中读取学生信息然后将学生的信息通过POI技术导入到数据库的学生表中。2.实现思路以下是具体的实现思路准备excel文件,里面存储若干学生信息包含学生姓名、年龄和手机号创建web项目导入相关jar依赖创建数据库并创建一张学生表使用POI读取文件的学生信息将获取到的学生信息封装到学生对象通过JDBC技术将学生信息保存到学生表。3.案例实现3.1 准备学生信息的excel文件我们先创建一个student.xlsx文件3.2 创建web项目导入jar依赖pom.xml核心依赖如下!-- POI依赖坐标 --
dependencygroupIdorg.apache.poi/groupIdartifactIdpoi-ooxml/artifactIdversion4.1.2/version
/dependency
!-- servlet --
dependencygroupIdjavax.servlet/groupIdartifactIdjavax.servlet-api/artifactIdversion4.0.1/versionscopeprovided/scope
/dependency
!-- 数据库相关 --
dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion5.1.6/version
/dependency
!--c3p0--
dependencygroupIdcom.mchange/groupIdartifactIdc3p0/artifactIdversion0.9.5/version
/dependency
!-- dbutils --
dependencygroupIdcommons-dbutils/groupIdartifactIdcommons-dbutils/artifactIdversion1.7/version
/dependency
/dependencies3.3 创建数据库并创建一张学生表#创建学生数据库
CREATE DATABASE studb;
#创建学生表
CREATE TABLE student (id INT(11) NOT NULL AUTO_INCREMENT,sname VARCHAR(30) DEFAULT NULL,age INT(11) DEFAULT NULL,phone VARCHAR(20) DEFAULT NULL,PRIMARY KEY (id)
)3.4 创建Student实体类package com.qf.pojo;public class Student {private Integer id;private String sname;private Integer age;private String phone;public Student(String sname, Integer age, String phone) {this.sname sname;this.age age;this.phone phone;}public Student(Integer id, String sname, Integer age, String phone) {this.id id;this.sname sname;this.age age;this.phone phone;}public Student() {}public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getSname() {return sname;}public void setSname(String sname) {this.sname sname;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age age;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone phone;}Overridepublic String toString() {return Student{ id id , sname sname \ , age age , phone phone \ };}
}3.5 创建StudentServletWebServlet(/student)
public class StudentServlet extends BaseServlet{private StudentService studentServicenew StudentService();/*POI导入学生信息*/public void poiImport(HttpServletRequest request,HttpServletResponse response){System.out.println(POI导入学生信息);String pathD:\\ssm\\students.xlsx;try {//创建工作表对象Workbook workbooknew XSSFWorkbook(path);//获取目标sheetSheet sheet workbook.getSheetAt(0);//获取sheet数据总行数int rows sheet.getPhysicalNumberOfRows();//获取sheet中所有数据for (int i 1; i rows; i) {//获取当前行对象Row row sheet.getRow(i);String sname row.getCell(0).getStringCellValue();//姓名double value row.getCell(1).getNumericCellValue();//年龄Integer age (int)value;double tel row.getCell(2).getNumericCellValue();//手机号BigDecimal bigDecimalnew BigDecimal(tel);String phone bigDecimal.toString();//将获取的数据封装到学生对象Student studentnew Student(sname,age,phone);//将数据保存数据库表studentService.insertStudent(student);}System.out.println(POI导入学生信息完毕);response.setContentType(text/html;charsetutf-8);response.getWriter().write(POI导入学生信息完毕);} catch (Exception e) {e.printStackTrace();}}
}3.6 创建StudentServicepublic class StudentService {private StudentDao studentDaonew StudentDao();/*增加学生*/public int insertStudent(Student s){return studentDao.addStudent(s);}
}3.7 创建StudentDaopublic class StudentDao extends BaseDaoStudent {/*增加学生*/public int addStudent(Student s){String sqlinsert into student ( sname, age, phone) values (?,?,?);return update(sql, s.getSname(), s.getAge(), s.getPhone());}
}% page contentTypetext/html;charsetUTF-8 languagejava %
html
headtitlePOI使用/title
/head
body
h2POI导入数据/h2
a hrefstudent?keypoiImport导入学生信息/a
/body
/html4.效果图示例首页效果如下图导入成功提示导入成功后学生表:至此我们就实现了POI导入excel文件的操作当然还有一些更复杂的操作在这里没有展开例如导入excel中的部分行、部分列的数据以及导出数据到excel等操作。