如何优雅的用POI导入Excel文件
在企業(yè)級(jí)項(xiàng)目開(kāi)發(fā)中,要經(jīng)常涉及excel文件和程序之間導(dǎo)入導(dǎo)出的業(yè)務(wù)要求,那么今天來(lái)講一講excel文件導(dǎo)入的實(shí)現(xiàn)。java實(shí)現(xiàn)對(duì)excel的操作有很多種方式,例如EasyExcel等,今天我們使用的是POI技術(shù)實(shí)現(xiàn)excel文件的導(dǎo)入。
POI技術(shù)簡(jiǎn)介
1.POI概念
Apache POI 是用Java編寫的免費(fèi)開(kāi)源的跨平臺(tái)的Java API,Apache POI提供API給Java程序?qū)icrosoft Office格式檔案讀和寫的功能,其中使用最多的就是使用POI操作Excel文件。POI為“Poor Obfuscation Implementation”的首字母縮寫,意為“簡(jiǎn)潔版的模糊實(shí)現(xiàn)”。
官網(wǎng)地址:
https://poi.apache.org/components/index.html
2.POI坐標(biāo)依賴
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version> </dependency>3.POI核心API概述
3.1 創(chuàng)建工作簿對(duì)象
Workbook workbook=new XSSFWorkbook(path)3.2 獲取execl表中的sheet對(duì)象
Sheet sheet = workbook.getSheetAt(0);3.3 獲取excel文件中所有物理數(shù)據(jù)的有效行數(shù)
int rows = sheet.getPhysicalNumberOfRows()3.4 獲取行對(duì)象
Row row =sheet.getRow(i)3.5 獲取行中的列對(duì)象
Cell cell=row.getCell(0)3.6 獲取列的字符串類型數(shù)據(jù)
cell.getStringCellValue()3.7 獲取列的數(shù)字類型字段數(shù)據(jù)
cell.getNumericCellValue()POI技術(shù)使用
1.需求分析
從一個(gè)準(zhǔn)備好的Excel表格文件中讀取學(xué)生信息,然后將學(xué)生的信息通過(guò)POI技術(shù)導(dǎo)入到數(shù)據(jù)庫(kù)的學(xué)生表中。
2.實(shí)現(xiàn)思路
以下是具體的實(shí)現(xiàn)思路:
準(zhǔn)備excel文件,里面存儲(chǔ)若干學(xué)生信息:包含學(xué)生姓名、年齡和手機(jī)號(hào);
創(chuàng)建web項(xiàng)目,導(dǎo)入相關(guān)jar依賴;
創(chuàng)建數(shù)據(jù)庫(kù)并創(chuàng)建一張學(xué)生表;
使用POI讀取文件的學(xué)生信息;
將獲取到的學(xué)生信息封裝到學(xué)生對(duì)象;
通過(guò)JDBC技術(shù)將學(xué)生信息保存到學(xué)生表。
3.案例實(shí)現(xiàn)
3.1 準(zhǔn)備學(xué)生信息的excel文件
我們先創(chuàng)建一個(gè)student.xlsx文件
3.2 創(chuàng)建web項(xiàng)目,導(dǎo)入jar依賴
pom.xml核心依賴如下:
<!-- POI依賴坐標(biāo) --> <dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version> </dependency> <!-- servlet --> <dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version><scope>provided</scope> </dependency> <!-- 數(shù)據(jù)庫(kù)相關(guān) --> <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version> </dependency> <!--c3p0--> <dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5</version> </dependency> <!-- dbutils --> <dependency><groupId>commons-dbutils</groupId><artifactId>commons-dbutils</artifactId><version>1.7</version> </dependency> </dependencies>3.3 創(chuàng)建數(shù)據(jù)庫(kù)并創(chuàng)建一張學(xué)生表
#創(chuàng)建學(xué)生數(shù)據(jù)庫(kù) CREATE DATABASE studb; #創(chuàng)建學(xué)生表 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 創(chuàng)建Student實(shí)體類
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 創(chuàng)建StudentServlet
@WebServlet("/student") public class StudentServlet extends BaseServlet{private StudentService studentService=new StudentService();/*POI導(dǎo)入學(xué)生信息*/public void poiImport(HttpServletRequest request,HttpServletResponse response){System.out.println("POI導(dǎo)入學(xué)生信息");String path="D:\\ssm\\students.xlsx";try {//創(chuàng)建工作表對(duì)象Workbook workbook=new XSSFWorkbook(path);//獲取目標(biāo)sheetSheet sheet = workbook.getSheetAt(0);//獲取sheet數(shù)據(jù)總行數(shù)int rows = sheet.getPhysicalNumberOfRows();//獲取sheet中所有數(shù)據(jù)for (int i = 1; i < rows; i++) {//獲取當(dāng)前行對(duì)象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();//手機(jī)號(hào)BigDecimal bigDecimal=new BigDecimal(tel);String phone = bigDecimal.toString();//將獲取的數(shù)據(jù)封裝到學(xué)生對(duì)象Student student=new Student(sname,age,phone);//將數(shù)據(jù)保存數(shù)據(jù)庫(kù)表studentService.insertStudent(student);}System.out.println("POI導(dǎo)入學(xué)生信息完畢!");response.setContentType("text/html;charset=utf-8");response.getWriter().write("POI導(dǎo)入學(xué)生信息完畢!");} catch (Exception e) {e.printStackTrace();}} }3.6 創(chuàng)建StudentService
public class StudentService {private StudentDao studentDao=new StudentDao();/*增加學(xué)生*/public int insertStudent(Student s){return studentDao.addStudent(s);} }3.7 創(chuàng)建StudentDao
public class StudentDao extends BaseDao<Student> {/*增加學(xué)生*/public int addStudent(Student s){String sql="insert into `student` ( `sname`, `age`, `phone`) values (?,?,?)";return update(sql, s.getSname(), s.getAge(), s.getPhone());} } <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>POI使用</title> </head> <body> <h2>POI導(dǎo)入數(shù)據(jù)</h2> <a href="student?key=poiImport">導(dǎo)入學(xué)生信息</a> </body> </html>4.效果圖示例
首頁(yè)效果如下圖:
導(dǎo)入成功提示:
導(dǎo)入成功后學(xué)生表:
至此,我們就實(shí)現(xiàn)了POI導(dǎo)入excel文件的操作,當(dāng)然還有一些更復(fù)雜的操作在這里沒(méi)有展開(kāi),例如導(dǎo)入excel中的部分行、部分列的數(shù)據(jù),以及導(dǎo)出數(shù)據(jù)到excel等操作。
總結(jié)
以上是生活随笔為你收集整理的如何优雅的用POI导入Excel文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: easy poi 双行标题导出
- 下一篇: iOS学习之多线程