Java中绘制Excel表格导出操作(复杂表头)
poi常用类说明
类名 | 说明 |
---|---|
HSSFWorkbook | Excel的文档对象 |
HSSFSheet | Excel的表单 |
HSSFRow | Excel的行 |
HSSFCell | Excel的格子单元 |
HSSFFont | Excel字体 |
HSSFDataFormat | 格子单元的日期格式 |
HSSFHeader | Excel文档Sheet的页眉 |
HSSFFooter | Excel文档Sheet的页脚 |
HSSFCellStyle | 格子单元样式 |
HSSFDateUtil | 日期 |
HSSFPrintSetup | 打印 |
HSSFErrorConstants | 错误信息表 |
导入依赖
<!--导出excel表格-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.1</version>
</dependency>
1、创建一个空的Excel表格输出
@RequestMapping("/exportExcel")
public void exportExcel(HttpServletResponse response, HttpServletRequest request) throws Exception {
//创建工作薄对象
HSSFWorkbook workbook=new HSSFWorkbook();//这里也可以设置sheet的Name
//创建工作表对象
HSSFSheet sheet = workbook.createSheet();
//创建工作表的行
HSSFRow row = sheet.createRow(0);//设置第一行,从零开始
row.createCell(2).setCellValue("aaaaaaaaaaaa");//第一行第三列为aaaaaaaaaaaa
SimpleDateFormat adf=new SimpleDateFormat("yyyy-MM-dd");
row.createCell(0).setCellValue(adf.format(new Date()));//第一行第一列为日期
workbook.setSheetName(0,"sheet的Name");//设置sheet的Name
String fileName = "Excel表格.xls";// 文件名
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename="
+ URLEncoder.encode(fileName, "UTF-8"));
OutputStream out = new BufferedOutputStream(response.getOutputStream());
//向外输出excel表格
workbook.write(out);
out.flush();
out.close();
}
2、Excel单元格格式设置操作
/**
* 导出新建的Excel表格(设置单元格的格式)
*
* @param response
* @param request
* @throws Exception
*/
@RequestMapping("/exportExcel")
public void exportExcel(HttpServletResponse response, HttpServletRequest request) throws Exception {
//创建Excel工作簿对象
HSSFWorkbook workbook = new HSSFWorkbook();
//创建Excel工作表对象
HSSFSheet sheet = workbook.createSheet("table1");
//创建行的单元格,从0开始
HSSFRow row = sheet.createRow(0);
//创建单元格
HSSFCell cell = row.createCell(0);
//给单元格赋值
cell.setCellValue(new Date());
//设置单元格样式
HSSFCellStyle style = workbook.createCellStyle();
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
cell.setCellStyle(style);
//设置保留2位小数--使用Excel内嵌的格式
HSSFCell cell1 = row.createCell(1);
cell1.setCellValue(12.3456789);
style=workbook.createCellStyle();
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
cell1.setCellStyle(style);
//设置货币格式--使用自定义的格式
HSSFCell cell2 = row.createCell(2);
cell2.setCellValue(12345.6789);
style=workbook.createCellStyle();
style.setDataFormat(workbook.createDataFormat().getFormat("¥#,##0"));
cell2.setCellStyle(style);
//设置百分比格式--使用自定义的格式
HSSFCell cell3 = row.createCell(3);
cell3.setCellValue(0.123456789);
style=workbook.createCellStyle();
style.setDataFormat(workbook.createDataFormat().getFormat("0.00%"));
cell3.setCellStyle(style);
//设置中文大写格式--使用自定义的格式
HSSFCell cell4 = row.createCell(4);
cell4.setCellValue(123456);
style=workbook.createCellStyle();
style.setDataFormat(workbook.createDataFormat().getFormat("[DbNum2][$-804]0"));
cell4.setCellStyle(style);
//设置科学计数法格式--使用自定义的格式
HSSFCell cell5 = row.createCell(5);
cell5.setCellValue(456789);
style=workbook.createCellStyle();
style.setDataFormat(workbook.createDataFormat().getFormat("0.00E+00"));
cell5.setCellStyle(style);
String fileName = "Excel表格.xls";// 文件名
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename="
+ URLEncoder.encode(fileName, "UTF-8"));
OutputStream out = new BufferedOutputStream(response.getOutputStream());
//向外输出excel表格
workbook.write(out);
out.flush();
out.close();
}
HSSFDataFormat.getFormat和HSSFDataFormat.getBuiltinFormat的区别:
当使用Excel内嵌的(或者说预定义)的格式时,直接用HSSFDataFormat.getBuiltinFormat静态方法即可。当使用自己定义的格式时,必须先调用HSSFWorkbook.createDataFormat(),因为这时在底层会先找有没有匹配的内嵌FormatRecord,如果没有就会新建一个FormatRecord,所以必须先调用这个方法,然后你就可以用获得的HSSFDataFormat实例的getFormat方法了,当然相对而言这种方式比较麻烦,所以内嵌格式还是用HSSFDataFormat.getBuiltinFormat静态方法更加直接一些。
3、合并单元格操作
/**
* 导出新建的Excel表格(合并单元格操作)
*
* @param response
* @param request
* @throws Exception
*/
@RequestMapping("/exportExcel")
public void exportExcel(HttpServletResponse response, HttpServletRequest request) throws Exception {
//创建一个Excel表格文件
HSSFWorkbook workbook = new HSSFWorkbook();
//在Excel表中建一个工作表
HSSFSheet sheet = workbook.createSheet("工作表");
//在工作表中创建一行
HSSFRow row = sheet.createRow(0);
//在一行中创建一列,从0开始
HSSFCell cell = row.createCell(0);
//合并列
cell.setCellValue("合并列");
CellRangeAddress region = new CellRangeAddress(0, 0, 0, 5);
sheet.addMergedRegion(region);
HSSFCell cell1 = row.createCell(6);
//合并行
cell1.setCellValue("合并行");
region = new CellRangeAddress(0, 5, 6, 6);
sheet.addMergedRegion(region);
//设置Excel表的名称
String fileName = "单元格合并Excel.xls";
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename="
+ URLEncoder.encode(fileName, "UTF-8"));
//将输出流转换成高效缓存输出流
OutputStream out = new BufferedOutputStream(response.getOutputStream());
//向外输出excel表格
workbook.write(out);
out.flush();
out.close();
}
CellRangeAddress对象其实就是表示一个区域,其构造方法如下:
CellRangeAddress(firstRow, lastRow, firstCol, lastCol),参数的说明:
firstRow 区域中第一个单元格的行号
lastRow 区域中最后一个单元格的行号
firstCol 区域中第一个单元格的列号
lastCol 区域中最后一个单元格的列号
提示: 即使你没有用CreateRow和CreateCell创建过行或单元格,也完全可以直接创建区域然后把这一区域合并,Excel的区域合并信息是单独存储的,和RowRecord、ColumnInfoRecord不存在直接关系。
单元格对齐
HSSFCell cell=row.createCell(0);
cell.setCellValue("单元格对齐");
HSSFCellStyle style=workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);//水平居中
style.setVerticalAlignment(VerticalAlignment.JUSTIFY);//垂直居中
style.setWrapText(true);//自动换行
style.setIndention((short)5);//缩进
style.setRotation((short)60);//文本旋转,这里的取值是从-90到90,而不是0-180度。
cell.setCellStyle(style);
水平对齐相关参数(HorizontalAlignment类中)
垂直对齐相关参数(VerticalAlignment类中)
4、设置单元格样式
/**
* 导出新建的Excel表格(设置字体和单元格样式)
*
* @param response
* @param request
* @throws Exception
*/
@RequestMapping("/exportExcel")
public void exportExcel(HttpServletResponse response, HttpServletRequest request) throws Exception {
//创建Excel文件
HSSFWorkbook workbook = new HSSFWorkbook();
//创建工作表,并设置表名
HSSFSheet sheet = workbook.createSheet("table1");
//创建一行
HSSFRow row = sheet.createRow(0);
//创建一行中的一列
HSSFCell cell = row.createCell(0);
//设置单元格中的值
cell.setCellValue("合并列");
CellRangeAddress range = new CellRangeAddress(0, 0, 0, 6);
sheet.addMergedRegion(range);
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);//设置水平居中
style.setVerticalAlignment(VerticalAlignment.CENTER);//设置垂直居中
style.setBorderLeft(BorderStyle.THIN);//设置左边框
style.setBorderTop(BorderStyle.THIN);//设置上边框
style.setBorderRight(BorderStyle.THIN);//设置右边框
style.setBorderBottom(BorderStyle.THIN);//设置下边框
style.setLeftBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());//设置左边框颜色为黑色
style.setTopBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());//设置上边框颜色为黑色
style.setRightBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());//设置上边框颜色为黑色
style.setBottomBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());//设置上边框颜色为黑色
//设置字体
HSSFFont font = workbook.createFont();
font.setFontName("华文楷体");//设置字体名称
font.setFontHeightInPoints((short) 16);//设置字号
style.setFont(font);//将字体设置到样式中
row.setHeightInPoints(32);//设置行的高度为32
cell.setCellStyle(style);
//设置Excel表的名称
String fileName = "样式Excel.xls";
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename="
+ URLEncoder.encode(fileName, "UTF-8"));
//将输出流转换成高效缓存输出流
OutputStream out = new BufferedOutputStream(response.getOutputStream());
//向外输出excel表格
workbook.write(out);
out.flush();
out.close();
}
5、导出Excel表格(例子:学生成绩表)
成绩表
序号 姓名 性别 成绩 总成绩
语文 数学 英语 物理 化学 生物 历史
/**
* 导出Excel表格(学生成绩表)
*
* @param response
* @param request
* @throws Exception
*/
@RequestMapping("/exportExcel")
public void exportExcel(HttpServletResponse response, HttpServletRequest request) throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("711");
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellValue("成绩表");
CellRangeAddress range = new CellRangeAddress(0, 0, 0, 10);
sheet.addMergedRegion(range);
HSSFCellStyle style = workbook.createCellStyle();
HSSFFont font = workbook.createFont();
font.setFontName("华文楷体");
font.setFontHeightInPoints((short) 18);
style.setFont(font);
row.setHeightInPoints(50);
style.setAlignment(HorizontalAlignment.CENTER);//设置水平居中
style.setVerticalAlignment(VerticalAlignment.CENTER);//设置垂直居中
cell.setCellStyle(style);
//第二行导航栏设置
range = new CellRangeAddress(1, 2, 0, 0);
sheet.addMergedRegion(range);
range = new CellRangeAddress(1, 2, 1, 1);
sheet.addMergedRegion(range);
range = new CellRangeAddress(1, 2, 2, 2);
sheet.addMergedRegion(range);
range = new CellRangeAddress(1, 2, 10, 10);
sheet.addMergedRegion(range);
range = new CellRangeAddress(1, 1, 3, 9);
sheet.addMergedRegion(range);
style = workbook.createCellStyle();
font = workbook.createFont();
font.setFontName("华文楷体");
font.setFontHeightInPoints((short) 12);
style.setAlignment(HorizontalAlignment.CENTER);//设置水平居中
style.setVerticalAlignment(VerticalAlignment.CENTER);//设置垂直居中
style.setFont(font);
HSSFRow row1 = sheet.createRow(1);
cell = row1.createCell(0);
cell.setCellValue("序号");
cell.setCellStyle(style);
HSSFCell cell1 = row1.createCell(1);
cell1.setCellValue("姓名");
cell1.setCellStyle(style);
HSSFCell cell2 = row1.createCell(2);
cell2.setCellValue("性别");
cell2.setCellStyle(style);
HSSFCell cell3 = row1.createCell(3);
cell3.setCellValue("成绩");
cell3.setCellStyle(style);
HSSFCell cell10 = row1.createCell(10);
cell10.setCellValue("总成绩");
cell10.setCellStyle(style);
HSSFRow row2 = sheet.createRow(2);
cell3 = row2.createCell(3);
cell3.setCellValue("语文");
cell3.setCellStyle(style);
HSSFCell cell4 = row2.createCell(4);
cell4.setCellValue("数学");
cell4.setCellStyle(style);
HSSFCell cell5 = row2.createCell(5);
cell5.setCellValue("英语");
cell5.setCellStyle(style);
HSSFCell cell6 = row2.createCell(6);
cell6.setCellValue("物理");
cell6.setCellStyle(style);
HSSFCell cell7 = row2.createCell(7);
cell7.setCellValue("化学");
cell7.setCellStyle(style);
HSSFCell cell8 = row2.createCell(8);
cell8.setCellValue("生物");
cell8.setCellStyle(style);
HSSFCell cell9 = row2.createCell(9);
cell9.setCellValue("历史");
cell9.setCellStyle(style);
//设置Excel表的名称
String fileName = "学生成绩表.xls";
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename="
+ URLEncoder.encode(fileName, "UTF-8"));
//将输出流转换成高效缓存输出流
OutputStream out = new BufferedOutputStream(response.getOutputStream());
//向外输出excel表格
workbook.write(out);
out.flush();
out.close();
}