excel导出

package com.paic.sas.exam.web.util;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFComment;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
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.HSSFColor;

import com.paic.sas.exam.dto.ExportReport;
import com.paic.sas.exam.dto.Questions;

public class ExportExcel {
        

       public void exportExcel(List<String>headers, List<ExportReport> exportReport,

             OutputStream out) {

          exportExcel("测试POI导出EXCEL文档", headers, exportReport,out, "yyyy-MM-dd");

       }


    

       /**

        * 这是一个通用的方法,利用了JAVA的反射机制,可以将放置在JAVA集合中并且符号一定条件的数据以EXCEL 的形式输出到指定IO设备上

        *

        * @param title

        *            表格标题名

        * @param headers

        *            表格属性列名数组

        * @param dataset

        *            需要显示的数据集合,集合中一定要放置符合javabean风格的类的对象。此方法支持的

        *            javabean属性的数据类型有基本数据类型及String,Date,byte[](图片数据)

        * @param out

        *            与输出设备关联的流对象,可以将EXCEL文档导出到本地文件或者网络中

        * @param pattern

        *            如果有时间数据,设定输出格式。默认为"yyy-MM-dd"

        */

       @SuppressWarnings("all")

       public void exportExcel(String title, List<String> headers,

               List<ExportReport> exportReport, OutputStream out, String pattern) {

          // 声明一个工作薄

          HSSFWorkbook workbook = new HSSFWorkbook();

          // 生成一个表格

          HSSFSheet sheet = workbook.createSheet(title);

          // 设置表格默认列宽度为15个字节

          sheet.setDefaultColumnWidth((int) 20);

          // 生成一个样式

          HSSFCellStyle style = workbook.createCellStyle();

          // 设置这些样式

          style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);

          style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

          style.setBorderBottom(HSSFCellStyle.BORDER_THIN);

          style.setBorderLeft(HSSFCellStyle.BORDER_THIN);

          style.setBorderRight(HSSFCellStyle.BORDER_THIN);

          style.setBorderTop(HSSFCellStyle.BORDER_THIN);

          style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

          // 生成一个字体

          HSSFFont font = workbook.createFont();

          font.setColor(HSSFColor.VIOLET.index);

          font.setFontHeightInPoints((short) 12);

          font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

          // 把字体应用到当前的样式

          style.setFont(font);

          // 生成并设置另一个样式

          HSSFCellStyle style2 = workbook.createCellStyle();

          style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);

          style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

          style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);

          style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);

          style2.setBorderRight(HSSFCellStyle.BORDER_THIN);

          style2.setBorderTop(HSSFCellStyle.BORDER_THIN);

          style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);

          style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

          // 生成另一个字体

          HSSFFont font2 = workbook.createFont();

          font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);

          // 把字体应用到当前的样式

          style2.setFont(font2);

         

          // 声明一个画图的顶级管理器

          HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

          // 定义注释的大小和位置,详见文档

          HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5));

          // 设置注释内容

          comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));

          // 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容.

          comment.setAuthor("leno");
         

          //产生表格标题行

          HSSFRow row = sheet.createRow(0);

          for (int i = 0; i < headers.size(); i++) {

             HSSFCell cell = row.createCell(i);

             cell.setCellStyle(style);

             HSSFRichTextString text = new HSSFRichTextString(headers.get(i));

             cell.setCellValue(text);

          }

    

          //遍历集合数据,产生数据行
      int index=0;
    if (null!=exportReport&&exportReport.size()!=0) {
        for (ExportReport excel : exportReport) {
            index++;
             HSSFRow rows = sheet.createRow(index);
             HSSFCell cell = rows.createCell(0);
             cell.setCellStyle(style2);
             cell.setCellValue(excel.getUserName());
            
             HSSFCell cell1 = rows.createCell(1);
             cell1.setCellStyle(style2);
             cell1.setCellValue(excel.getUmName());
            
             HSSFCell cell2 = rows.createCell(2);
             cell2.setCellStyle(style2);
             cell2.setCellValue(excel.getDepartmentName());
            
             HSSFCell cell3 = rows.createCell(3);
             cell3.setCellStyle(style2);
             cell3.setCellValue(excel.getFieldManager());
            
             HSSFCell cell4 = rows.createCell(4);
             cell4.setCellStyle(style2);
             cell4.setCellValue(excel.getTestPaperName());
            
             HSSFCell cell5 = rows.createCell(5);
             cell5.setCellStyle(style2);
             cell5.setCellValue(excel.getSingleScore());
            
             HSSFCell cell6 = rows.createCell(6);
             cell6.setCellStyle(style2);
             cell6.setCellValue(excel.getMultipleScore());
            
             HSSFCell cell7 = rows.createCell(7);
             cell7.setCellStyle(style2);
             cell7.setCellValue(excel.getJudgementScore());
            
             List<Questions> questionList = excel.getQuestionList();
             if (null!=questionList&&questionList.size()!=0) {
                 int i=8;
                for (Questions questions : questionList) {
                     HSSFComment comments = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 7, 10));
                      comments.setString(new HSSFRichTextString(questions.getPersonalAnwser()));
                    HSSFCell cells = rows.createCell(i);
                    cells.setCellStyle(style2);
                    cells.setCellValue(questions.getPersonalAnwser());
                    sheet.autoSizeColumn(i);
                    cells.setCellComment(comments);
                    //comment.setAuthor(questions.getPersonalAnwser());
                    i++;
                }
            }
        }
    }
    
    try {
        workbook.write(out);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}
}

posted on 2016-04-01 16:29  java外行  阅读(163)  评论(0)    收藏  举报

导航