package cn.tenyears.demo.excel;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.hssf.util.Region;
public class FormExcel {
protected HSSFWorkbook workbook = null;
protected HSSFSheet sheet = null;
protected int startX;
protected int startY;
protected int cX;
protected int currentX;
protected int currentY;
protected int virtualY;
protected HSSFRow currentRow = null;
protected HSSFCell currentCell = null;
protected String title = null;
public FormExcel(HSSFWorkbook workbook, HSSFSheet sheet, int startX,
int startY, int cX, String title) {
this.workbook = workbook;
this.sheet = sheet;
this.startX = startX;
this.startY = startY;
this.cX = cX;
this.title = title;
virtualY = 0;
init();
}
// 创建Title
protected void init() {
currentX = startX - 1;
currentY = startY - 1;
if (title != null) {
createRow();
createCell(title);
Region r = new Region(currentY, (short) currentX, currentY + 1,
(short) (currentX + cX - 1));
sheet.addMergedRegion(r);
setFont("黑体", 16);
setHAlign(HSSFCellStyle.ALIGN_CENTER);
setVAlign(HSSFCellStyle.VERTICAL_CENTER);
currentY += 1;
}
}
public void createRow() {
if (virtualY != 0) {
currentY = virtualY + 1;
virtualY = 0;
} else
currentY++;
currentX = startX - 1; // 归 0
this.currentRow = sheet.createRow(currentY);
}
public void createCell(String value) {
currentX++;
this.currentCell = currentRow.createCell((short) currentX);
this.currentCell.setCellValue(new HSSFRichTextString(value));
this.currentCell.setCellType(HSSFCell.CELL_TYPE_STRING);
createStyle();
}
public void createCell(String value, short h, short v) {
createCell(value);
createStyle();
setHAlign(h);
setVAlign(v);
}
public void createMemo(String value, int cx, int cy) {
createCell(value);
Region r = new Region(currentY, (short) currentX, currentY + cy - 1,
(short) (currentX + cx - 1));
sheet.addMergedRegion(r);
currentX += cx - 1;
virtualY = currentY + cy - 1;
}
public void createStyle() {
HSSFCellStyle style = workbook.createCellStyle();
currentCell.setCellStyle(style);
}
public void setFont(String fontName, int size) {
HSSFCellStyle style = currentCell.getCellStyle();
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) size);
font.setFontName(fontName);
style.setFont(font);
}
public void setHAlign(short align) {
currentCell.getCellStyle().setAlignment(align);
}
public void setVAlign(short align) {
currentCell.getCellStyle().setVerticalAlignment(align);
}
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
FormExcel form = new FormExcel(wb, sheet, 0, 0, 10, "张山的简历");
form.createRow();
form.createCell("姓名");
form.createCell("张山");
form.createCell("年龄");
form.createCell("25");
form.createCell("毕业学校");
form.createCell("XX大学");
form.createRow();
form.createCell("英语水平");
form.createCell("六级");
form.createCell("专业");
form.createCell("计算机");
form.createRow();
form.createMemo("简历描述", 1, 8);
form.setHAlign(HSSFCellStyle.ALIGN_CENTER);
form.setVAlign(HSSFCellStyle.VERTICAL_CENTER);
form.createMemo("大学经历", 9, 8);
form.setHAlign(HSSFCellStyle.ALIGN_LEFT);
form.setVAlign(HSSFCellStyle.VERTICAL_TOP);
form.createRow();
form.createMemo("大学课程", 1, 5);
form.setHAlign(HSSFCellStyle.ALIGN_CENTER);
form.setVAlign(HSSFCellStyle.VERTICAL_CENTER);
form.createMemo("数据结构 编译原理 离散数学等", 9, 5);
form.setHAlign(HSSFCellStyle.ALIGN_LEFT);
form.setVAlign(HSSFCellStyle.VERTICAL_TOP);
form.createRow();
form.createMemo("求职意向", 1, 5);
form.setHAlign(HSSFCellStyle.ALIGN_CENTER);
form.setVAlign(HSSFCellStyle.VERTICAL_CENTER);
form.createMemo("程序员", 9, 5);
form.setHAlign(HSSFCellStyle.ALIGN_LEFT);
form.setVAlign(HSSFCellStyle.VERTICAL_TOP);
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();[www.iocblog.net 来源]
}
} |