Data export/download, data export and download

Source: Internet
Author: User

Data export/download, data export and download

Tool class:

Import java. io. IOException; import java.net. URLEncoder; import sun. misc. BASE64Encoder; public class FileUtils, encode the attachment name ** @ param filename * download file name * @ param agent * client browser * @ return encoded Download Attachment name * @ throws IOException */public static String encodeDownloadFilename (string filename, string agent) throws IOException {if (agent. contains ("Firefox") {// Firefox filename = "=? UTF-8? B? "+ New BASE64Encoder (). encode (filename. getBytes (" UTF-8 ") + "? = "; Filename = filename. replaceAll ("\ r \ n", "");} else {// IE and other browsers filename = URLEncoder. encode (filename, "UTF-8"); filename = filename. replace ("+", "");} return filename ;}}

 

 

 

/*** @ Description: Query all partition data. Use the Attachment Form to download * @ return * @ throws Exception **/@ Action ("subareaAction_exportXls") public String exportXls () throws Exception {List <SubArea> list = subAreaService. findAll (); // write data to the excel file HSSFWorkbook workbook = new HSSFWorkbook () through POI; // blank excel file // create the tab HSSFSheet sheet = workbook. createSheet ("partition data on the first page"); // create a row // create a header row HSSFRow headRow = sheet. createRow (0); // create a cell headRow. createCell (0 ). setCellValue ("Partition Number"); headRow. createCell (1 ). setCellValue ("keyword"); headRow. createCell (2 ). setCellValue ("secondary keyword"); headRow. createCell (3 ). setCellValue ("START number"); headRow. createCell (4 ). setCellValue ("Stop number"); headRow. createCell (5 ). setCellValue ("province city"); // create a data row for (SubArea subArea: list) {HSSFRow dataRow = sheet. createRow (sheet. getLastRowNum () + 1); dataRow. createCell (0 ). setCellValue (subArea. getId (); dataRow. createCell (1 ). setCellValue (subArea. getKeyWords (); dataRow. createCell (2 ). setCellValue (subArea. getAssistKeyWords (); dataRow. createCell (3 ). setCellValue (subArea. getStartNum (); dataRow. createCell (4 ). setCellValue (subArea. getEndNum (); dataRow. createCell (5 ). setCellValue (subArea. getArea (). getName ();} // solve the Chinese problem of the file name (import tool class): String fileName = "partition data .xls"; HttpServletRequest request = ServletActionContext. getRequest (); String agent = request. getHeader ("User-Agent"); fileName = FileUtils. encodeDownloadFilename (fileName, agent); // File Download: one stream and two headers (File MIME type; file opening method (inline opened inside the browser; attachment download -- attachment) // from the web. find the file type corresponding to xls in xml, for example, search for xls HttpServletResponse response = ServletActionContext. getResponse (); response. setHeader ("content-type", "application/vnd. ms-excel "); response. setHeader ("content-disposition", "attachment; fileName =" + fileName); workbook. write (response. getOutputStream (); return NONE ;}

 

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.