Excel generation and download JSF implementation!

xiaoxiao2021-04-05  263

Package cn.ccb.elms.common;

import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import Javax.faces.context.facescontext; 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

/ ***

Function Description: Used for file download * @AuThor gjg * @version 1.0 2006-04-27 * / public class downloadutil {/ ** * with two-dimensional array format Generate Excel file * @Param DataList string [] [] * / public static void genericExcel (String DataList []) {string strfilename = ""; try {// Generate an Excel file and save SSConfig ssc = new ssconfig (); String strZFileName = "Query_" ssc.getSysTimeStamp (); // master file name strFileName = strZFileName ".xls"; // file full name FileOutputStream fileOut = new FileOutputStream (strFileName); HSSFWorkbook wb = new HSSFWorkbook ( ); HSSFCellStyle cs = wb.createCellStyle (); // format object HSSFFont fCol = wb.createFont (); // font object, header fCol.setFontHeightInPoints ((short) 10); fCol.setBoldweight (fCol.BOLDWEIGHT_BOLD); // Worksheet hssfsheet sheet = wb.createsheet (0 ""); wb.setsheetname (0, strzfilename, (short) 1); // First line, play head hssfrow rot = null;

INT nrownum = 0; for (int NH = 0; NH

/ ** * Generate Excel file * @deprecated * @param ColName_zh List * @param data List * / public static void genericExcel2 (List ColName_zh, List data) {String strFileName = "" List by Format; try {if (ColName_zh == NULL || DATA == NULL) {throw new exception ("" The number of head columns or data columns is empty! ");

/ / Generate an Excel file and save SSCONFIG SSC = new ssconfig () on the server (); string strafilename = "query_" ssc.getsysTimeStamp (); // master file name strfilename = strzfilename ".xls"; // file full name FileOutputStream fileOut = new FileOutputStream (strFileName); HSSFWorkbook wb = new HSSFWorkbook (); HSSFCellStyle cs = wb.createCellStyle (); // format object HSSFFont fCol = wb.createFont (); // font object, header fCol.setFontHeightInPoints ( (Short); fcol.setboldweight (fcol.boldweight_bold); // Worksheet hssfsheet sheet = wb.createsheet (0 ""); wb.setsheetname (0, strzfilename, (short) 1); // First Row, placed head hssfrow row = null; row = sheet.createrow ((Short) 0); int NY = 0; for (Iterator iter = colname_zh.iterator (); itute.hasnext ();) {hssfcell cell = row .createcell ((Short) NY); cell.setencoding (hssfcell.encoding_utf_16); cs.setfont (fcol); cs.setalignment (hssfcellstyle.align_center); cell.setcellstyle (CELL.SETCELLVALUE (IT Er.Next () "" "NY ;} //} //} The starting line of the data is // y represents the data from the first column start populating int NX = 1; // Data from the first row start for (Iterator Item = data.iterator (); item.hasnext (); {ny = 0; row = sheet.createrow (short) NX); arraylist al2 = (arraylist) tries ney ney (); for (Iterator ITER2 = Al2) .iterator (); it2.hasnext ();) {hssfcell cell = row.createcell (short) NY); cell.setencoding (hssfcell.encoding_utf_16); // cs.setfont (fcol); cs.setAlignment (hssfcellstyle. Align_center); // Cell.SetCellStyle (CS);

Cell.setCellValue (iter2.next () ""); NY ;} NX ;} wb.write (fileout); fileout.close ();} catch (exception e) {E.PrintStackTrace ();} // File name download file downloadfile (strfilename);} / ** *

function description: Download file according to the file name provided * @Param strfilename string * @return void * / private static void Downloadfile (String strfileName) {try {File exportFile = new File (strfileName); 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 "); byte [] b = new byte [1024] ; INT i = 0; fileinputstream FII = new java.io.fileinputStream (exportfile); while ((i = fis.read (b))> 0) {servletOutputStream.write (b, 0, i);}} catch (ioException e) {E.PrintStackTrace ();} facescontext.getCurrentInstance (). ResponseComplete ();}

Public static void main (string [] args) {/ genericexcel () usage string s [] [] = new string [] [] {{"column 1", "column 2", "column 3", "column 4 "}, {" A "," 22 "," 33 "," 44 "}}; genericExcel (s);

// genericexcel2 () Usage // Al represents Head ArrayList Al = New ArrayList (); Al.Add ("A"); Al.Add ("B"); Al.Add ("c"); // AL2 Representing multi-line data arraylist al2 = new arraylist (); // AL3 represents a row of data arraylist al3 = new arraylist (); al3.add ("1"); al3.add ("2"); al3.add ("3 "); Al2.add (al3); al3 = new arrayList (); // must be New A new ArrayList object Al3.Add (" 11 "); al3.add (" 22 "); al3.add ( "33"); al2.add (al3); genericexcel2 (al, al2);}}

转载请注明原文地址:https://www.9cbs.com/read-132241.html

New Post(0)