1.导入依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!--xlsx(07)-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
2.poi的应用类
HSSFWorkBook execl的文档对象
HSSFSheet excel的表单
HSSFRow excel的行
HSSFCell excel的单元格
HSSFFont excel的字体
HSSFCellStyle excel的单元格样式
HSSFName 名称
HSSFDataFormat 日期格式
HSSFHeader sheet头
HSSFFotter sheet尾
HSSFDateUtil 日期
HSSFPrintSetup 打印
HSSFErrorConstants 错误信息表
3.使用方式
//创建HSSFWorkbook对象(excel的文档对象)
HSSFWorkbook workbook = new HSSFWorkbook();
//建立新的sheet对象(excel的表单)title="test"
HSSFSheet sheet = workbook.createSheet(title);
// 产生表格标题行(0表示创建的行是第一行,参数为行索引)
HSSFRow rowm = sheet.createRow(0);
//创建单元格(excel的单元格,参数为列索引)
HSSFCell cellTitle = rowm.createCell(0);
使用顺序:
HSSFWork–>HSSFSheet–>HSSFRow–>HSSFCell
都是由前一个来创建当前的
//单元格合并
//CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol)
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length - 1)));
//创建表的列 头
// 定义所需列数
int columnNum = rowName.length;
HSSFRow rowRowName = sheet.createRow(2); //参数2代表列头在第二行
//将列头设置到sheet的单元格中
for (int i = 0; i< columnNum; i++) {
HSSFCell cellRowName = rowRowName.createCell(i);
cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING);
HSSFRichTextString text = new HSSFRichTextString(rowName[i]);
System.out.println("text = "+ text);
cellRowName.setCellValue(text);
cellRowName.setCellStyle(columnTopStyle);
}
插入表内容
for (int i = 0; i < dataList.size(); i++) {
//Enlist 代表创建的实体类
Enlist enlist = dataList.get(i);// 遍历每个对象
//i+3是考虑到表头和列头的存在
HSSFRow row = sheet.createRow(i + 3);// 创建所需的行数
row.createCell(0,HSSFCell.CELL_TYPE_NUMERIC).setCellValue(enlist.getId());
row.createCell(1,HSSFCell.CELL_TYPE_STRING).setCellValue(enlist.getUsername());
row.createCell(2,HSSFCell.CELL_TYPE_STRING).setCellValue(enlist.getPhone());
row.createCell(3,HSSFCell.CELL_TYPE_NUMERIC).setCellValue(enlist.getAge());
row.createCell(4,HSSFCell.CELL_TYPE_STRING).setCellValue(enlist.getSex());
}
表的导出:
String filename = "E:\\Excel-.xls";
FileOutputStream output = new FileOutputStream(filename);
HSSFWorkBook book = new new HSSFWorkbook();
book.write(output);
output.flush();
output.close();
结果展示
源码:
String filename = "E:\\code\\Excel-.xls";
String title = "test";
FileOutputStream output = null;
//数据库获取数据
List<Enlist> list = service.selectAll();
String rowName[] = {"id","姓名","电话","年龄","性别"};
try {
output=new FileOutputStream(filename);
ExportExcel excel = new ExportExcel(title,rowName,list);
excel.export(output);
}catch (Exception e){
e.printStackTrace();
}
output.flush();
output.close();
实体类
@Data
@Accessors(chain = true)
@TableName("enlist")
public class Enlist {
@TableId
private Integer id;
private String username;
private String phone;
private Integer age;
private String sex;
public Enlist(String username, String phone, Integer age,String sex) {
this.username = username;
this.phone = phone;
this.age = age;
this.sex = sex;
}
public Enlist() {
}
实现类:
public class ExportExcel {
// 显示的导出表的标题
private String title;
// 导出表的列名
private String[] rowName;
private List<Enlist> dataList;
// 构造函数,传入要导出的数据
public ExportExcel(String title, String[] rowName, List<Enlist> dataList) {
this.dataList = dataList;
this.rowName = rowName;
this.title = title;
}
// 导出数据
public void export(FileOutputStream out) throws Exception {
try {
//创建一个表
//创建HSSFWorkbook对象(excel的文档对象)
HSSFWorkbook workbook = new HSSFWorkbook();
//建立新的sheet对象(excel的表单)
HSSFSheet sheet = workbook.createSheet(title);
//在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
// 产生表格标题行
HSSFRow rowm = sheet.createRow(0);
//创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
HSSFCell cellTitle = rowm.createCell(0);
//sheet样式定义【】
HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);
HSSFCellStyle style = this.getStyle(workbook);
//单元格合并 CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol)
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length - 1)));
cellTitle.setCellStyle(columnTopStyle);
cellTitle.setCellValue(title);
// 定义所需列数
int columnNum = rowName.length;
HSSFRow rowRowName = sheet.createRow(2);
//创建列头
//将列头设置到sheet的单元格中
for (int i = 0; i< columnNum; i++) {
HSSFCell cellRowName = rowRowName.createCell(i);
cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING);
HSSFRichTextString text = new HSSFRichTextString(rowName[i]);
System.out.println("text = "+ text);
cellRowName.setCellValue(text);
cellRowName.setCellStyle(columnTopStyle);
}
// 将查询到的数据设置到sheet对应的单元格中
for (int i = 0; i < dataList.size(); i++) {
Enlist enlist = dataList.get(i);// 遍历每个对象
HSSFRow row = sheet.createRow(i + 3);// 创建所需的行数
System.out.println("enlist = "+enlist);
// HSSFCell cell = null;
row.createCell(0,HSSFCell.CELL_TYPE_NUMERIC).setCellValue(enlist.getId());
row.createCell(1,HSSFCell.CELL_TYPE_STRING).setCellValue(enlist.getUsername());
row.createCell(2,HSSFCell.CELL_TYPE_STRING).setCellValue(enlist.getPhone());
row.createCell(3,HSSFCell.CELL_TYPE_NUMERIC).setCellValue(enlist.getAge());
row.createCell(4,HSSFCell.CELL_TYPE_STRING).setCellValue(enlist.getSex());
}
// 让列宽随着导出的列长自动适应
for (int colNum = 0; colNum < columnNum; colNum++) {
int columnWidth = sheet.getColumnWidth(colNum) / 256;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
HSSFRow currentRow;
if (sheet.getRow(rowNum) == null) {
currentRow = sheet.createRow(rowNum);
} else {
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(colNum) != null) {
HSSFCell currentCell = currentRow.getCell(colNum);
if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
int length = currentCell.getStringCellValue().getBytes().length;
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
if (colNum == 0) {
sheet.setColumnWidth(colNum, (columnWidth - 2) * 256);
} else {
sheet.setColumnWidth(colNum, (columnWidth + 4) * 256);
}
}
if (workbook != null) {
try {
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 列头单元格样式
*/
public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {
// 设置字体
HSSFFont font = workbook.createFont();
// 设置字体大小
font.setFontHeightInPoints((short) 11);
// 字体加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 设置字体名字
font.setFontName("Courier New");
// 设置样式
HSSFCellStyle style = workbook.createCellStyle();
// 设置低边框
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
// 设置低边框颜色
style.setBottomBorderColor(HSSFColor.BLACK.index);
// 设置右边框
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
// 设置顶边框
style.setTopBorderColor(HSSFColor.BLACK.index);
// 设置顶边框颜色
style.setTopBorderColor(HSSFColor.BLACK.index);
// 在样式中应用设置的字体
style.setFont(font);
// 设置自动换行
style.setWrapText(false);
// 设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}
public HSSFCellStyle getStyle(HSSFWorkbook workbook) {
// 设置字体
HSSFFont font = workbook.createFont();
// 设置字体大小
font.setFontHeightInPoints((short) 10);
// 字体加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 设置字体名字
font.setFontName("Courier New");
// 设置样式;
HSSFCellStyle style = workbook.createCellStyle();
// 设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
// 设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
// 设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
// 设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
// 设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
// 设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
// 设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
// 设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
// 在样式用应用设置的字体;
style.setFont(font);
// 设置自动换行;
style.setWrapText(false);
// 设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
return style;
}
}