Java Web implements the excel template download function (1)

Source: Internet
Author: User
Recently, most of the company's main tasks are making bank reports. Here, I would like to summarize the Java Web Implementation of the download of excel report templates. Paste the core code as follows: publicStringdownload () throwsException {request, the respose object can also use other

Recently, most of the company's main tasks are making bank reports. Here, I would like to summarize the downloading of java web excel report templates.

Paste the core Code as follows:

Public String download () throws Exception {// request, respose object can also be obtained in other ways, Catalog is a custom class HttpServletRequest request = Catalog. getRequest (); HttpServletResponse response = Catalog. getResponse (); final String comp_id = (String) request. getSession (). getAttribute (Constant. COMP_ID); HSSFWorkbook workbook = new HSSFWorkbook (); loadStyle (workbook); HSSFSheet JobSheet = workbook. createSheet ("jobDetials"); produceJobSheet (workbook, JobSheet); String filename = comp_id + "_" + "dynamictable_report.xls"; // output runExcelFileExport (response, workbook, filename); return "" ;}// your private void produceJobSheet (HSSFWorkbook workbook, HSSFSheet sheet) throws Exception {HSSFRow row = sheet. createRow (0); // Create the hard columns of headerHSSFCell cell_emp = row. createCell (0); cell_emp.setCellStyle (arial_bold_style); cell_emp.setCellValue ("EmployeeId"); sheet. setColumnWidth (0, 6000); HSSFCell cell_name = row. createCell (1); cell_name.setCellStyle (arial_bold_style); cell_name.setCellValue ("EmployeeName"); sheet. setColumnWidth (1, 6000); HSSFCell cell_gradeId = row. createCell (2); cell_gradeId.setCellStyle (arial_bold_style); cell_gradeId.setCellValue ("GlobleEEID"); sheet. setColumnWidth (2, 6000) ;}// specify private static void runExcelFileExport (HttpServletResponse response, HSSFWorkbook wb, String filename) {try {ServletOutputStream sos = response. getOutputStream (); ByteArrayOutputStream buffer = new ByteArrayOutputStream (); wb. write (buffer); response. setContentType ("application/vnd. ms-excel "); response. setContentLength (buffer. size (); response. setHeader ("Content-Disposition", "attachment; filename =" + filename); response. setHeader ("Pragma", "public"); response. setHeader ("Cache-Control", "max-age = 0"); sos. write (buffer. toByteArray (); buffer. flush (); sos. flush ();} catch (IOException e) {e. printStackTrace ();}}


OK. The three main methods at this location are complete.

If you want to implement web page functions, of course you need a jsp page, a button, and a button to call the background download method. Therefore, put these three methods in an action, controller, or servlet.
Ps:

1.
You can use the getOutputStream () method defined by HttpResponse to obtain the instance of ServletOutputStream,
In this way, you can use the ServletOutputStream. write method to write the returned page content to the output stream or write the content of the downloaded file.

2. Why does it appear as a downloaded object?

Response. setHeader ("Content-Disposition", "attachment; filename =" + filename); the core is the attchment. Make sure that the value of this parameter appears after you click the button on the web page. The content written to the output stream will be downloaded.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.