欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

poi导出excel表格伪代码

更新时间: 2014-01-05 03:07:32 责任编辑: Author_N1

 

POI导出Excel报表伪代码

  // ============创建HSSFWorkbook============
  HSSFWorkbook wb = new HSSFWorkbook();
  // ============创建HSSFWorkbook============
  
  // ============创建字样============
  HSSFFont titleFont = wb.createFont();
  titleFont.setFontHeightInPoints((short)20);
  titleFont.setBoldweight((short)20);
  titleFont.setFontName("黑体");


  HSSFFont dateFont = wb.createFont();
  dateFont.setFontHeightInPoints((short)12);
  dateFont.setFontName("楷体_GB2312");
  // ============创建字样============
  
  // ============创建样式============
  HSSFCellStyle titleStytle = wb.createCellStyle();
  titleStytle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  titleStytle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  titleStytle.setFont(titleFont);
  titleStytle.setWrapText(true);
  
  HSSFCellStyle dataStytle = wb.createCellStyle();
  dataStytle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
  dataStytle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  dataStytle.setBorderRight(HSSFCellStyle.BORDER_THIN);
  dataStytle.setBorderTop(HSSFCellStyle.BORDER_THIN);
  dataStytle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  dataStytle.setWrapText(true);
  
  HSSFCellStyle dataStytle1 = wb.createCellStyle();
  dataStytle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  dataStytle1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  dataStytle1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  dataStytle1.setBorderRight(HSSFCellStyle.BORDER_THIN);
  dataStytle1.setBorderTop(HSSFCellStyle.BORDER_THIN);
  dataStytle1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  dataStytle.setWrapText(true);
  // ============创建样式============
  
  // ============创建HSSFSheet============
  HSSFSheet sheet = wb.createSheet();
  sheet.setDisplayGridlines(false);
  // ============创建HSSFSheet============
  
  // 标题行
  HSSFRow row = sheet.createRow(0);
  row.setHeightInPoints(30);
  sheet.addMergedRegion(new Region(0,(short)0,0,(short)(titleColumn.length-1)));
  HSSFCell cell = row.createCell((short)0);
  // cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue(new HSSFRichTextString(title));
  cell.setCellStyle(titleStytle);
  
  // 日期行
  row = sheet.createRow(1);
  row.setHeightInPoints(20);
  sheet.addMergedRegion(new Region(1,(short)0,1,(short)(titleColumn.length-1)));
  cell = row.createCell((short)0);
  // cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  cell.setCellValue(new HSSFRichTextString(date));
  
  // 表头行
  row = sheet.createRow(2);
  row.setHeightInPoints(20);
  for (int i = 0; i < titleColumnSize.length; i++) {
   cell = row.createCell((short)i);
   sheet.setColumnWidth((short)i, (short)titleColumnSize[i]);
   // cell.setEncoding(HSSFCell.ENCODING_UTF_16);
   cell.setCellValue(new HSSFRichTextString(titleColumn[i]));
   cell.setCellStyle(dataStytle);
  }
  
  // 数据行
  if(null!=reportDTO){
   List creditGroupList = reportDTO.getCreditGroup();
    for (Iterator iterator = creditGroupList.iterator(); iterator.hasNext();) {
      CreditGroup creditGroup = (CreditGroup)iterator.next();
      List creditList = creditGroup.getCreditList();
      System.out.println(creditList.size());
      for (Iterator iterator2 = creditList.iterator(); iterator2.hasNext();) {
       Credit credit = (Credit) iterator2.next();
       row = sheet.createRow(sheet.getLastRowNum()+1);
       row.setHeightInPoints(20);
       for (int i = 0; i < titleColumnSize.length; i++) {
        cell = row.createCell((short)i);
        // cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        if(i == 0 ){
         cell.setCellValue(new HSSFRichTextString(creditGroup.getGroupID()));
         cell.setCellStyle(dataStytle);
        }
        if(i == 1 ){
         cell.setCellValue(new HSSFRichTextString(creditGroup.getGroupName()));
         cell.setCellStyle(dataStytle);
        }
        
        if(i == 2 ){
         cell.setCellValue(new HSSFRichTextString(credit.getCreditName()));
         cell.setCellStyle(dataStytle);
        }
        if(i == 3 ){
         cell.setCellValue(new HSSFRichTextString(credit.getGuojianNum()));
         cell.setCellStyle(dataStytle);
        }
        if(i == 4 ){
         cell.setCellValue(new HSSFRichTextString(credit.getDuijianNum()));
         cell.setCellStyle(dataStytle);
        }
        if(i == 5 ){
         cell.setCellValue(new HSSFRichTextString(credit.getBujianNmu()));
         cell.setCellStyle(dataStytle);
        }
       }
      }
      sheet.addMergedRegion(new Region(sheet.getLastRowNum()-creditList.size()+1,(short)0,sheet.getLastRowNum(),(short)0));
      sheet.addMergedRegion(new Region(sheet.getLastRowNum()-creditList.size()+1,(short)1,sheet.getLastRowNum(),(short)1));
      sheet.getRow(sheet.getLastRowNum()-creditList.size()+1).getCell((short)0).setCellStyle(dataStytle1);
      sheet.getRow(sheet.getLastRowNum()-creditList.size()+1).getCell((short)1).setCellStyle(dataStytle1);
    }
  }
  response.setHeader("Content-Type", "application/msexcel;charset=GBK");
  response.setHeader("Content-Disposition", "attachment;filename=" + new String("Report".getBytes(), "iso-8859-1") + ".xls");
  os = response.getOutputStream();
  wb.write(os);

上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-01-05 03:07:32
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/18636/
本文WWW.DOC100.NET DOC100.NET版权所有。