Javaweb export Excel file and eject the download box _java

Source: Internet
Author: User
Tags java web

First, the introduction

Reports are often involved in Java Web development, and the most recent projects need to be implemented to display data from a database as a table and to implement the ability to export to Excel files.

Second, the related jar bag

Using POI can be a good solution to Excel import and export problems, poi download Address:
Poi-3.6-20091214.jar

Third, key code

First import the jar package above.
When you build Excel, the general data source is a list, and the following code that generates Excel format is posted:

/** * below for generating Excel operations *//1. Create a workbook, corresponding to an Excel file Hssfworkbook wb =
      New Hssfworkbook ();
      2. Add a sheet to the workbook, corresponding to a sheet hssfsheet sheet = wb.createsheet ("XXX table") in Excel;
      3. Add the No. 0 row of the header in the sheet, the old version poi the number of columns in the Excel row short Hssfrow row = Sheet.createrow ((int) 0);
      4. Create the cell, set the value header, set the header center Hssfcellstyle style = Wb.createcellstyle ();

      Center format style.setalignment (hssfcellstyle.align_center);
      Set Header Hssfcell cell = Row.createcell (0);
      Cell.setcellvalue ("Table Head 1");

      Cell.setcellstyle (style);
      Cell = Row.createcell (1);
      Cell.setcellvalue ("Table Head 2");

      Cell.setcellstyle (style);
      Cell = Row.createcell (2);
      Cell.setcellvalue ("Table Head 3");

      Cell.setcellstyle (style);
      Cell = Row.createcell (3);
      Cell.setcellvalue ("Table Head 4");

      Cell.setcellstyle (style);
      Cell = Row.createcell (4);
      Cell.setcellvalue ("Table Head 5");

Cell.setcellstyle (style); 

To write data to Excel after you have generated Excel format:

Loop writes data to Excel for
      (int i = 0; i < lists.size (); i++) {
        row = Sheet.createrow ((int) i + 1);
        List list= Lists.get (i);
        Create a cell, set the value
        Row.createcell (0). Setcellvalue (List.getxxx ());
        Row.createcell (1). Setcellvalue (List.getxxx ());
        Row.createcell (2). Setcellvalue (List.getxxx ());
        Row.createcell (3). Setcellvalue (List.getxxx ());
        Row.createcell (4). Setcellvalue (List.getxxx ());
      }

Excel is then generated to stream output.
* Do not eject the download box

FileOutputStream out =new fileoutputstream ("E:/xxx.xls");
Wb.write (out); 
Out.close ();

* Pop-up Download box

String fileName = "XXX table";
      Bytearrayoutputstream OS = new Bytearrayoutputstream ();
      Wb.write (OS);
      byte[] content = Os.tobytearray ();
      InputStream is = new Bytearrayinputstream (content);
      Set response parameters, you can open the download page res.reset ();
      Res.setcontenttype ("Application/vnd.ms-excel;charset=utf-8"); Res.setheader ("Content-disposition", "attachment;filename=" + new String (filename + ". xls"). GetBytes (), "iso-88
      59-1 "));
      Servletoutputstream out = Res.getoutputstream ();
      Bufferedinputstream bis = null;

      Bufferedoutputstream BOS = NULL;
        try {bis = new Bufferedinputstream (IS);
        BOS = new Bufferedoutputstream (out);
        byte[] buff = new byte[2048];
        int bytesread;
        Simple Read/write loop.
        while ( -1!= (bytesread = bis.read (buff, 0, buff.length))) {bos.write (buff, 0, bytesread); } catch (Exception e) {//Todo:handle Exception E.printstacktracE ();
        finally {if (bis!= null) bis.close ();
      if (BOS!= null) bos.close ();

 }

When you are done, you can jump to another page.
At the same time poi can also upload the Excel display in the Web page, this another article summary, please look forward to!

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.