自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

實(shí)現(xiàn)了JSF下的生成EXCEL

開發(fā) 后端
本文介紹實(shí)現(xiàn)了JSF下的生成EXCEL,包括介紹例子用的是POI來生成Excel。

Excel這個(gè)大家?guī)缀趺刻於加玫降墓ぞ?,為我們的工作帶來了極大的方便。在現(xiàn)在的B/S系統(tǒng)中,特別是很多大型的辦公系統(tǒng)中,大量的報(bào)表需要處理,導(dǎo)出EXCEL的功能就顯得尤為重要了。導(dǎo)出Excel已經(jīng)是相當(dāng)成熟的技術(shù)了,但是在java中卻不是一件容易的事。特別是在JSF架構(gòu)的系統(tǒng)中,由于使用的人數(shù)和學(xué)習(xí)的資料都很少,實(shí)現(xiàn)導(dǎo)出Excel的功能也頗費(fèi)周折。由于項(xiàng)目的需要,本人需要實(shí)現(xiàn)這樣的功能,經(jīng)過對大量代碼的改造,實(shí)現(xiàn)了JSF下的生成EXCEL并在客戶端實(shí)現(xiàn)下載的功能。下面的例子中,我用的是POI來生成Excel。Apache的Jakata項(xiàng)目的POI子項(xiàng)目,目標(biāo)是處理ole2對象。 POI可以到http://www.apache.org/dyn/closer.cgi/jakarta/poi/下載。 編譯好的jar主要有這樣4個(gè):poi包,poi Browser包,poi hdf包,poi hssf例程包。實(shí)際運(yùn)行時(shí),需要有poi包就可以了。

在下面的工具類中,我通過private static void downloadFile(String strfileName) 這個(gè)方法在生成EXCEL以后實(shí)現(xiàn)在客戶端的下載。在這個(gè)類中,這個(gè)方法就是經(jīng)過改造的JSF實(shí)現(xiàn)。不過這個(gè)工具類有個(gè)不足之處就是,傳遞給 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,并同時(shí)實(shí)現(xiàn)了客戶端下載 不足之處:下載方法傳入的文件名不支持中文  
  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.   * 這個(gè)list里面存放的是對象數(shù)組。數(shù)組元素可以轉(zhuǎn)化為字符串顯示的。這個(gè)對象數(shù)組一般對應(yīng)數(shù)據(jù)庫里的幾列。  
  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上下文環(huán)境  
  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("生成報(bào)表有誤:" + 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.   * 功能說明:根據(jù)提供的文件名下載文件,不支持中文文件名  
  121.   *   
  122.   * 此方法由yongtree添加,實(shí)現(xiàn)文件生成后的下載  
  123.   *  
  124.   * @param strfileName  
  125.   * String  
  126.   * @return void  
  127.   */  
  128.   private static void downloadFile(String strfileName) {  
  129.   try {  
  130.   // 獲得JSF上下文環(huán)境  
  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的切入點(diǎn)控制JSF
  3. 編寫JSF框架自定義UI組件
  4. JSF界面組件套裝RichFaces 3.3.1 GA版發(fā)布
  5. 構(gòu)建Ajax JSF事件驅(qū)動(dòng)
責(zé)任編輯:佚名 來源: IT專家網(wǎng)
相關(guān)推薦

2009-06-25 11:11:25

控制JSF切入點(diǎn)

2009-06-23 11:23:13

DataTableJSF動(dòng)態(tài)生成

2009-06-01 09:30:51

JSF2.0FaceletsAjax4JSF

2009-09-23 17:56:45

JSF入門

2010-06-09 09:15:58

JSF 2Ajax組件

2009-06-22 16:42:26

JSF的工作方式

2009-06-25 13:03:48

JSF的UI組件

2009-06-23 13:21:26

JSF和Spring

2009-06-17 15:18:38

JSF與Spring

2009-06-29 13:22:19

JSF技術(shù)JSF組件

2009-06-26 13:48:57

G4JSFGWTJSF

2009-06-17 16:56:45

JBoss服務(wù)器JSF實(shí)現(xiàn)

2016-12-30 09:23:06

Linux代碼文件

2009-06-26 14:06:08

JSF基礎(chǔ)框架

2021-03-26 07:09:15

Java技術(shù)pdfExcel

2011-07-21 17:11:09

AjaxJSF

2009-06-22 15:35:31

JSF和Struts

2009-06-22 14:07:46

JSF優(yōu)勢

2012-02-24 15:25:45

ibmdw

2009-06-25 11:21:36

JSF流行名詞
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號