使用poi导出excel

文件:poi 2.5jar

因遇到导出中文乱码问题,而3.xjar又去掉了cell.setEncoding(),所以就换成了2.5jar

代码段:

/**
 *
 * 功能描述: Excel操纵类,可以根据Excel模板来生成Excel对象<br>
  * Copyright: Copyright (c) 2005<br>
 */
public class ExportExcelUtil {
 private String title = "";
 private String exportFileName = "";
 private List<String> headers;
 private List<List> data;
 public String getTitle() {
  return title;
 }
public void setTitle(String title) {
  this.title = title;
 }
 public String getExportFileName() {
  return exportFileName;
 }
 public void setExportFileName(String exportFileName) {
  this.exportFileName = exportFileName;
 }
 public List<String> getHeaders() {
  return headers;
 }
public void setHeaders(List<String> headers) {
  this.headers = headers;
 }
public List<List> getData() {
  return data;
 }
public void setData(List<List> data) {
  this.data = data;
 }

 public void exportExcel(HttpServletResponse response){

    // 声明一个工作薄
    HSSFWorkbook workbook = new HSSFWorkbook(); 
    // 生成一个表格
    HSSFSheet sheet = workbook.createSheet(title); 
    // 设置表格默认列宽度为20个字节
    sheet.setDefaultColumnWidth((short)20);  
    // 生成一个样式
    HSSFCellStyle style = workbook.createCellStyle(); 
    // 设置这些样式
    style.setFillForegroundColor(HSSFColor.GOLD.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.BLACK.index); 
    // font.setFontHeightInPoints((short) 12);
    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 
    // 把字体应用到当前的样式
    style.setFont(font);  
    // 指定当单元格内容显示不下时自动换行
    style.setWrapText(true);  
  
 /*
  *
  * 以下可以用于设置导出的数据的样式
  *  // 生成并设置另一个样式 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); 
    OutputStream out=null;
    try{
    for (int i = 0; i < headers.size(); i++) {  
        HSSFCell cell = row.createCell((short)i); 
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellStyle(style); 
        HSSFRichTextString text = new HSSFRichTextString((String)headers.get(i)); 
        cell.setCellValue(text.toString()); 
    } 
    // 遍历集合数据,产生数据行
    if(data != null){  
        for(int rowIndex=0;rowIndex<data.size();rowIndex++){ 
            row = sheet.createRow(rowIndex+1);
            List rowData=data.get(rowIndex);
            for(int cellIndex=0;cellIndex<rowData.size();cellIndex++){ 
                HSSFCell cell = row.createCell((short)cellIndex); 
                cell.setEncoding(HSSFCell.ENCODING_UTF_16);
                String cellStr=rowData.get(cellIndex)+"";
                HSSFRichTextString richString = new HSSFRichTextString(cellStr == null ? "" :cellStr);  
                cell.setCellValue(richString.toString()); 
            }  
        }    
    }  
    response.setContentType("octets/stream"); 
    response.addHeader("Content-Disposition","attachment;filename="+new String(exportFileName.getBytes("GBK"),"ISO8859-1")); 
    out =response.getOutputStream(); 
    workbook.write(out);
    }catch(Exception e){
     e.printStackTrace(); 
    }
    finally{
     try{
       out.close(); 
     }catch(Exception e){
      e.printStackTrace(); 
     }
    } 
}
}

调用:

  ExportExcelUtil excel = new ExportExcelUtil();
  List list = 数据;
  List<String> heads=new ArrayList();
  heads.add("name");
  heads.add("pass")
  excel.setHeaders(heads);
  List<List> data = new ArrayList<List>();
  User user =null;
  //对查询出来的数据进行处理
  for(int i=0;i<list.size();i++){
   List dList=new ArrayList();
      user=(User)list.get(i);
   //用户名
   dList.add(user.getName());
   //密码
   dList.add(user.getPass());
   data.add(dList);
  }
  excel.setData(data);
  excel.setExportFileName("用户.xls");
  excel.setTitle("用户");
  HttpServletResponse response = this.getResponse();
  excel.exportExcel(response);

你可能感兴趣的:(导出Excel,Poi导出Excel)