實(shí)現(xiàn)了JSF下的生成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) 的文件名不支持中文,希望大家注意,也希望各位能給出解決辦法。
- 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,并同時(shí)實(shí)現(xiàn)了客戶端下載 不足之處:下載方法傳入的文件名不支持中文
- *
- * @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
- * 這個(gè)list里面存放的是對象數(shù)組。數(shù)組元素可以轉(zhuǎn)化為字符串顯示的。這個(gè)對象數(shù)組一般對應(yīng)數(shù)據(jù)庫里的幾列。
- * @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上下文環(huán)境
- 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("生成報(bào)表有誤:" + 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);
- }
- }
- /**
- *
- * 功能說明:根據(jù)提供的文件名下載文件,不支持中文文件名
- *
- * 此方法由yongtree添加,實(shí)現(xiàn)文件生成后的下載
- *
- * @param strfileName
- * String
- * @return void
- */
- private static void downloadFile(String strfileName) {
- try {
- // 獲得JSF上下文環(huán)境
- 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();
- }
- }
【編輯推薦】