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 ;}