POI export an Excel instance and poi export an excel file

Source: Internet
Author: User

POI export an Excel instance and poi export an excel file

1. Add the field filename to the action.

private String filename;public String getFilename() {return filename;}public void setFilename(String filename) {this.filename = filename;}


2. Write a public method in the action that returns InputStream. The name starts with get:Getimpactactlog.

</Pre> <p> <span style = "font-size: 18px"> <strong> <span style = "color: # ff0000; background-color: rgb (255,255,255) "> </span> </strong> </span> </p> <span style =" font-size: 18px "> <strong> <span style =" color: # ff0000; background-color: rgb (255,255,255) "> <span style =" white-space: pre "> </span> </strong> </span> </p> <pre code_snippet_id =" 610206 "snippet_file_name =" blog_20150302_2_7951902 "name =" code" class = "java">/***** Method Name: getimpactactlog * method Description: (obtains the export Knowledge Operation Log) * Creator: liyafei * modification time: 11:35:13 * parameter @ return parameter description * return type InputStream return type * @ throws */public InputStream getimpactactlog () {// Step 2: Create an excel table, encapsulate data to excelHSSFWorkbook workbook = new HSSFWorkbook (); HSSFSheet sheet = workbook. createSheet (); HSSFCellStyle setBorder = workbook. createCellStyle (); // set the border: setBorder. setBorderBottom (HSSFCellStyle. BORDER_THIN); // setBorder. setBorderLeft (HSSFCellStyle. BORDER_THIN); // setBorder on the left border. setBorderTop (HSSFCellStyle. BORDER_THIN); // setBorder. setBorderRight (HSSFCellStyle. BORDER_THIN); // setBorder on the right border. setWrapText (true); // set automatic line feed HSSFRow row = sheet. createRow (0); // create the first HSSFCell cell = null; // the first cell = row. createCell (0); // create a cell in column 1st. setCellValue ("Serial Number"); cell = row. createCell (1); // create a cell in Column 2nd. setCellValue ("Knowledge title"); cell = row. createCell (2); // create a cell in column 3rd. setCellValue ("Operation user"); cell = row. createCell (3); // create a cell in column 4th. setCellValue ("Operation Type"); cell = row. createCell (4); // create a cell in column 5th. setCellValue ("Operation Time"); cell = row. createCell (5); // create a cell in column 6th. setCellValue ("operation result"); cell = row. createCell (6); // create a cell in column 7th. setCellValue ("Detailed description"); // query the data List <HashMap <String, String> data = knowledgeActLogService. getShowList (model); for (int I = 0; I <data. size (); I ++) {HashMap <String, String> map = data. get (I); row = sheet. createRow (I + 1); // create row I + 1 cell = row. createCell (0); // create the first cell column. setCellValue (I + 1); cell = row. createCell (1); // create the second cell column. setCellValue (map. get ("K_NAME"); cell = row. createCell (2); cell. setCellValue (map. get ("STAFF_NAME"); cell = row. createCell (3); // create the third cell column. setCellValue (map. get ("ACTTYPE_NAME"); cell = row. createCell (4); SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); String acttime = format. format (map. get ("ACT_TIME"); if (acttime. length ()> 19) {acttime = acttime. substring (0, 19);} cell. setCellValue (acttime); cell = row. createCell (5); // create Object ob = map in the fourth column. get ("ISSUCCESS"); String issuccess = ob. toString (). trim (); if (issuccess. equals ("1") {cell. setCellValue ("successful");} else {cell. setCellValue ("failed");} cell = row. createCell (6); cell. setCellValue (map. get ("ACT_DESC");} // adjust the column width for (int I = 0; I <row. getLastCellNum (); I ++) {sheet. autoSizeColumn (short) I);} // Step 3: Write the output stream ByteArrayOutputStream baos = new ByteArrayOutputStream (); try {workbook. write (baos); // write} catch (IOException e) {e. printStackTrace ();} byte [] ba = baos. toByteArray (); ByteArrayInputStream bais = new ByteArrayInputStream (ba); return bais ;}


3. Write a method in action to process the export: getImport

/***** Method Name: getImport * method Description: (export Knowledge Operation Log) * created by: liyafei * modification time: 10:47:08 * parameter @ return * parameter @ throws Exception parameter description * return type String return type * @ throws */public String getImport () throws Exception {this. setFilename ("Knowledge Operation Log .xls"); filename = URLEncoder. encode (filename, "UTF-8"); return "import ";}


4. Configure the action file

 <result name="import" type="stream"><param name="contentType">application/vnd.ms-excel</param><param name="contentDisposition">attachment;filename=${filename}</param><param name="inputName">importActLog</param></result>



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.