POI匯出Excel執行個體,poi匯出excel

來源:互聯網
上載者:User

POI匯出Excel執行個體,poi匯出excel

1、在action中添加欄位:filename

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


2、在action中寫個返回 InputStream 的public方法,名稱以get開頭:getImportActLog。

</pre><p><span style="font-size:18px"><strong><span style="color:#ff0000; background-color:rgb(255,255,255)"></span></strong></span></p><p><span style="font-size:18px"><strong><span style="color:#ff0000; background-color:rgb(255,255,255)"><span style="white-space:pre"></span></span></strong></span></p><pre code_snippet_id="610206" snippet_file_name="blog_20150302_2_7951902" name="code" class="java">/** * * 方法名: getImportActLog* 方法描述: (擷取匯出知識動作記錄)* 建立人:liyafei * 修改時間:2015-3-2 上午11:35:13 * 參數 @return 參數說明* 傳回型別 InputStream 傳回型別* @throws */public InputStream getImportActLog(){// 第二步:構建excel表格,封裝資料到excelHSSFWorkbook workbook = new HSSFWorkbook();HSSFSheet sheet = workbook.createSheet();HSSFCellStyle setBorder = workbook.createCellStyle();//設定邊框:setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下邊框setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左邊框setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN);//上邊框setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN);//右邊框setBorder.setWrapText(true);//設定自動換行HSSFRow row = sheet.createRow(0);// 建立第一行HSSFCell cell = null;// 第一列cell = row.createCell(0); // 建立第1列cell.setCellValue("序號");cell = row.createCell(1); // 建立第2列cell.setCellValue("知識標題");cell = row.createCell(2); // 建立第3列cell.setCellValue("操作使用者");cell = row.createCell(3); // 建立第4列cell.setCellValue("操作類型");cell = row.createCell(4); // 建立第5列cell.setCellValue("操作時間");cell = row.createCell(5); // 建立第6列cell.setCellValue("操作結果");cell = row.createCell(6); // 建立第7列cell.setCellValue("詳細描述");//查詢資料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);// 建立第i+1行cell = row.createCell(0);// 建立第一列cell.setCellValue(i + 1);cell = row.createCell(1);// 建立第二列cell.setCellValue(map.get("K_NAME"));cell = row.createCell(2);cell.setCellValue(map.get("STAFF_NAME"));cell = row.createCell(3);// 建立第三列cell.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);// 建立第四列Object ob = map.get("ISSUCCESS");String issuccess = ob.toString().trim();if(issuccess.equals("1")){cell.setCellValue("成功");}else {cell.setCellValue("失敗");}cell = row.createCell(6);cell.setCellValue(map.get("ACT_DESC"));}//調整列寬for (int i = 0 ;i<row.getLastCellNum();i++){sheet.autoSizeColumn((short)i); }// 第三步:寫入輸出資料流ByteArrayOutputStream baos = new ByteArrayOutputStream();try {workbook.write(baos);// 寫入} catch (IOException e) {e.printStackTrace();}byte[] ba = baos.toByteArray();ByteArrayInputStream bais = new ByteArrayInputStream(ba);return bais;}


3、在action中寫個方法用於處理匯出:getImport

/** * * 方法名: getImport* 方法描述: (匯出知識動作記錄)* 建立人:liyafei * 修改時間:2015-3-2 上午10:47:08 * 參數 @return* 參數 @throws Exception 參數說明* 傳回型別 String 傳回型別* @throws */public String getImport() throws Exception {this.setFilename("知識動作記錄.xls");filename = URLEncoder.encode(filename, "utf-8");return "import";}


4、在配置action檔案

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



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.