1. 程式人生 > >Spring使用POI實現Excel匯入匯出

Spring使用POI實現Excel匯入匯出

Apache POI 是建立和維護操作各種符合Office Open XML(OOXML)標準和微軟的OLE 2複合文件格式(OLE2)的Java API。用它可以使用Java讀取和建立,修改MS Excel檔案.而且,還可以使用Java讀取和建立MS Word和MSPowerPoint檔案。Apache POI 提供Java操作Excel解決方案(適用於Excel97-2008)。


       簡單理解就是通過POI,java可以與office建立聯絡。


       本次專案實踐基於SSM框架,簡單封裝了Excel批量匯入匯出功能,實現過程如下


       1. maven匯入java包:

[html]  view plain  copy
  1.   <dependency>  
  2. <groupId>org.apache.poi</groupId>    
  3. <artifactId>poi-ooxml</
    artifactId>    
  4. <version>3.5-FINAL</version>    
  5.  </dependency>   

       2. 建立Excel實體--ExcelBean

[java]  view plain  copy
  1. /**
     
  2.  *  
  3. * @Description: 匯入匯出excel 
  4. * @author haipeng 
  5. * @date 2017年4月11日 
  6.  */  
  7. public class ExcelBean implements java.io.Serializable {  
  8.      private String headTextName;//列頭(標題)名  
  9.      private String propertyName;//對應欄位名  
  10.      private Integer cols;//合併單元格數  
  11.      private XSSFCellStyle cellStyle;  
  12.        
  13.      public ExcelBean(){  
  14.            
  15.      }  
  16.      public ExcelBean(String headTextName, String propertyName){  
  17.          this.headTextName = headTextName;  
  18.          this.propertyName = propertyName;  
  19.      }  
  20.        
  21.      public ExcelBean(String headTextName, String propertyName, Integer cols) {  
  22.          super();  
  23.          this.headTextName = headTextName;  
  24.          this.propertyName = propertyName;  
  25.          this.cols = cols;  
  26.      }   
  27.        
  28.      public String getHeadTextName() {  
  29.         return headTextName;  
  30.     }  
  31.   
  32.     public void setHeadTextName(String headTextName) {  
  33.         this.headTextName = headTextName;  
  34.     }  
  35.   
  36.     public String getPropertyName() {  
  37.         return propertyName;  
  38.     }  
  39.   
  40.     public void setPropertyName(String propertyName) {  
  41.         this.propertyName = propertyName;  
  42.     }  
  43.   
  44.     public Integer getCols() {  
  45.         return cols;  
  46.     }  
  47.   
  48.     public void setCols(Integer cols) {  
  49.         this.cols = cols;  
  50.     }  
  51.   
  52.     public XSSFCellStyle getCellStyle() {  
  53.         return cellStyle;  
  54.     }  
  55.   
  56.     public void setCellStyle(XSSFCellStyle cellStyle) {  
  57.         this.cellStyle = cellStyle;  
  58.     }  
  59. }  

      3.  封裝Excel工具類--ExcelUtils

[java]  view plain  copy
  1. public class ExcelUtils {  
  2.     private final static String excel2003L =".xls";    //2003- 版本的excel    
  3.     private final static String excel2007U =".xlsx";   //2007+ 版本的excel    
  4.     /*************************************檔案上傳****************************/  
  5.     public static  List<List<Object>> getBankListByExcel(InputStream in,String fileName) throws Exception{    
  6.         List<List<Object>> list = null;    
  7.             
  8.         //建立Excel工作薄    
  9.         Workbook work = getWorkbook(in,fileName);    
  10.         if(null == work){    
  11.             throw new Exception("建立Excel工作薄為空!");    
  12.         }    
  13.         Sheet sheet = null;    
  14.         Row row = null;    
  15.         Cell cell = null;    
  16.             
  17.         list = new ArrayList<List<Object>>();    
  18.         //遍歷Excel中所有的sheet    
  19.         for (int i = 0; i < work.getNumberOfSheets(); i++) {    
  20.             sheet = work.getSheetAt(i);    
  21.             if(sheet==null){continue;}    
  22.                 
  23.             //遍歷當前sheet中的所有行    
  24.             for (int j = sheet.getFirstRowNum(); j < sheet.getLastRowNum(); j++) {    
  25.                 row = sheet.getRow(j);    
  26.                 if(row==null||row.getFirstCellNum()==j){continue;}    
  27.                     
  28.                 //遍歷所有的列    
  29.                 List<Object> li = new ArrayList<Object>();    
  30.                 for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) {    
  31.                     cell = row.getCell(y);    
  32.                     li.add(getCellValue(cell));    
  33.                 }    
  34.