1.POI的基础知识:
HSSF提供给用户使用的对象在org.apache.poi.hssf.usermodel包中,主要部分包括Excell对象,样式和格式,还有辅助操作。有以下几种对象:
HSSFWorkbook excell的文档对象
HSSFSheet excell的表单
HSSFRow excell的行
HSSFCell excell的格子单元
HSSFFont excell字体
HSSFName 名称
HSSFDataFormat 日期格式
在poi1.7中才有以下2项:
HSSFHeader sheet头
HSSFFooter sheet尾
和这个样式
HSSFCellStyle cell样式
辅助操作包括
HSSFDateUtil 日期
HSSFPrintSetup 打印
HSSFErrorConstants 错误信息表
2.POI的基础功能点:
1.创建sheet:
写道
public void createSheet(String title) {
workBook = new HSSFWorkbook();
sheet = workBook.createSheet(title);
sheet.setVerticallyCenter(true);
}
2.创建row:
写道
public HSSFRow createRow(HSSFCellStyle style, int currentRow, int colNum) {
HSSFRow row = sheet.createRow(currentRow);
for (short cellIndex = 0; cellIndex < colNum; cellIndex++) {
HSSFCell cell = row.createCell(cellIndex);
cell.setCellValue("");
cell.setCellStyle(style);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
}
return row;
}
注意:在创建row的时候,如果正行的cell的样式一样的话,最好在这个时候就把样式一起设置给cell。因为合并单元格后,合并后的cell的样式是由合并前多个cell的样式组合成的。(- -。有点绕)
3.合并单元格:
写道
public void mergedRegion(int rowFrom, int colFrom, int rowTo, int colTo) {
Region region = new Region(rowFrom, (short) colFrom, rowTo,
(short) colTo);
sheet.addMergedRegion(region);
}
3例子:
写道
/**
* 生成表头
*
* @param sheet
* @param header
* @param title
* @param rowNum
*/
private void createInterfaceSheetHeader(String[] header, String title,
int rowNum) {
HSSFSheet sheet = getSheet();
for (int i = 0; i < header.length; i++) {
if ("说明".equals(header[i])) {
sheet.setColumnWidth(i, 5000);
} else {
sheet.setColumnWidth(i, 3000);
}
}
short length = (short) (header.length - 1);
// row 1
HSSFRow row = null;
row = createRow(getCellStyle1(), 0, header.length);
row.setHeightInPoints(50);
mergedRegion(0, (short) 0, 0, length);
HSSFCell cell = row.getCell(0);
cell.setCellValue(title);
// row2-4
row = createRow(getCellStyle2(), 1, header.length);
row.setHeightInPoints(15);
cell = row.getCell(0);
cell.setCellValue("版本号");
mergedRegion(1, (short) 1, 1, length);
cell = row.getCell(1);
row = createRow(getCellStyle2(), 2, header.length);
row.setHeightInPoints(15);
cell = row.getCell(0);
cell.setCellValue("定制日期");
mergedRegion(2, (short) 1, 2, length);
cell = row.getCell(1);
//生成当前日期
cell.setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
row = createRow(getCellStyle2(), 3, header.length);
row.setHeightInPoints(15);
cell = row.getCell(0);
cell.setCellValue("定制者");
mergedRegion(3, (short) 1, 3, length);
cell = row.getCell(1);
// row5
row = createRow(getCellStyle3(), 4, header.length);
row.setHeightInPoints(20);
mergedRegion(4, (short) 0, 4, length);
cell = row.getCell(0);
cell.setCellValue("修订记录");
// row6-8
row = createRow(getCellStyle2(), 5, header.length);
row.setHeightInPoints(15);
mergedRegion(5, (short) 2, 5, length);
cell = row.getCell(0);
cell.setCellValue("修订日期");
cell = row.getCell(1);
cell.setCellValue("修订者");
cell = row.getCell(2);
cell.setCellValue("修订说明");
row = createRow(getCellStyle2(), 6, header.length);
row.setHeightInPoints(15);
mergedRegion(6, (short) 2, 6, length);
row = createRow(getCellStyle2(), 7, header.length);
row.setHeightInPoints(15);
mergedRegion(7, (short) 2, 7, length);
// row9
row = createRow(getCellStyle4(), 8, header.length);
row.setHeightInPoints(15);
mergedRegion(8, (short) 0, 8, length);
createSheetHeader(sheet, header, 9);
}
/**
* row 1
*
* @return
*/
public HSSFCellStyle getCellStyle1() {
HSSFCellStyle style = getWorkBook().createCellStyle();
HSSFFont font = getWorkBook().createFont();
setCommonStyle(style);
font.setFontName("宋体");
font.setFontHeightInPoints((short) 14);// 设置字体大小
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
style.setWrapText(true);
style.setFont(font);
return style;
}
/**
* row 2-4 6-8
*
* @return
*/
public HSSFCellStyle getCellStyle2() {
HSSFCellStyle style = getWorkBook().createCellStyle();
HSSFFont font = getWorkBook().createFont();
setCommonStyle(style);
font.setFontName("宋体");
font.setFontHeightInPoints((short) 10);// 设置字体大小
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
style.setWrapText(true);
style.setFont(font);
return style;
}
/**
* row 5
*
* @return
*/
public HSSFCellStyle getCellStyle3() {
HSSFCellStyle style = getWorkBook().createCellStyle();
HSSFFont font = getWorkBook().createFont();
setCommonStyle(style);
font.setFontName("宋体");
font.setFontHeightInPoints((short) 11);// 设置字体大小
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
style.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setWrapText(true);
style.setFont(font);
return style;
}
/**
* row 9
*
* @return
*/
public HSSFCellStyle getCellStyle4() {
HSSFCellStyle style = getWorkBook().createCellStyle();
HSSFFont font = getWorkBook().createFont();
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THICK);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
font.setFontName("宋体");
font.setFontHeightInPoints((short) 11);// 设置字体大小
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
style.setWrapText(true);
style.setFont(font);
return style;
}
/**
* row 10
*
* @return
*/
public HSSFCellStyle getCellStyle5() {
HSSFCellStyle style = getWorkBook().createCellStyle();
HSSFFont font = getWorkBook().createFont();
setCommonStyle(style);
font.setFontName("宋体");
font.setFontHeightInPoints((short) 11);// 设置字体大小
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
style.setFillForegroundColor(HSSFColor.LAVENDER.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setWrapText(true);
style.setFont(font);
return style;
}
/**
* row 11-n
*
* @return
*/
public HSSFCellStyle getCellStyle6() {
HSSFCellStyle style = getWorkBook().createCellStyle();
HSSFFont font = getWorkBook().createFont();
setCommonStyle(style);
font.setFontName("宋体");
font.setFontHeightInPoints((short) 10.5);// 设置字体大小
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
style.setWrapText(true);
style.setFont(font);
return style;
}