1. 程式人生 > >maven+springmvc+POI匯入Excel

maven+springmvc+POI匯入Excel

說明

POI可以對2003-和2007+版本的Excel檔案做匯入匯出操作,本章只簡單介紹對Excel檔案的匯入操作。

       Excel檔案的上傳處理處理請求,依然使用SpringMvc中的MultipartRequest方式處理。

       前端JSP中使用傳統form表單提交方式和Juery.form.js外掛提供的非同步表單請求方式,分別對這兩種方式進行介紹。

環境

Maven+JDK8+ Tomcat7.x + Spring4.1.8

說明

 jquery.form.js 的版本要3.23左右

ImportExcelUtil.Java:Excel解析工具類

UploadExcelControl.java :處理來自頁面的請求控制器

InfoVo.java :將Excel轉換為物件儲存

main.jsp:前端訪問頁

……..


pom.xml:

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency
>
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.9</version> </dependency>

ImportExcelUtil.java(Excel解析工具類)

[java] view plain copy print?
  1. package com.poiexcel.util;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.text.DecimalFormat;  
  5. import java.text.SimpleDateFormat;  
  6. import java.util.ArrayList;  
  7. import java.util.List;  
  8. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  9. import org.apache.poi.ss.usermodel.Cell;  
  10. import org.apache.poi.ss.usermodel.Row;  
  11. import org.apache.poi.ss.usermodel.Sheet;  
  12. import org.apache.poi.ss.usermodel.Workbook;  
  13. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  14. publicclass ImportExcelUtil {  
  15.     privatefinalstatic String excel2003L =“.xls”;    //2003- 版本的excel
  16.     privatefinalstatic String excel2007U =“.xlsx”;   //2007+ 版本的excel
  17.     /** 
  18.      * 描述:獲取IO流中的資料,組裝成List<List<Object>>物件 
  19.      * @param in,fileName 
  20.      * @return 
  21.      * @throws IOException  
  22.      */
  23.     public  List<List<Object>> getBankListByExcel(InputStream in,String fileName) throws Exception{  
  24.         List<List<Object>> list = null;  
  25.         //建立Excel工作薄
  26.         Workbook work = this.getWorkbook(in,fileName);  
  27.         if(null == work){  
  28.             thrownew Exception(“建立Excel工作薄為空!”);  
  29.         }  
  30.         Sheet sheet = null;  
  31.         Row row = null;  
  32.         Cell cell = null;  
  33.         list = new ArrayList<List<Object>>();  
  34.         //遍歷Excel中所有的sheet
  35.         for (int i = 0; i < work.getNumberOfSheets(); i++) {  
  36.             sheet = work.getSheetAt(i);  
  37.             if(sheet==null){continue;}  
  38.             //遍歷當前sheet中的所有行
  39.             for (int j = sheet.getFirstRowNum(); j < sheet.getLastRowNum(); j++) {  
  40.                 row = sheet.getRow(j);  
  41.                 if(row==null||row.getFirstCellNum()==j){continue;}  
  42.                 //遍歷所有的列
  43.                 List<Object> li = new ArrayList<Object>();  
  44.                 for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {  
  45.                     cell = row.getCell(y);  
  46.                     li.add(this.getCellValue(cell));  
  47.                 }  
  48.                 list.add(li);  
  49.             }  
  50.         }  
  51.         work.close();  
  52.         return list;  
  53.     }  
  54.     /** 
  55.      * 描述:根據檔案字尾,自適應上傳檔案的版本  
  56.      * @param inStr,fileName 
  57.      * @return 
  58.      * @throws Exception 
  59.      */
  60.     public  Workbook getWorkbook(InputStream inStr,String fileName) throws Exception{  
  61.         Workbook wb = null;  
  62.         String fileType = fileName.substring(fileName.lastIndexOf(”.”));  
  63.         if(excel2003L.equals(fileType)){  
  64.             wb = new HSSFWorkbook(inStr);  //2003-
  65.         }elseif(excel2007U.equals(fileType)){  
  66.             wb = new XSSFWorkbook(inStr);  //2007+
  67.         }else{  
  68.             thrownew Exception(“解析的檔案格式有誤!”);  
  69.         }  
  70.         return wb;  
  71.     }  
  72.     /** 
  73.      * 描述:對錶格中數值進行格式化 
  74.      * @param cell 
  75.      * @return 
  76.      */
  77.     public  Object getCellValue(Cell cell){  
  78.         Object value = null;  
  79.         DecimalFormat df = new DecimalFormat(“0”);  //格式化number String字元
  80.         SimpleDateFormat sdf = new SimpleDateFormat(“yyy-MM-dd”);  //日期格式化
  81.         DecimalFormat df2 = new DecimalFormat(“0.00”);  //格式化數字
  82.         switch (cell.getCellType()) {  
  83.         case Cell.CELL_TYPE_STRING:  
  84.             value = cell.getRichStringCellValue().getString();  
  85.             break;  
  86.         case Cell.CELL_TYPE_NUMERIC:  
  87.             if(“General”.equals(cell.getCellStyle().getDataFormatString())){  
  88.                 value = df.format(cell.getNumericCellValue());  
  89.             }elseif(“m/d/yy”.equals(cell.getCellStyle().getDataFormatString())){  
  90.                 value = sdf.format(cell.getDateCellValue());  
  91.             }else{  
  92.                 value = df2.format(cell.getNumericCellValue());  
  93.             }  
  94.             break;  
  95.         case Cell.CELL_TYPE_BOOLEAN:  
  96.             value = cell.getBooleanCellValue();  
  97.             break;  
  98.         case Cell.CELL_TYPE_BLANK:  
  99.             value = ”“;  
  100.             break;  
  101.         default:  
  102.             break;  
  103.         }  
  104.         return value;  
  105.     }  
  106. }  
package com.poiexcel.util;

import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class ImportExcelUtil {

    private final static String excel2003L =".xls";    //2003- 版本的excel
    private final static String excel2007U =".xlsx";   //2007+ 版本的excel

    /**
     * 描述:獲取IO流中的資料,組裝成List<List<Object>>物件
     * @param in,fileName
     * @return
     * @throws IOException 
     */
    public  List<List<Object>> getBankListByExcel(InputStream in,String fileName) throws Exception{
        List<List<Object>> list = null;

        //建立Excel工作薄
        Workbook work = this.getWorkbook(in,fileName);
        if(null == work){
            throw new Exception("建立Excel工作薄為空!");
        }
        Sheet sheet = null;
        Row row = null;
        Cell cell = null;

        list = new ArrayList<List<Object>>();
        //遍歷Excel中所有的sheet
        for (int i = 0; i < work.getNumberOfSheets(); i++) {
            sheet = work.getSheetAt(i);
            if(sheet==null){continue;}

            //遍歷當前sheet中的所有行
            for (int j = sheet.getFirstRowNum(); j < sheet.getLastRowNum(); j++) {
                row = sheet.getRow(j);
                if(row==null||row.getFirstCellNum()==j){continue;}

                //遍歷所有的列
                List<Object> li = new ArrayList<Object>();
                for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {
                    cell = row.getCell(y);
                    li.add(this.getCellValue(cell));
                }
                list.add(li);
            }
        }
        work.close();
        return list;
    }

    /**
     * 描述:根據檔案字尾,自適應上傳檔案的版本 
     * @param inStr,fileName
     * @return
     * @throws Exception
     */
    public  Workbook getWorkbook(InputStream inStr,String fileName) throws Exception{
        Workbook wb = null;
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        if(excel2003L.equals(fileType)){
            wb = new HSSFWorkbook(inStr);  //2003-
        }else if(excel2007U.equals(fileType)){
            wb = new XSSFWorkbook(inStr);  //2007+
        }else{
            throw new Exception("解析的檔案格式有誤!");
        }
        return wb;
    }

    /**
     * 描述:對錶格中數值進行格式化
     * @param cell
     * @return
     */
    public  Object getCellValue(Cell cell){
        Object value = null;
        DecimalFormat df = new DecimalFormat("0");  //格式化number String字元
        SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd");  //日期格式化
        DecimalFormat df2 = new DecimalFormat("0.00");  //格式化數字

        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING:
            value = cell.getRichStringCellValue().getString();
            break;
        case Cell.CELL_TYPE_NUMERIC:
            if("General".equals(cell.getCellStyle().getDataFormatString())){
                value = df.format(cell.getNumericCellValue());
            }else if("m/d/yy".equals(cell.getCellStyle().getDataFormatString())){
                value = sdf.format(cell.getDateCellValue());
            }else{
                value = df2.format(cell.getNumericCellValue());
            }
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            value = cell.getBooleanCellValue();
            break;
        case Cell.CELL_TYPE_BLANK:
            value = "";
            break;
        default:
            break;
        }
        return value;
    }


}


UploadExcelControl.java (spring控制器)

[java] view plain copy print?
  1. package com.poiexcel.control;  
  2. import java.io.InputStream;  
  3. import java.io.PrintWriter;  
  4. import java.util.List;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7. import org.springframework.stereotype.Controller;  
  8. import org.springframework.web.bind.annotation.RequestMapping;  
  9. import org.springframework.web.bind.annotation.RequestMethod;  
  10. import org.springframework.web.bind.annotation.ResponseBody;  
  11. import org.springframework.web.multipart.MultipartFile;  
  12. import org.springframework.web.multipart.MultipartHttpServletRequest;  
  13. import com.poiexcel.util.ImportExcelUtil;  
  14. import com.poiexcel.vo.InfoVo;  
  15. @Controller
  16. @RequestMapping(“/uploadExcel/*”)    
  17. publicclass UploadExcelControl {  
  18.     /**  
  19.      * 描述:通過傳統方式form表單提交方式匯入excel檔案  
  20.      * @param request  
  21.      * @throws Exception  
  22.      */  
  23.     @RequestMapping(value=“upload.do”,method={RequestMethod.GET,RequestMethod.POST})  
  24.     public  String  uploadExcel(HttpServletRequest request) throws Exception {  
  25.         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;    
  26.         System.out.println(”通過傳統方式form表單提交方式匯入excel檔案!”);  
  27.         InputStream in =null;  
  28.         List<List<Object>> listob = null;  
  29.         MultipartFile file = multipartRequest.getFile(”upfile”);  
  30.         if(file.isEmpty()){  
  31.             thrownew Exception(“檔案不存在!”);  
  32.         }  
  33.         in = file.getInputStream();  
  34.         listob = new ImportExcelUtil().getBankListByExcel(in,file.getOriginalFilename());  
  35.         in.close();  
  36.         //該處可呼叫service相應方法進行資料儲存到資料庫中,現只對資料輸出
  37.         for (int i = 0; i < listob.size(); i++) {  
  38.             List<Object> lo = listob.get(i);  
  39.             InfoVo vo = new InfoVo();  
  40.             vo.setCode(String.valueOf(lo.get(0)));  
  41.             vo.setName(String.valueOf(lo.get(1)));  
  42.             vo.setDate(String.valueOf(lo.get(2)));  
  43.             vo.setMoney(String.valueOf(lo.get(3)));  
  44.             System.out.println(”列印資訊–>機構:”+vo.getCode()+“  名稱:”+vo.getName()+“   時間:”+vo.getDate()+“   資產:”+vo.getMoney());  
  45.         }  
  46.         return“result”;  
  47.     }  
  48.     /** 
  49.      * 描述:通過 jquery.form.js 外掛提供的ajax方式上傳檔案 
  50.      * @param request 
  51.      * @param response 
  52.      * @throws Exception 
  53.      */
  54.     @ResponseBody
  55.     @RequestMapping(value=“ajaxUpload.do”,method={RequestMethod.GET,RequestMethod.POST})  
  56.     publicvoid  ajaxUploadExcel(HttpServletRequest request,HttpServletResponse response) throws Exception {  
  57.         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;    
  58.         System.out.println(”通過 jquery.form.js 提供的ajax方式上傳檔案!”);  
  59.         InputStream in =null;  
  60.         List<List<Object>> listob = null;  
  61.         MultipartFile file = multipartRequest.getFile(”upfile”);  
  62.         if(file.isEmpty()){  
  63.             thrownew Exception(“檔案不存在!”);  
  64.         }  
  65.         in = file.getInputStream();  
  66.         listob = new ImportExcelUtil().getBankListByExcel(in,file.getOriginalFilename());  
  67.         //該處可呼叫service相應方法進行資料儲存到資料庫中,現只對資料輸出
  68.         for (int i = 0; i < listob.size(); i++) {  
  69.             List<Object> lo = listob.get(i);  
  70.             InfoVo vo = new InfoVo();  
  71.             vo.setCode(String.valueOf(lo.get(0)));  
  72.             vo.setName(String.valueOf(lo.get(1)));  
  73.             vo.setDate(String.valueOf(lo.get(2)));   
  74.             vo.setMoney(String.valueOf(lo.get(3)));  
  75.             System.out.println(”列印資訊–>機構:”+vo.getCode()+“  名稱:”+vo.getName()+“   時間:”+vo.getDate()+“   資產:”+vo.getMoney());  
  76.         }  
  77.         PrintWriter out = null;  
  78.         response.setCharacterEncoding(”utf-8”);  //防止ajax接受到的中文資訊亂碼
  79.         out = response.getWriter();  
  80.         out.print(”檔案匯入成功!”);  
  81.         out.flush();  
  82.         out.close();  
  83.     }  
  84. }  
package com.poiexcel.control;

import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.poiexcel.util.ImportExcelUtil;
import com.poiexcel.vo.InfoVo;

@Controller
@RequestMapping("/uploadExcel/*")  
public class UploadExcelControl {

    /**
     * 描述:通過傳統方式form表單提交方式匯入excel檔案
     * @param request
     * @throws Exception
     */
    @RequestMapping(value="upload.do",method={RequestMethod.GET,RequestMethod.POST})
    public  String  uploadExcel(HttpServletRequest request) throws Exception {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;  
        System.out.println("通過傳統方式form表單提交方式匯入excel檔案!");

        InputStream in =null;
        List<List<Object>> listob = null;
        MultipartFile file = multipartRequest.getFile("upfile");
        if(file.isEmpty()){
            throw new Exception("檔案不存在!");
        }
        in = file.getInputStream();
        listob = new ImportExcelUtil().getBankListByExcel(in,file.getOriginalFilename());
        in.close();

        //該處可呼叫service相應方法進行資料儲存到資料庫中,現只對資料輸出
        for (int i = 0; i < listob.size(); i++) {
            List<Object> lo = listob.get(i);
            InfoVo vo = new InfoVo();
            vo.setCode(String.valueOf(lo.get(0)));
            vo.setName(String.valueOf(lo.get(1)));
            vo.setDate(String.valueOf(lo.get(2)));
            vo.setMoney(String.valueOf(lo.get(3)));

            System.out.println("列印資訊-->機構:"+vo.getCode()+"  名稱:"+vo.getName()+"   時間:"+vo.getDate()+"   資產:"+vo.getMoney());
        }
        return "result";
    }

    /**
     * 描述:通過 jquery.form.js 外掛提供的ajax方式上傳檔案
     * @param request
     * @param response
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="ajaxUpload.do",method={RequestMethod.GET,RequestMethod.POST})
    public  void  ajaxUploadExcel(HttpServletRequest request,HttpServletResponse response) throws Exception {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;  

        System.out.println("通過 jquery.form.js 提供的ajax方式上傳檔案!");

        InputStream in =null;
        List<List<Object>> listob = null;
        MultipartFile file = multipartRequest.getFile("upfile");
        if(file.isEmpty()){
            throw new Exception("檔案不存在!");
        }

        in = file.getInputStream();
        listob = new ImportExcelUtil().getBankListByExcel(in,file.getOriginalFilename());

        //該處可呼叫service相應方法進行資料儲存到資料庫中,現只對資料輸出
        for (int i = 0; i < listob.size(); i++) {
            List<Object> lo = listob.get(i);
            InfoVo vo = new InfoVo();
            vo.setCode(String.valueOf(lo.get(0)));
            vo.setName(String.valueOf(lo.get(1)));
            vo.setDate(String.valueOf(lo.get(2))); 
            vo.setMoney(String.valueOf(lo.get(3)));

            System.out.println("列印資訊-->機構:"+vo.getCode()+"  名稱:"+vo.getName()+"   時間:"+vo.getDate()+"   資產:"+vo.getMoney());
        }

        PrintWriter out = null;
        response.setCharacterEncoding("utf-8");  //防止ajax接受到的中文資訊亂碼
        out = response.getWriter();
        out.print("檔案匯入成功!");
        out.flush();
        out.close();
    }


}


InfoVo.java(儲存Excel資料對應的物件)

[java] view plain copy print?
  1. package com.poiexcel.vo;  
  2. //將Excel每一行數值轉換為物件
  3. publicclass InfoVo {  
  4.     private String code;  
  5.     private String name;  
  6.     private String date;  
  7.     private String money;  
  8.     public String getCode() {  
  9.         return code;  
  10.     }  
  11.     publicvoid setCode(String code) {  
  12.         this.code = code;  
  13.     }  
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.     publicvoid setName(String name) {  
  18.         this.name = name;  
  19.     }  
  20.     public String getDate() {  
  21.         return date;  
  22.     }  
  23.     publicvoid setDate(String date) {  
  24.         this.date = date;  
  25.     }  
  26.     public String getMoney() {  
  27.         return money;  
  28.     }  
  29.     publicvoid setMoney(String money) {  
  30.         this.money = money;  
  31.     }  
  32. }  
package com.poiexcel.vo;


//將Excel每一行數值轉換為物件
public class InfoVo {

    private String code;
    private String name;
    private String date;
    private String money;

    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public String getMoney() {
        return money;
    }
    public void setMoney(String money) {
        this.money = money;
    }
}


main.jsp(前端程式碼):

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
    <%  
    String path = request.getContextPath();  
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
    %>  
    <html>  
      <head>  
        <base href="<%=basePath%>">  
        <script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>  
        <script type="text/javascript" src="js/jquery.form.js"></script>   
        <title>My JSP 'index.jsp' starting page</title>  
        <script type="text/javascript">  
                //ajax 方式上傳檔案操作  
                 $(document).ready(function(){  
                    $('#btn').click(function(){  
                        if(checkData()){  
                            $('#form1').ajaxSubmit({    
                                url:'uploadExcel/ajaxUpload.do',  
                                dataType: 'text',  
                                success: resutlMsg,  
                                error: errorMsg  
                            });   
                            function resutlMsg(msg){  
                                alert(msg);     
                                $("#upfile").val("");  
                            }  
                            function errorMsg(){   
                                alert("匯入excel出錯!");      
                            }  
                        }  
                    });  
                 });  

                 //JS校驗form表單資訊  
                 function checkData(){  
                    var fileDir = $("#upfile").val();  
                    var suffix = fileDir.substr(fileDir.lastIndexOf("."));  
                    if("" == fileDir){  
                        alert("選擇需要匯入的Excel檔案!");  
                        return false;  
                    }  
                    if(".xls" != suffix && ".xlsx" != suffix ){  
                        alert("選擇Excel格式的檔案匯入!");  
                        return false;  
                    }  
                    return true;  
                 }  
        </script>   
      </head>  

      <body>  
      <div>1.通過簡單的form表單提交方式,進行檔案的上</br> 2.通過jquery.form.js外掛提供的form表單一步提交功能 </div></br>  
        <form method="POST"  enctype="multipart/form-data" id="form1" action="uploadExcel/upload.do">  
            <table>  
             <tr>  
                <td>上傳檔案: </td>  
                <td> <input id="upfile" type="file" name="upfile"></td>  
             </tr>  
            <tr>  
                <td><input type="submit" value="提交" onclick="return checkData()"></td>  
                <td><input type="button" value="ajax方式提交" id="btn" name="btn" ></td>  
             </tr>  
            </table>    
        </form>  

      </body>  
    </html>  

web.xml

<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="3.0"   
    xmlns="http://java.sun.com/xml/ns/javaee"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd ">  

      <!-- 載入Spring容器監聽 -->  
      <listener>      
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
      </listener>  

      <!-- 設定Spring容器載入配置檔案路徑 -->  
      <context-param>      
          <param-name>contextConfigLocation</param-name>      
          <param-value>WEB-INF/application/applicationContext-*.xml</param-value>  
      </context-param>  

      <!--配置Springmvc核心控制器-->  
      <servlet>            
            <servlet-name>springmvc</servlet-name>           
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
            <load-on-startup>1</load-on-startup>  
      </servlet>      
      <!--為DispatcherServlet建立對映 -->        
      <servlet-mapping>    
            <servlet-name>springmvc</servlet-name>        
            <url-pattern>*.do</url-pattern>      
      </servlet-mapping>   
    <welcome-file-list>  
        <welcome-file>main.jsp</welcome-file>  
    </welcome-file-list>    
</web-app>
springmvc-servlet.xml(只做簡單配置)
[html] view plain copy print?
  1. <?xmlversion=“1.0”encoding=“UTF-8”?>
  2. <beansxmlns=“http://www.springframework.org/schema/beans”
  3.      xmlns:context=“http://www.springframework.org/schema/context”
  4.      xmlns:mvc=“http://www.springframework.org/schema/mvc”
  5.      xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  6.      xmlns:tx=“http://www.springframework.org/schema/tx”
  7.      xsi:schemaLocation=”  
  8.           http://www.springframework.org/schema/beans       
  9.           http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  
  10.           http://www.springframework.org/schema/context  
  11.           http://www.springframework.org/schema/context/spring-context-4.1.xsd  
  12.           http://www.springframework.org/schema/mvc  
  13.           http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd  
  14.           http://www.springframework.org/schema/tx       
  15.           http://www.springframework.org/schema/tx/spring-tx-4.1.xsd     
  16.           http://www.springframework.org/schema/context       
  17.           http://www.springframework.org/schema/beans/spring-beans-4.1.xsd ”>
  18.      <!– 啟動註解驅動–>
  19.      <mvc:annotation-driven/>
  20.      <!– 啟動包掃描功能–>
  21.      <context:component-scanbase-package=“com.poiexcel.*”/>
  22. </beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:mvc="http://www.springframework.org/schema/mvc"  
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="
          http://www.springframework.org/schema/beans     
          http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-4.1.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
          http://www.springframework.org/schema/tx     
          http://www.springframework.org/schema/tx/spring-tx-4.1.xsd   
          http://www.springframework.org/schema/context     
          http://www.springframework.org/schema/beans/spring-beans-4.1.xsd ">

     <!-- 啟動註解驅動-->  
     <mvc:annotation-driven/>  
     <!-- 啟動包掃描功能-->  
     <context:component-scan base-package="com.poiexcel.*" />  

</beans>  


applicationContext-base.xml

[html] view plain copy print?
  1. <?xmlversion=“1.0”encoding=“UTF-8”?>
  2. <beansxmlns=“http://www.springframework.org/schema/beans”
  3.      xmlns:context=“http://www.springframework.org/schema/context”
  4.      xmlns:mvc=“http://www.springframework.org/schema/mvc”
  5.      xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  6.      xmlns:tx=“http://www.springframework.org/schema/tx”
  7.      xsi:schemaLocation=”  
  8.           http://www.springframework.org/schema/beans       
  9.           http://www.springframework.org/schema/beans/spring-beans-4.1.xsd   
  10.           http://www.springframework.org/schema/context  
  11.           http://www.springframework.org/schema/context/spring-context-4.1.xsd   
  12.           http://www.springframework.org/schema/mvc  
  13.           http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd  
  14.           http://www.springframework.org/schema/tx       
  15.           http://www.springframework.org/schema/tx/spring-tx-4.1.xsd      
  16.           http://www.springframework.org/schema/context       
  17.           http://www.springframework.org/schema/context/spring-context-4.1.xsd ”>
  18.      <!– 檢視解釋類 –>
  19.      <beanclass=“org.springframework.web.servlet.view.InternalResourceViewResolver”>
  20.       <propertyname=“prefix”value=“/WEB-INF/jsp/”/>
  21.       <propertyname=“suffix”value=“.jsp”/>