Java使用POI实现Excel的导出和导入
- POI介绍
- maven导入POI依赖
- POI相关类
- 创建User实体类
- 导出Excel代码
- 单元格样式工具类代码
- 导出Excel结果
POI介绍
Apache POI是Apache软件基金会的开源项目,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
HSSF提供读写Microsoft Excel XLS格式档案的功能。也就是以HSSF开头的类和接口都是操作Excel的。
maven导入POI依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
POI相关类
HSSFWorkbook:工作区,相当于一个Excel文档,一个HSSFWorkbook对象代表一个Excel文档。
HSSFSheet:sheet表,Excel文档中的sheet,一个HSSFSheet对象代表一个sheet表。
HSSFRow:sheet表中的行,一个HSSFRow对象代表一行。
HSSFCell:行中的单元格,一个HSSFCell对象代表一个单元格。
HSSFCellStyle:一个单元格的样式类。
HSSFFont:一个字体的样式类
创建User实体类
package cn.xxxq.pojo;
import java.util.Date;
/**
* 用户实体类
*/
public class User {
private Integer id;
private String name;
private String address;
private String sex;
private Date birth;
public User() {}
public User(Integer id, String name, String address, String sex, Date birth) {
this.id = id;
this.name = name;
this.address = address;
this.sex = sex;
this.birth = birth;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSex() { return sex;}
public void setSex(String sex) { this.sex = sex; }
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
}
导出Excel代码
package cn.xxxq.poi;
import cn.xxxq.pojo.User;
import cn.xxxq.utils.CellStyleUtil;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 手动创建用户集合数据,使用POI将其导出到Excel。
*/
public class Java_Excel_Test {
public static void main(String[] args) {
List<User> users = new ArrayList<User>();
for (int i = 0; i < 10; i++) {
User user = new User(i, "小白" + i, "珠海", i % 2==0?"女":"男", new Date());
users.add(user);
//将数据导出到Excel中
exportToExcel(users,"f:/users.xls");
}
}
public static void exportToExcel(List<User> users,String exportPath){
//1.创建工作区
HSSFWorkbook workbook = new HSSFWorkbook();
//2.在工作区中创建sheet
// workbook.createSheet();创建默认名字的sheet:sheet1.sheet2...
HSSFSheet userSheet = workbook.createSheet("用户数据");
//3.sheet的相关设置
//设置默认列宽
userSheet.setDefaultColumnWidth(20);
//设置默认行高
userSheet.setDefaultRowHeight((short) (20*20));
//合并第一行和第二行,合并5个单元格
CellRangeAddress firstRow_region = new CellRangeAddress(0, 0, 0, 4);
userSheet.addMergedRegion(firstRow_region);
CellRangeAddress secondRow_region = new CellRangeAddress(1, 1, 0, 4);
userSheet.addMergedRegion(secondRow_region);
//sheet由行和列组成,0代表第一行
int row = 0;
//创建第一行
HSSFRow fristRow = userSheet.createRow(row);
//创建第一行的第一个单元格
HSSFCell row1_firstCell = fristRow.createCell(0);
//为第一行的第一个单元格赋值
row1_firstCell.setCellValue("用户数据");
//设置标题样式
row1_firstCell.setCellStyle(CellStyleUtil.createTitleStyle(workbook));
row++;
//创建第二行
HSSFRow secondRow = userSheet.createRow(row);
//创建第二行的第一个单元格
HSSFCell row2_firstCell = secondRow.createCell(0);
row2_firstCell.setCellValue("总条数:"+users.size()+",导出时间:"+new Date().toLocaleString());
设置副标题样式
row2_firstCell.setCellStyle(CellStyleUtil.createSubTitleStyle(workbook));
row++;
String[] titles = {"用户ID", "用户名", "用户地址", "性别", "入职时间"};
//创建第三行
HSSFRow threeRow = userSheet.createRow(row);
for (int i = 0; i < titles.length; i++) {
HSSFCell row3_cell = threeRow.createCell(i);
row3_cell.setCellStyle(CellStyleUtil.createDataGridTitleStyle(workbook));
row3_cell.setCellValue(titles[i]);
}
for (int i = 0; i < users.size(); i++) {
User user = users.get(i);
row++;
HSSFRow nextRow = userSheet.createRow(row);
HSSFCell cell1 = nextRow.createCell(0);
cell1.setCellValue(user.getId());
cell1.setCellStyle(CellStyleUtil.createBaseCellStyle(workbook));
HSSFCell cell2 = nextRow.createCell(1);
cell2.setCellValue(user.getName());
cell2.setCellStyle(CellStyleUtil.createBaseCellStyle(workbook));
HSSFCell cell3 = nextRow.createCell(2);
cell3.setCellValue(user.getAddress());
cell3.setCellStyle(CellStyleUtil.createBaseCellStyle(workbook));
HSSFCell cell4 = nextRow.createCell(3);
cell4.setCellValue(user.getSex());
cell4.setCellStyle(CellStyleUtil.createBaseCellStyle(workbook));
HSSFCell cell5 = nextRow.createCell(4);
cell5.setCellValue(user.getBirth().toLocaleString());
cell5.setCellStyle(CellStyleUtil.createBaseCellStyle(workbook));
}
try {
//导出到指定路径
workbook.write(new File(exportPath));
} catch (IOException e) {
e.printStackTrace();
}
}
}
单元格样式工具类代码
package cn.xxxq.utils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
/**
* 单元格样式工具类
*/
public class CellStyleUtil {
/**
* 创建基础样式:水平垂直居中
* @param workbook
* @return
*/
public static HSSFCellStyle createBaseCellStyle(HSSFWorkbook workbook){
HSSFCellStyle cellStyle = workbook.createCellStyle();
//设置水平居中
cellStyle.setAlignment(HorizontalAlignment.CENTER);
//设置垂直居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
return cellStyle;
}
/**
* 创建标题样式
*/
public static HSSFCellStyle createTitleStyle(HSSFWorkbook workbook) {
//创建基础样式的单元格
HSSFCellStyle cellStyle = createBaseCellStyle(workbook);
//设置字体及相关样式
HSSFFont font=workbook.createFont();
font.setFontName("华文彩云");//字体
font.setBold(true);//加粗
font.setFontHeightInPoints((short)35); //字体大小
font.setColor(HSSFColor.HSSFColorPredefined.RED.getIndex());//颜色,红色
cellStyle.setFont(font);
return cellStyle;
}
/**
* 创建副标题样式
* @param workbook
* @return
*/
public static HSSFCellStyle createSubTitleStyle(HSSFWorkbook workbook){
HSSFCellStyle cellStyle = createBaseCellStyle(workbook);
//设置字体及相关样式
HSSFFont font = workbook.createFont();
font.setFontName("华文行楷");//字体
font.setBold(true);//加粗
font.setItalic(true);//斜体
font.setFontHeightInPoints((short) 25);//字体大小
font.setColor(HSSFColor.HSSFColorPredefined.DARK_YELLOW.getIndex());//字体颜色,黄色
cellStyle.setFont(font);//设置单元格的字体样式
return cellStyle;
}
/**
* 创建数据表格表头样式
* @param workbook
* @return
*/
public static HSSFCellStyle createDataGridTitleStyle(HSSFWorkbook workbook){
HSSFCellStyle cellStyle = createBaseCellStyle(workbook);
//设置字体及相关样式
HSSFFont font = workbook.createFont();
font.setFontName("黑体");//字体
font.setBold(true);//加粗
font.setFontHeightInPoints((short) 20);//字体大小
font.setColor(HSSFColor.HSSFColorPredefined.DARK_BLUE.getIndex());//字体颜色,蓝色
cellStyle.setFont(font);
return cellStyle;
}
}
导出Excel结果