1.POI简介
Jakarta POI 是一套用于访问微软格式文档的Java API. 组件HWPF用于操作Word的; 组件HSSF用于操作Excel格式文件.
2.常用组件
HSSFWorkbook – excel的文档对象 HSSFSheet – excel的表单 HSSFRow – excel的行 HSSFCell – excel的格子单元 HSSFHeader – sheet头 HSSFFooter – sheet尾(只有打印的时候才能看到效果) HSSFDataFormat – 日期格式 HSSFCellStyle – cell样式 HSSFFont – excel字体 HSSFColor – 颜色 HSSFDateUtil – 日期 HSSFPrintSetup – 打印 HSSFErrorConstants – 错误信息表
合并单元格,构造参数依次表示起始行,截止行,起始列,截止列 eg:sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));
设置单元格样式时,先创建样式,再指定到单元格。 样式可指定对齐方式、背景填充方式及颜色、上下左右边框样式及颜色
设置单元格的填充方式,以及前景颜色和背景颜色时注意: a.如果需要前景颜色或背景颜色,一定要指定填充方式,两者顺序无所谓; b.如果同时存在前景颜色和背景颜色,前景颜色的设置要写在前面; c.前景颜色不是字体颜色。
3.结构说明
Excel <-- 一 工作空间(workbook) workbook <-- 多 工作表(sheet) sheet <-- 多 行(row) + 多列(cell)
4.操作步骤
a、用HSSFWorkbook打开或者创建Excel文件对象 b、用HSSFWorkbook对象返回或者创建Sheet对象 c、用Sheet对象返回行对象,用行对象得到Cell对象 d、对Cell对象读写
5.实例
第一种方法 固定导出字段导出excel; 第二种方法 用配置的方式将导出字段存储数库中导出excel,可重用;
6.pom.xml
<!-- Excel POI
-->
<dependency
>
<groupId
>org
.apache
.poi
</groupId
>
<artifactId
>poi
</artifactId
>
<version
>3.11</version
>
</dependency
>
<dependency
>
<groupId
>org
.apache
.poi
</groupId
>
<artifactId
>poi
-ooxml
</artifactId
>
<version
>3.11</version
>
</dependency
>
<dependency
>
<groupId
>org
.apache
.poi
</groupId
>
<artifactId
>poi
-ooxml
-schemas
</artifactId
>
<version
>3.11</version
>
</dependency
>
7.代码块
public JtsResult
<String
> getExcel(HttpServletRequest request
, HttpServletResponse response
) {
List
<HomesteadVo
> Homesteads
= homesteadService
.getListPC(new HomesteadBo());
String
[] titleName
= {"名称","电话","镇","村","组","详细地址","户主身份证正反面","手持身份证照片",
"户口溥照片","房屋照片","房屋权属来源证明文件"};
try
{
HSSFWorkbook wb
= new HSSFWorkbook();
HSSFSheet sheet
= wb
.createSheet("宗地数据统计");
sheet
.setDefaultRowHeightInPoints(10);
sheet
.setDefaultColumnWidth(10);
HSSFCellStyle cellStyle
= wb
.createCellStyle();
cellStyle
.setBorderBottom(HSSFCellStyle
.BORDER_THIN
);
cellStyle
.setBottomBorderColor(HSSFColor
.BLACK
.index
);
cellStyle
.setBorderLeft(HSSFCellStyle
.BORDER_THIN
);
cellStyle
.setLeftBorderColor(HSSFColor
.RED
.index
);
cellStyle
.setBorderRight(HSSFCellStyle
.BORDER_THIN
);
cellStyle
.setRightBorderColor(HSSFColor
.BLACK
.index
);
cellStyle
.setBorderTop(HSSFCellStyle
.BORDER_THIN
);
cellStyle
.setTopBorderColor(HSSFColor
.BLACK
.index
);
HSSFFont font
= wb
.createFont();
cellStyle
.setFont(font
);
cellStyle
.setWrapText(false);
cellStyle
.setAlignment(HSSFCellStyle
.ALIGN_CENTER
);
cellStyle
.setVerticalAlignment(HSSFCellStyle
.VERTICAL_CENTER
);
HSSFRow row1
= sheet
.createRow(0);
HSSFCell cell
= row1
.createCell(0);
cell
.setCellValue("宗地数据一览表");
cell
.setCellStyle(cellStyle
);
sheet
.addMergedRegion(new CellRangeAddress(0, 0, 0, 11));
HSSFRow row2
= sheet
.createRow(1);
for (int i
= 0; i
< titleName
.length
; i
++) {
sheet
.setColumnWidth(i
, 50*100);
if(i
==5){
sheet
.setColumnWidth(i
, 50*155);
}
HSSFCell cellEntity
= row2
.createCell(i
);
cellEntity
.setCellValue(titleName
[i
]);
cellEntity
.setCellStyle(cellStyle
);
}
for (int i
= 0; i
< Homesteads
.size(); i
++) {
HSSFRow row
= sheet
.createRow(i
+ 2);
HomesteadVo homesteadVo
= Homesteads
.get(i
);
for (int j
= 0; j
< 11; j
++) {
HSSFCell cellEntity
= row
.createCell(j
);
cellEntity
.setCellStyle(cellStyle
);
switch (j
) {
case 0:
cellEntity
.setCellValue(homesteadVo
.getName());
break;
case 1:
cellEntity
.setCellValue(homesteadVo
.getPhone());
break;
case 2:
cellEntity
.setCellValue(homesteadVo
.getTown());
break;
case 3:
cellEntity
.setCellValue(homesteadVo
.getVillage());
break;
case 4:
cellEntity
.setCellValue(homesteadVo
.getVillageGroup());
break;
case 5:
cellEntity
.setCellValue(homesteadVo
.getGeographicalPosition());
break;
case 6:
cellEntity
.setCellValue(homesteadVo
.getIdCardProve());
break;
case 7:
cellEntity
.setCellValue(homesteadVo
.getHouseholdHandheldIdCardProve());
break;
case 8:
cellEntity
.setCellValue(homesteadVo
.getRegisteredPermanentResidenceProve());
break;
case 9:
cellEntity
.setCellValue(homesteadVo
.getHousingProve());
break;
case 10:
cellEntity
.setCellValue(homesteadVo
.getHousingOwnershipSourceProve());
break;
default:
break;
}
}
}
OutputStream output
;
output
= response
.getOutputStream();
response
.reset();
response
.setHeader("Content-disposition", "attachment; filename=fwyth.xls");
response
.setContentType("application/msexcel");
wb
.write(output
);
output
.close();
return null
;
} catch
(IOException e
) {
e
.printStackTrace();
} catch
(SecurityException e
) {
e
.printStackTrace();
}
JtsResult
<String
> result
= JtsResultUtil
.success("导出成功");
return result
;
}