今天需要实现将数据库表中的数据可以导出到excel中,方便后台管理人员查看和统计数据使用,先写一个简单的例子,后面会对这个例子进行封装,方便以后其他地方使用,下面开始:
1.首先下载poi.jar包,该jar包提供了实现导出excel表的功能
下载地址如下:
2、今天先写一个简单的例子,就没有使用数据库,新建一个Person类,代码如下;
package com._test.excel;
public class Person {
private int id;
private String name;
private int age;
public Person(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3、实现导出数据到excel表一共分为六步,分别是:
第一步、创建一个workbook对象,对应一个Excel文件,代码如下:
HSSFWorkbook wb = new HSSFWorkbook();
第二步、在workbook中添加一个sheet,对应Excel文件中的sheet,代码如下:
// 这个sheetname随便起
HSSFSheet sheet = wb.createSheet("sheet_test");
第三步,在sheet中添加表头第0行,代码如下:
HSSFRow row = sheet.createRow((int) 0);
第四步,创建单元格,并设置值表头 设置表头居中,代码如下:
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
// 设置表头行各个列的名字,
setSheetHeader(row, style);
/**
* 设置表头行各个列的名字
*
* @param row
* @param style
*/
private void setSheetHeader(HSSFRow row, HSSFCellStyle style) {
HSSFCell cell = row.createCell((short) 0);
cell.setCellStyle(style);
// 这个输出编码必须设置,否则汉字会乱码
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("学号");
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("年龄");
cell.setCellStyle(style);
}
第五步,获得要导出到excel表的数据 实际应用中这些数据从数据库得到,代码如下:
List<Person> list = getStudentDatas();
// 添加导出数据到表中
insertDatasToSheet(sheet, list);
/**
* 添加导出数据到表中
*
* @param sheet
* @param list
*/
private void insertDatasToSheet(HSSFSheet sheet, List<Person> list) {
HSSFCell cell = null;
HSSFRow row = null;
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow((int) i + 1);
Person stu = (Person) list.get(i);
// 创建单元格,并设置各个列中实际数据的值
cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue((double) stu.getId());
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(stu.getName());
cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue((double) stu.getAge());
}
}
第六步,将excel文件存到指定位置,代码如下:
/**
* 将excel文件存到指定位置
*
* @param filePath
* @param wb
*/
private void writeExcelToDisk(String filePath, HSSFWorkbook wb) {
try {
FileOutputStream fout = new FileOutputStream(filePath);
wb.write(fout);
fout.close();
System.out.println("excel已经导出到:" + filePath);
} catch (Exception e) {
e.printStackTrace();
}
}
4、新建一个导出数据到excel表的功能类,代码如下:
package com._test.excel;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* 实现导出数据到excel表的功能类
*/
public class CreateExcelToDisk {
/**
* 获得数据集合
*
* @return
*/
private static List<Person> getStudentDatas() {
List<Person> list = new ArrayList<Person>();
for (int i = 0; i < 5; i++) {
Person stu = new Person(i, "学生_" + i, 10 + i);
list.add(stu);
}
return list;
}
/**
* 导出excel文件
*/
public void exprotExcel(String filePath) {
// 第一步、创建一个workbook对象,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步、在workbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("sheet_test");// 这个sheetname随便起
// 第三步,在sheet中添加表头第0行
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
// 设置表头行各个列的名字,
setSheetHeader(row, style);
// 第五步,获得要导出到excel表的数据 实际应用中这些数据从数据库得到,
List<Person> list = getStudentDatas();
// 添加导出数据到表中
insertDatasToSheet(sheet, list);
// 第六步,将excel文件存到指定位置
writeExcelToDisk(filePath, wb);
}
/**
* 将excel文件存到指定位置
*
* @param filePath
* @param wb
*/
private void writeExcelToDisk(String filePath, HSSFWorkbook wb) {
try {
FileOutputStream fout = new FileOutputStream(filePath);
wb.write(fout);
fout.close();
System.out.println("excel已经导出到:" + filePath);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 添加导出数据到表中
*
* @param sheet
* @param list
*/
private void insertDatasToSheet(HSSFSheet sheet, List<Person> list) {
HSSFCell cell = null;
HSSFRow row = null;
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow((int) i + 1);
Person stu = (Person) list.get(i);
// 创建单元格,并设置各个列中实际数据的值
cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue((double) stu.getId());
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(stu.getName());
cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue((double) stu.getAge());
}
}
/**
* 设置表头行各个列的名字
*
* @param row
* @param style
*/
private void setSheetHeader(HSSFRow row, HSSFCellStyle style) {
HSSFCell cell = row.createCell((short) 0);
cell.setCellStyle(style);
// 这个输出编码必须设置,否则汉字会乱码
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("学号");
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("年龄");
cell.setCellStyle(style);
}
public static void main(String[] args) {
String filePath = "E:/students.xls";
CreateExcelToDisk excel = new CreateExcelToDisk();
excel.exprotExcel(filePath);
}
}
5、导出的excel表如下图所示: