1. 程式人生 > >SSM中excel的導出以及poi的使用

SSM中excel的導出以及poi的使用

app nds flush getc tor clas contain form response

首先,這是我對自己的需求而使用的邏輯,若有可以完美的地方方便告訴下小白。

MAVEN

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.16</version>
        </dependency>

1、前端頁面,偽異步(頁面不刷新)

為什麽不用ajax呢?

JQuery的ajax函數的返回類型只有xml、text、json、html等類型,沒有“流”類型。所以就用js做個form表單請求

上代碼

1 function exportExcel(){
2             var myurl="${context}/assetInLibrary/export";
3             var form=$("<form>");
4             form.attr("style","display:none");
5             form.attr("method","post");
6             form.attr("action",myurl);
7             $("body").append(form);
8 }

2、在工具包中創建ViewExcel,繼承AbstractExcelView

先上代碼

 1 public class ViewExcel extends AbstractExcelView {
 2 
 3     private String[] titles;
 4     
 5     //傳入指定的標題頭
 6     public ViewExcel(String[] titles) {
 7         this.titles=titles;
 8     }
 9     
10     @Override
11     protected void
buildExcelDocument(Map<String, Object> model, 12 HSSFWorkbook workbook, HttpServletRequest request, 13 HttpServletResponse response) throws Exception { 14 //獲取數據 15 List<Map<String, String>> list = (List<Map<String, String>>) model.get("excelList"); 16 //在workbook添加一個sheet 17 HSSFSheet sheet = workbook.createSheet(); 18 sheet.setDefaultColumnWidth(15); 19 HSSFCell cell=null; 20 //遍歷標題 21 for (int i = 0; i < titles.length; i++) { 22 //獲取位置 23 cell = getCell(sheet, 0, i); 24 setText(cell, titles[i]); 25 } 26 //數據寫出 27 for (int i = 0; i < list.size(); i++) { 28 //獲取每一個map 29 Map<String, String> map=list.get(i); 30 //一個map一行數據 31 HSSFRow row = sheet.createRow(i+1); 32 for (int j = 0; j < titles.length; j++) { 33 //遍歷標題,把key與標題匹配 34 String title=titles[j]; 35 //判斷該內容存在mapzhong 36 if(map.containsKey(title)){ 37 row.createCell(j).setCellValue(map.get(title)); 38 } 39 } 40 } 41 //設置下載時客戶端Excel的名稱 42 String filename = new SimpleDateFormat("yyyy-MM-dd").format(new Date())+".xls"; 43 response.setContentType("application/vnd.ms-excel"); 44 response.setHeader("Content-disposition", "attachment;filename=" + filename); 45 OutputStream ouputStream = response.getOutputStream(); 46 workbook.write(ouputStream); 47 ouputStream.flush(); 48 ouputStream.close(); 49 } 50 51 }

在構造函數中傳進來需導出的titles,也就是excel中的標題頭,這個邏輯會有點麻煩,因為我是創建Map,讓dao中查出來的數據根據我的Map(‘title’,‘value‘)進行封裝,且title要存在於傳進來的titles中,剩下看源碼就能明白

3、service中的數據封裝

 1 public List<Map<String, String>> selectAllAssetInlibraryInfo() {
 2         List<AssetInlibrary> list = assetInlibraryMapper.selectByExample(null);
 3         List<Map<String, String>> mapList=new ArrayList<Map<String,String>>();
 4         for (AssetInlibrary assetInlibrary : list) {
 5             Map<String, String> map=new HashMap<String, String>();
 6             map.put("編號", assetInlibrary.getId()+"");
 7             map.put("資產名稱", assetInlibrary.getTitle());
 8             AssetType assetType = assetTypeMapper.selectByPrimaryKey(assetInlibrary.getAssetTypeId());
 9             map.put("資產類型", assetType.getTitle());
10             AssetBrand assetBrand = assetBrandMapper.selectByPrimaryKey(assetInlibrary.getAssetBrandId());
11             map.put("資產品牌", assetBrand.getTitle());
12             AssetStorage assetStorage = assetStorageMapper.selectByPrimaryKey(assetInlibrary.getAssetStorageId());
13             map.put("資產存放地點", assetStorage.getTitle());
14             AssetProvider assetProvider = assetProviderMapper.selectByPrimaryKey(assetInlibrary.getAssetProviderId());
15             map.put("資產供應商", assetProvider.getTitle());
16             mapList.add(map);
17         }
18         return mapList;
19     }

4、controller中的數據交互

1 @RequestMapping("/assetInLibrary/export")
2     public ModelAndView export(ModelMap map) throws Exception{
3         List<Map<String,String>> list = assetInLibraryService.selectAllAssetInlibraryInfo();
4         String[] titles={"編號","資產名稱","資產類型","資產品牌","資產存放地點","資產供應商"};
5         ViewExcel excel=new ViewExcel(titles);
6         map.put("excelList", list);
7         return new ModelAndView(excel,map);
8     }

版權聲明:本文為不會代碼的小白原創文章,未經允許不得轉載。

SSM中excel的導出以及poi的使用