实现了JSF下的生成EXCEL

开发 后端
本文介绍实现了JSF下的生成EXCEL,包括介绍例子用的是POI来生成Excel。

Excel这个大家几乎每天都用到的工具,为我们的工作带来了极大的方便。在现在的B/S系统中,特别是很多大型的办公系统中,大量的报表需要处理,导出EXCEL的功能就显得尤为重要了。导出Excel已经是相当成熟的技术了,但是在java中却不是一件容易的事。特别是在JSF架构的系统中,由于使用的人数和学习的资料都很少,实现导出Excel的功能也颇费周折。由于项目的需要,本人需要实现这样的功能,经过对大量代码的改造,实现了JSF下的生成EXCEL并在客户端实现下载的功能。下面的例子中,我用的是POI来生成Excel。Apache的Jakata项目的POI子项目,目标是处理ole2对象。 POI可以到http://www.apache.org/dyn/closer.cgi/jakarta/poi/下载。 编译好的jar主要有这样4个:poi包,poi Browser包,poi hdf包,poi hssf例程包。实际运行时,需要有poi包就可以了。

在下面的工具类中,我通过private static void downloadFile(String strfileName) 这个方法在生成EXCEL以后实现在客户端的下载。在这个类中,这个方法就是经过改造的JSF实现。不过这个工具类有个不足之处就是,传递给 downloadFile(String strfileName) 的文件名不支持中文,希望大家注意,也希望各位能给出解决办法。

  1. package mj.util.excel;  
  2.   import java.io.File;  
  3.   import java.io.FileInputStream;  
  4.   import java.io.FileOutputStream;  
  5.   import java.io.IOException;  
  6.   import java.util.List;  
  7.   import javax.faces.context.FacesContext;  
  8.   import javax.servlet.ServletContext;  
  9.   import javax.servlet.ServletOutputStream;  
  10.   import javax.servlet.http.HttpServletResponse;  
  11.   import org.apache.poi.hssf.usermodel.HSSFCell;  
  12.   import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
  13.   import org.apache.poi.hssf.usermodel.HSSFFont;  
  14.   import org.apache.poi.hssf.usermodel.HSSFRow;  
  15.   import org.apache.poi.hssf.usermodel.HSSFSheet;  
  16.   import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  17.   /**  
  18.   * 本工具类解决了java到处Excel,并同时实现了客户端下载 不足之处:下载方法传入的文件名不支持中文  
  19.   *  
  20.   * @author yongtree  
  21.   *  
  22.   */  
  23.   public class ExcelUtils {  
  24.   private static String sheetName = "data";  
  25.   private HSSFWorkbook wb;  
  26.   private HSSFSheet sheet;  
  27.   private HSSFRow row;  
  28.   private HSSFCell cell;  
  29.   private HSSFFont font;  
  30.   private HSSFCellStyle cellStyle;  
  31.   private FileOutputStream fileOut;  
  32.   public ExcelUtils() {  
  33.   wb = new HSSFWorkbook();  
  34.   }  
  35.   /**  
  36.   * @param excelName  
  37.   * excel名称。  
  38.   * @param list  
  39.   * 这个list里面存放的是对象数组。数组元素可以转化为字符串显示的。这个对象数组一般对应数据库里的几列。  
  40.   * @param firstRowValue  
  41.   */  
  42.   public void outputExcel(String excelName, List list, String[] firstRowValue) {  
  43.   try {  
  44.   this.createSheet(firstRowValue);  
  45.   this.setValueToRow(excelName, list);  
  46.   } catch (Exception ex) {  
  47.   System.out.print(ex);  
  48.   }  
  49.   // System.out.println("文件名是:" + excelName);  
  50.   downloadFile(excelName);  
  51.   }  
  52.   public void outputExcel(String excelName, List list) {  
  53.   try {  
  54.   this.setValueToRow(excelName, list);  
  55.   } catch (Exception e) {  
  56.   // TODO: handle exception  
  57.   }  
  58.   downloadFile(excelName);  
  59.   }  
  60.   private void setValueToRow(String excelName, List list) {  
  61.   // 获得JSF上下文环境  
  62.   FacesContext context = FacesContext.getCurrentInstance();  
  63.   // 获得ServletContext对象  
  64.   ServletContext servletContext = (ServletContext) context  
  65.   .getExternalContext().getContext();  
  66.   // 取得文件的绝对路径  
  67.   excelName = servletContext.getRealPath("/UploadFile") + "/" + excelName;  
  68.   System.out.println("生成文件的路径是:" + excelName);  
  69.   Object[] obj;  
  70.   try {  
  71.   for (int i = 0; i < list.size(); i++) {  
  72.   row = sheet.createRow(i + 1);  
  73.   obj = (Object[]) list.get(i);  
  74.   this.createCell(row, obj);  
  75.   }  
  76.   fileOut = new FileOutputStream(excelName);  
  77.   wb.write(fileOut);  
  78.   } catch (Exception ex) {  
  79.   System.out.print("生成报表有误:" + ex);  
  80.   } finally {  
  81.   try {  
  82.   fileOut.flush();  
  83.   fileOut.close();  
  84.   } catch (Exception e) {  
  85.   System.out.println("ExcelUtil.setValueToRow()");  
  86.   }  
  87.   }  
  88.   }  
  89.   private void createSheet(String[] firstRowValue) {  
  90.   try {  
  91.   sheet = wb.createSheet(ExcelUtils.sheetName);  
  92.   row = sheet.createRow(0);  
  93.   font = wb.createFont();  
  94.   font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
  95.   cellStyle = wb.createCellStyle();  
  96.   cellStyle.setFont(font);  
  97.   for (int i = 0; i < firstRowValue.length; i++) {  
  98.   cell = row.createCell((short) i);  
  99.   cell.setCellStyle(cellStyle);  
  100.   cell.setEncoding(HSSFCell.ENCODING_UTF_16);  
  101.   cell.setCellValue(firstRowValue[i]);  
  102.   }  
  103.   } catch (Exception ex) {  
  104.   System.out.print(ex);  
  105.   }  
  106.   }  
  107.   private void createCell(HSSFRow row, Object[] obj) {  
  108.   try {  
  109.   for (int i = 0; i < obj.length; i++) {  
  110.   cell = row.createCell((short) i);  
  111.   cell.setEncoding(HSSFCell.ENCODING_UTF_16);  
  112.   cell.setCellValue(obj[i].toString());  
  113.   }  
  114.   } catch (Exception ex) {  
  115.   System.out.print(ex);  
  116.   }  
  117.   }  
  118.   /**  
  119.   *   
  120.   * 功能说明:根据提供的文件名下载文件,不支持中文文件名  
  121.   *   
  122.   * 此方法由yongtree添加,实现文件生成后的下载  
  123.   *  
  124.   * @param strfileName  
  125.   * String  
  126.   * @return void  
  127.   */  
  128.   private static void downloadFile(String strfileName) {  
  129.   try {  
  130.   // 获得JSF上下文环境  
  131.   FacesContext context = FacesContext.getCurrentInstance();  
  132.   // 获得ServletContext对象  
  133.   ServletContext servletContext = (ServletContext) context  
  134.   .getExternalContext().getContext();  
  135.   // 取得文件的绝对路径  
  136.   String excelName = servletContext.getRealPath("/UploadFile") + "/"  
  137.   + strfileName;  
  138.   File exportFile = new File(excelName);  
  139.   HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext  
  140.   .getCurrentInstance().getExternalContext().getResponse();  
  141.   ServletOutputStream servletOutputStream = httpServletResponse 
  142.   .getOutputStream();  
  143.   httpServletResponse.setHeader("Content-disposition",  
  144.   "attachment; filename=" + strfileName);  
  145.   httpServletResponse.setContentLength((int) exportFile.length());  
  146.   httpServletResponse.setContentType("application/x-download");  
  147.   // httpServletResponse.setContentType("application/vnd.ms-excel");  
  148.   byte[] b = new byte[1024];  
  149.   int i = 0;  
  150.   FileInputStream fis = new java.io.FileInputStream(exportFile);  
  151.   while ((i = fis.read(b)) > 0) {  
  152.   servletOutputStream.write(b, 0, i);  
  153.   }  
  154.   } catch (IOException e) {  
  155.   e.printStackTrace();  
  156.   }  
  157.   FacesContext.getCurrentInstance().responseComplete();  
  158.   }  
  159.   } 

 


【编辑推荐】

  1. ESRI为什么选择JSF
  2. 从JSF的切入点控制JSF
  3. 编写JSF框架自定义UI组件
  4. JSF界面组件套装RichFaces 3.3.1 GA版发布
  5. 构建Ajax JSF事件驱动
责任编辑:佚名 来源: IT专家网
相关推荐

2009-06-25 11:11:25

控制JSF切入点

2009-06-23 11:23:13

DataTableJSF动态生成

2009-06-01 09:30:51

JSF2.0FaceletsAjax4JSF

2010-06-09 09:15:58

JSF 2Ajax组件

2009-09-23 17:56:45

JSF入门

2016-12-30 09:23:06

Linux代码文件

2009-06-17 16:56:45

JBoss服务器JSF实现

2021-03-26 07:09:15

Java技术pdfExcel

2009-06-22 16:42:26

JSF的工作方式

2009-06-25 13:03:48

JSF的UI组件

2009-06-29 13:22:19

JSF技术JSF组件

2009-06-23 13:21:26

JSF和Spring

2009-06-17 15:18:38

JSF与Spring

2009-06-26 13:48:57

G4JSFGWTJSF

2009-06-26 14:06:08

JSF基础框架

2009-06-22 15:35:31

JSF和Struts

2011-07-21 17:11:09

AjaxJSF

2009-06-22 14:07:46

JSF优势

2012-02-24 15:25:45

ibmdw

2009-06-25 11:21:36

JSF流行名词
点赞
收藏

51CTO技术栈公众号