In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to generate EXCEL under JSF. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Excel, a tool that we use almost every day, has brought great convenience to our work. In the current Bhand S system, especially in many large-scale office systems, a large number of reports need to be processed, so the function of exporting EXCEL is particularly important. Exporting Excel is a fairly mature technology, but it is not an easy task in java. Especially in the system of JSF architecture, because the number of users and learning materials are very small, it is also quite difficult to achieve the function of exporting Excel. Due to the needs of the project, I need to achieve such a function, after the transformation of a large number of code, to achieve the generation of EXCEL under JSF and download in the client. In the following example, I use POI to generate Excel. Apache's Jakata project is a POI sub-project with the goal of working with ole2 objects. POI can be downloaded from http://www.apache.org/dyn/closer.cgi/jakarta/poi/. There are mainly four compiled jar: poi package, poi Browser package, poi hdf package, poi hssf routine package. When actually running, you need to have a poi package.
In the following utility class, I use the private static void downloadFile (String strfileName) method to download on the client side after generating the EXCEL. In this class, this method is the modified JSF implementation. However, this tool class has a deficiency is that the file name passed to downloadFile (String strfileName) does not support Chinese, I hope you can pay attention, but also hope you can give a solution.
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; / * this utility class solves the Excel everywhere in java, and at the same time implements the shortcomings of client download: the file name passed in by the download method does not support Chinese * * @ 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 name. * @ param list * this list contains an array of objects. Array elements can be converted to string display. This array of objects generally corresponds to several columns in the database. * @ 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 ("file name is:" + 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) {/ / get the JSF context FacesContext context = FacesContext.getCurrentInstance (); / / get the ServletContext object ServletContext servletContext = (ServletContext) context .getExternalContext (). GetContext (); / / get the absolute path to the file excelName = servletContext.getRealPath ("/ UploadFile") + "/" + excelName System.out.println ("the path to generate files is:" + 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 ();}} Thank you for reading! This is the end of this article on "how to generate EXCEL under JSF". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.