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;
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);
exportToExcel(users,"f:/users.xls");
}
}
public static void exportToExcel(List<User> users,String exportPath){
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet userSheet = workbook.createSheet("用户数据");
userSheet.setDefaultColumnWidth(20);
userSheet.setDefaultRowHeight((short) (20*20));
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);
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 {
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;
}
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;
}
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结果