温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何实现JSF下的生成EXCEL

发布时间:2021-12-01 09:29:53 来源:亿速云 阅读:123 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关如何实现JSF下的生成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) 的文件名不支持中文,希望大家注意,也希望各位能给出解决办法。

   package mj.util.excel;  import java.io.File;  import java.io.FileInputStream;  import java.io.FileOutputStream;  import java.io.IOException;  import java.util.List;  import javax.faces.context.FacesContext;  import javax.servlet.ServletContext;  import javax.servlet.ServletOutputStream;  import javax.servlet.http.HttpServletResponse;  import org.apache.poi.hssf.usermodel.HSSFCell;  import org.apache.poi.hssf.usermodel.HSSFCellStyle;  import org.apache.poi.hssf.usermodel.HSSFFont;  import org.apache.poi.hssf.usermodel.HSSFRow;  import org.apache.poi.hssf.usermodel.HSSFSheet;  import org.apache.poi.hssf.usermodel.HSSFWorkbook;  /**  * 本工具类解决了java到处Excel,并同时实现了客户端下载 不足之处:下载方法传入的文件名不支持中文  *  * @author yongtree  *  */  public class ExcelUtils {  private static String sheetName = "data";  private HSSFWorkbook wb;  private HSSFSheet sheet;  private HSSFRow row;  private HSSFCell cell;  private HSSFFont font;  private HSSFCellStyle cellStyle;  private FileOutputStream fileOut;  public ExcelUtils() {  wb = new HSSFWorkbook();  }  /**  * @param excelName  * excel名称。  * @param list  * 这个list里面存放的是对象数组。数组元素可以转化为字符串显示的。这个对象数组一般对应数据库里的几列。  * @param firstRowValue  */  public void outputExcel(String excelName, List list, String[] firstRowValue) {  try {  this.createSheet(firstRowValue);  this.setValueToRow(excelName, list);  } catch (Exception ex) {  System.out.print(ex);  }  // System.out.println("文件名是:" + excelName);  downloadFile(excelName);  }  public void outputExcel(String excelName, List list) {  try {  this.setValueToRow(excelName, list);  } catch (Exception e) {  // TODO: handle exception  }  downloadFile(excelName);  }  private void setValueToRow(String excelName, List list) {  // 获得JSF上下文环境  FacesContext context = FacesContext.getCurrentInstance();  // 获得ServletContext对象  ServletContext servletContext = (ServletContext) context  .getExternalContext().getContext();  // 取得文件的绝对路径  excelName = servletContext.getRealPath("/UploadFile") + "/" + excelName;  System.out.println("生成文件的路径是:" + excelName);  Object[] obj;  try {  for (int i = 0; i < list.size(); i++) {  row = sheet.createRow(i + 1);  obj = (Object[]) list.get(i);  this.createCell(row, obj);  }  fileOut = new FileOutputStream(excelName);  wb.write(fileOut);  } catch (Exception ex) {  System.out.print("生成报表有误:" + ex);  } finally {  try {  fileOut.flush();  fileOut.close();  } catch (Exception e) {  System.out.println("ExcelUtil.setValueToRow()");  }  }  }  private void createSheet(String[] firstRowValue) {  try {  sheet = wb.createSheet(ExcelUtils.sheetName);  row = sheet.createRow(0);  font = wb.createFont();  font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  cellStyle = wb.createCellStyle();  cellStyle.setFont(font);  for (int i = 0; i < firstRowValue.length; i++) {  cell = row.createCell((short) i);  cell.setCellStyle(cellStyle);  cell.setEncoding(HSSFCell.ENCODING_UTF_16);  cell.setCellValue(firstRowValue[i]);  }  } catch (Exception ex) {  System.out.print(ex);  }  }  private void createCell(HSSFRow row, Object[] obj) {  try {  for (int i = 0; i < obj.length; i++) {  cell = row.createCell((short) i);  cell.setEncoding(HSSFCell.ENCODING_UTF_16);  cell.setCellValue(obj[i].toString());  }  } catch (Exception ex) {  System.out.print(ex);  }  }  /**  *   * 功能说明:根据提供的文件名下载文件,不支持中文文件名  *   * 此方法由yongtree添加,实现文件生成后的下载  *  * @param strfileName  * String  * @return void  */  private static void downloadFile(String strfileName) {  try {  // 获得JSF上下文环境  FacesContext context = FacesContext.getCurrentInstance();  // 获得ServletContext对象  ServletContext servletContext = (ServletContext) context  .getExternalContext().getContext();  // 取得文件的绝对路径  String excelName = servletContext.getRealPath("/UploadFile") + "/"  + strfileName;  File exportFile = new File(excelName);  HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext  .getCurrentInstance().getExternalContext().getResponse();  ServletOutputStream servletOutputStream = httpServletResponse .getOutputStream();  httpServletResponse.setHeader("Content-disposition",  "attachment; filename=" + strfileName);  httpServletResponse.setContentLength((int) exportFile.length());  httpServletResponse.setContentType("application/x-download");  // httpServletResponse.setContentType("application/vnd.ms-excel");  byte[] b = new byte[1024];  int i = 0;  FileInputStream fis = new java.io.FileInputStream(exportFile);  while ((i = fis.read(b)) > 0) {  servletOutputStream.write(b, 0, i);  }  } catch (IOException e) {  e.printStackTrace();  }  FacesContext.getCurrentInstance().responseComplete();  }  }

感谢各位的阅读!关于“如何实现JSF下的生成EXCEL”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI