昨天领导交给的任务是,用java读取mysql数据库,并将数据写入Excel文件,搞了一天,总算在下班(8:50)前解决了,在b站找的讲解视频,但是没有源文件。中途遇到很多问题,会在文末列出。
先列出文件结构(其实并没有什么结构~)
其中,sqlExcel是主程序,Student是一个实体类。总共分2步:
-1,读取数据库
-2,将数据写入Excel文件
1读取数据库
1.1连接数据库
所用到的方法是JdbcTemplate,代码如下:
//1,读取数据库
//1.1,连接数据库
String url = "jdbc:mysql://localhost:3306/mytest";
String user = "root";
String password = "Qianrui1994";
DriverManagerDataSource source = new DriverManagerDataSource();
source.setUrl(url);
source.setUsername(user);
source.setPassword(password);
JdbcTemplate jdbcTemplate = new JdbcTemplate(source);
System.out.println("数据库连接成功");
1.2 读取数据
新建数组List<Student> studentList存储数据,其中,list是数组,数组中的数据类型是Student类,这是自定义的,在Student.java中定义了,其代码如下:
package com.demo;
public class Student {
private int id;
private String name;
private String password;
private int mark;
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public int getMark() {
return mark;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
public void setMark(int mark) {
this.mark = mark;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", mark=" + mark +
'}';
}
}
读取方式是用query读取,并返回由类Student组成的数组,代码如下:
//1.2,读取数据
//新建数组studentList存储数据
List<Student> studentList = new ArrayList<Student>();
//用query读取,并返回由类Student组成的数组
String sql = "select * from user2";
studentList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Student>(Student.class));
System.out.println("查询成功,结果如下:");
System.out.println(studentList);
2 写入Excel
2.1新建工作簿
2.2新建工作表
2.3写入第一行
2.4写入第二行~最后
//2.1,新建工作簿
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
//2.2,新建工作表
HSSFSheet hssfSheet = hssfWorkbook.createSheet("table1");
//2.3 写入第一行
HSSFRow hssfRow = hssfSheet.createRow(0);
hssfRow.createCell(0).setCellValue("id");
hssfRow.createCell(1).setCellValue("name");
hssfRow.createCell(2).setCellValue("password");
hssfRow.createCell(3).setCellValue("mark");
//2.4 写入第二行~最后
int lastRow = studentList.size();
for (int i=0; i<lastRow; i++){
HSSFRow hssfRow1 = hssfSheet.createRow(i+1);
hssfRow1.createCell(0).setCellValue(studentList.get(i).getId());
hssfRow1.createCell(1).setCellValue(studentList.get(i).getName());
hssfRow1.createCell(2).setCellValue(studentList.get(i).getPassword());
hssfRow1.createCell(3).setCellValue(studentList.get(i).getMark());
}
3 新建输出流,将hssfWorkbook对象写入文件
try{
//新建输出流
FileOutputStream fileOutputStream = new FileOutputStream("/Users/hushuli/Downloads/student4.xsl");
//写入文件
hssfWorkbook.write(fileOutputStream);
//关闭输出流
fileOutputStream.close();
System.out.println("文件写入成功");
}catch (Exception e){
e.printStackTrace();
}
依赖文件很重要,pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>poiTest</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--poi驱动-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>io.github.daosupport</groupId>
<artifactId>jdbcTemplate-support</artifactId>
<version>1.0.1-RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.15.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.15.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.15.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.15.RELEASE</version>
</dependency>
</dependencies>
</project>
遇到的问题,我后续再整理,先睡了...,先把完整代码附上
package com.demo;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
public class sqlExcel {
public static void main(String[] args) {
//1,读取数据库
//1.1,连接数据库
String url = "jdbc:mysql://localhost:3306/mytest";
String user = "root";
String password = "Qianrui1994";
DriverManagerDataSource source = new DriverManagerDataSource();
source.setUrl(url);
source.setUsername(user);
source.setPassword(password);
JdbcTemplate jdbcTemplate = new JdbcTemplate(source);
System.out.println("数据库连接成功");
//1.2,读取数据
//新建数组studentList存储数据
List<Student> studentList = new ArrayList<Student>();
//用query读取,并返回由类Student组成的数组
String sql = "select * from user2";
studentList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Student>(Student.class));
System.out.println("查询成功,结果如下:");
System.out.println(studentList);
//2,写入Excel
//2.1,新建工作簿
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
//2.2,新建工作表
HSSFSheet hssfSheet = hssfWorkbook.createSheet("table1");
//2.3 写入第一行
HSSFRow hssfRow = hssfSheet.createRow(0);
hssfRow.createCell(0).setCellValue("id");
hssfRow.createCell(1).setCellValue("name");
hssfRow.createCell(2).setCellValue("password");
hssfRow.createCell(3).setCellValue("mark");
//2.4 写入第二行~最后
int lastRow = studentList.size();
for (int i=0; i<lastRow; i++){
HSSFRow hssfRow1 = hssfSheet.createRow(i+1);
hssfRow1.createCell(0).setCellValue(studentList.get(i).getId());
hssfRow1.createCell(1).setCellValue(studentList.get(i).getName());
hssfRow1.createCell(2).setCellValue(studentList.get(i).getPassword());
hssfRow1.createCell(3).setCellValue(studentList.get(i).getMark());
}
//3 新建输出流,将hssfWorkbook对象写入文件
try{
//新建输出流
FileOutputStream fileOutputStream = new FileOutputStream("/Users/hushuli/Downloads/student4.xsl");
//写入文件
hssfWorkbook.write(fileOutputStream);
//关闭输出流
fileOutputStream.close();
System.out.println("文件写入成功");
}catch (Exception e){
e.printStackTrace();
}
}
}