java poi的使用经验

最近项目中需要使用excel做表现层,现将使用总结如下:
  HSSFWorkbook ---- HSSFWorkSheet--------HSSFRow-------HSSFCell

  excel文件 excel的一张表 表的一行 一个单元格

  用来生成Cell的方法:


  private void createCell(HSSFWorkbook wb, HSSFRow row, int col, String val) {

  HSSFCell cell = row.createCell(col);

  cell.setCellValue(val);

  //设置单元格样式

  HSSFCellStyle cellstyle = wb.createCellStyle();

  cellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);//设置水平对齐方式

  cellstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//设置垂直对齐方式

  //设置单元格格式

  //cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);

  cell.setCellStyle(cellstyle);

  }



  创建表头


  public boolean createXlsHead_day(HSSFWorkbook wb) throws Exception{

  boolean flag = true;

  try{

  HSSFSheet sheet1 = wb.createSheet("Sheet1");

  wb.setSheetName(0, "XREB消费日报表");

  HSSFRow head = sheet1.createRow(0);

  HSSFRow head2 = sheet1.createRow(1);

  // 第一行表头

  createCell(wb, head, 0, "消费日期");

  createCell(wb, head, 1, "消费地点");

  createCell(wb, head, 3, "夜餐");

  createCell(wb, head, 5, "午餐");

  createCell(wb, head, 7, "晚餐");

  createCell(wb, head, 9, "小记");

  CellRangeAddress range1 = new CellRangeAddress(0, 1, 0, 0); //合并单元格

  CellRangeAddress range2 = new CellRangeAddress(0, 0, 1, 2);

  CellRangeAddress range3 = new CellRangeAddress(0, 0, 3, 4);

  CellRangeAddress range4 = new CellRangeAddress(0, 0, 5, 6);

  CellRangeAddress range5 = new CellRangeAddress(0, 0, 7, 8);

  CellRangeAddress range6 = new CellRangeAddress(0, 0, 9, 10);

  sheet1.addMergedRegion(range1);

  sheet1.addMergedRegion(range2);

  sheet1.addMergedRegion(range3);

  sheet1.addMergedRegion(range4);

  sheet1.addMergedRegion(range5);

  sheet1.addMergedRegion(range6);

  // 表头第二行

  createCell(wb, head2, 1, "消费机号");

  createCell(wb, head2, 2, "营业点名称");

  createCell(wb, head2, 3, "人次");

  createCell(wb, head2, 4, "金额");

  createCell(wb, head2, 5, "人次");

  createCell(wb, head2, 6, "金额");

  createCell(wb, head2, 7, "人次");

  createCell(wb, head2, 8, "金额");

  createCell(wb, head2, 9, "人次");

  createCell(wb, head2, 10, "金额");

  }catch(Exception ex){

  flag = false;

  throw ex;

  }

  return flag;

  }
 

你可能感兴趣的:(Java POI)