如何优雅的用POI导入Excel文件
在企業級項目開發中,要經常涉及excel文件和程序之間導入導出的業務要求,那么今天來講一講excel文件導入的實現。java實現對excel的操作有很多種方式,例如EasyExcel等,今天我們使用的是POI技術實現excel文件的導入。
POI技術簡介
1.POI概念
Apache POI 是用Java編寫的免費開源的跨平臺的Java API,Apache POI提供API給Java程序對Microsoft Office格式檔案讀和寫的功能,其中使用最多的就是使用POI操作Excel文件。POI為“Poor Obfuscation Implementation”的首字母縮寫,意為“簡潔版的模糊實現”。
官網地址:
https://poi.apache.org/components/index.html
2.POI坐標依賴
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version> </dependency>3.POI核心API概述
3.1 創建工作簿對象
Workbook workbook=new 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 cell=row.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依賴坐標 --> <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> <!-- 數據庫相關 --> <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 創建數據庫并創建一張學生表
#創建學生數據庫 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 創建StudentServlet
@WebServlet("/student") public class StudentServlet extends BaseServlet{private StudentService studentService=new StudentService();/*POI導入學生信息*/public void poiImport(HttpServletRequest request,HttpServletResponse response){System.out.println("POI導入學生信息");String path="D:\\ssm\\students.xlsx";try {//創建工作表對象Workbook workbook=new 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 bigDecimal=new BigDecimal(tel);String phone = bigDecimal.toString();//將獲取的數據封裝到學生對象Student student=new Student(sname,age,phone);//將數據保存數據庫表studentService.insertStudent(student);}System.out.println("POI導入學生信息完畢!");response.setContentType("text/html;charset=utf-8");response.getWriter().write("POI導入學生信息完畢!");} catch (Exception e) {e.printStackTrace();}} }3.6 創建StudentService
public class StudentService {private StudentDao studentDao=new StudentDao();/*增加學生*/public int insertStudent(Student s){return studentDao.addStudent(s);} }3.7 創建StudentDao
public class StudentDao extends BaseDao<Student> {/*增加學生*/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導入數據</h2> <a href="student?key=poiImport">導入學生信息</a> </body> </html>4.效果圖示例
首頁效果如下圖:
導入成功提示:
導入成功后學生表:
至此,我們就實現了POI導入excel文件的操作,當然還有一些更復雜的操作在這里沒有展開,例如導入excel中的部分行、部分列的數據,以及導出數據到excel等操作。
總結
以上是生活随笔為你收集整理的如何优雅的用POI导入Excel文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: easy poi 双行标题导出
- 下一篇: iOS学习之多线程