项目 >> Excel >> POI

使用POI

实际工作中使用Excel的地方很多,而ApachePOI是Java编写的操作Excel文件的有效方法之一,功能很强大,使用也很方便,可以脱离Office使用,另外关键它是开源的。POI最新版本是3.0测试版。其实POI不仅仅能够操作Excel,也能操作Word,OLE2文档等。[www.iocblog.net 来源]

以下编写了一个简单的Java类,用于生成Excel表格,类似于“简历”类型的样式,算是练手。

POI的Java命名有点怪异,显得比较罗嗦,而且很多类方法有商榷之处,不过这些不影响使用,毕竟实用为主。

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, (shortcurrentX, 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((shortcurrentX);
    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, (shortcurrentX, 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((shortsize);
    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[] argsthrows IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");

    FormExcel form = new FormExcel(wb, sheet, 0010"张山的简历");
    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("简历描述"18);
    form.setHAlign(HSSFCellStyle.ALIGN_CENTER);
    form.setVAlign(HSSFCellStyle.VERTICAL_CENTER);
    form.createMemo("大学经历"98);
    form.setHAlign(HSSFCellStyle.ALIGN_LEFT);
    form.setVAlign(HSSFCellStyle.VERTICAL_TOP);

    form.createRow();
    form.createMemo("大学课程"15);
    form.setHAlign(HSSFCellStyle.ALIGN_CENTER);
    form.setVAlign(HSSFCellStyle.VERTICAL_CENTER);
    form.createMemo("数据结构 编译原理 离散数学等"95);
    form.setHAlign(HSSFCellStyle.ALIGN_LEFT);
    form.setVAlign(HSSFCellStyle.VERTICAL_TOP);
    form.createRow();

    form.createMemo("求职意向"15);
    form.setHAlign(HSSFCellStyle.ALIGN_CENTER);
    form.setVAlign(HSSFCellStyle.VERTICAL_CENTER);
    form.createMemo("程序员"95);
    form.setHAlign(HSSFCellStyle.ALIGN_LEFT);
    form.setVAlign(HSSFCellStyle.VERTICAL_TOP);

    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();[www.iocblog.net 来源]
  }
}



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。

相关项目