parseExcel

java 代码
  1. /**  
  2.  *   
  3.  */  
  4. package cn.flyingsoft.oais.service.exchange.entity.impl;   
  5.   
  6. import java.io.File;   
  7. import java.io.FileInputStream;   
  8. import java.io.FileNotFoundException;   
  9. import java.io.IOException;   
  10. import java.util.ArrayList;   
  11. import java.util.List;   
  12.   
  13. import org.apache.poi.hssf.usermodel.HSSFCell;   
  14. import org.apache.poi.hssf.usermodel.HSSFRow;   
  15. import org.apache.poi.hssf.usermodel.HSSFSheet;   
  16. import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
  17.   
  18. import cn.flyingsoft.oais.di.ip.entity.DataMap;   
  19. import cn.flyingsoft.oais.di.ip.entity.Field;   
  20. import cn.flyingsoft.oais.di.ip.entity.ValueNumber;   
  21. import cn.flyingsoft.oais.di.ip.entity.ValueText;   
  22. import cn.flyingsoft.oais.service.exchange.entity.SimplePkg;   
  23.   
  24. /**解析xsl文件  
  25.  * @author jin 20070308  
  26.  *  
  27.  */  
  28. public class ParseExcel {   
  29.        
  30.     private HSSFWorkbook workBook;   
  31.     private SimplePkg simplePkg = null  ;   
  32.     private HSSFSheet sheet;   
  33.        
  34.     public ParseExcel(){   
  35.        
  36.     }   
  37.        
  38.     //获取workBook   
  39.     private HSSFWorkbook getWorkBook(FileInputStream fileInputStream){   
  40.         try {   
  41.             workBook = new HSSFWorkbook(fileInputStream) ;   
  42.         } catch (IOException e) {   
  43.             e.printStackTrace();   
  44.         }   
  45.         return workBook;   
  46.            
  47.     }   
  48.        
  49.     //获取sheet   
  50.     private HSSFSheet getSheet(){   
  51.         sheet = workBook.getSheetAt(0) ;   
  52.         return sheet;   
  53.            
  54.     }   
  55.        
  56.     //获取总行数   
  57.     private int getRowNum(){   
  58.         int RowNum = sheet.getLastRowNum();   
  59.         return RowNum ;   
  60.     }   
  61.        
  62.     //获取单元类型 Numeric:0 String:1 Formula:2 Blank:3 Boolean:4 Error:5   
  63.     private short getCellType(HSSFCell cell){   
  64.         short cellType = 3;   
  65.         try{   
  66.             cellType = (short) cell.getCellType();   
  67.         }   
  68.         catch(Exception ex){   
  69.             cellType = 3;   
  70.         }   
  71.         return cellType;   
  72.     }   
  73.        
  74.     //获取所有row的值   
  75.     public List getRowValues(File file,int level){   
  76.         FileInputStream fileInputStream;   
  77.         try {   
  78.             fileInputStream = new FileInputStream(file);   
  79.             workBook = this.getWorkBook(fileInputStream) ;   
  80.             sheet = this.getSheet() ;   
  81.         } catch (FileNotFoundException e) {   
  82.             e.printStackTrace();   
  83.         }   
  84.         List list = new ArrayList() ;   
  85.         DataMap dataMap ;   
  86.         /**  
  87.          * 获取第一行  
  88.          */  
  89.         String[] title = new String[sheet.getRow(0).getLastCellNum()] ;   
  90.         for (short n=0; n< title.length; n++){   
  91.             title[n] =  sheet.getRow(0).getCell(n).getStringCellValue().trim() ;   
  92.         }   
  93.         /**  
  94.          * 遍历所有行  
  95.          */  
  96.         for(int i = 1 ;i<=this.getRowNum(); i++){   
  97.             simplePkg = new SimplePkg() ;   
  98.             simplePkg.setLevel(level) ;   
  99.             dataMap = simplePkg.getDescription() ;   
  100.             HSSFRow row = sheet.getRow(i);   
  101.             short cellNum = row.getLastCellNum();   
  102.             HSSFCell cell = null;   
  103.             /**  
  104.              * 遍历所有列  
  105.              */  
  106.             for(short j = 0 ; j                 cell = row.getCell(j);   
  107.                 if(getCellType(cell) == 0) dataMap.put(new Field(title[j]),new ValueNumber(String.valueOf(cell.getNumericCellValue()).trim())) ;   
  108.                 else if(getCellType(cell) == 1) dataMap.put(new Field(title[j]),new ValueText(cell.getStringCellValue().trim())) ;   
  109.                 else if(getCellType(cell) == 2) dataMap.put(new Field(title[j]),new ValueText(cell.getCellFormula().trim())) ;   
  110.                 else if(getCellType(cell) == 4) dataMap.put(new Field(title[j]),new ValueText(new Boolean(cell.getBooleanCellValue()).toString().trim())) ;   
  111.                 simplePkg.setDescription(dataMap) ;   
  112.             }   
  113.             list.add(simplePkg) ;   
  114.         }   
  115.         return list ;   
  116.     }   
  117.        
  118.     //   
  119.     public List returnSimList(List files){   
  120.         List simplePkgs = new ArrayList() ;   
  121.         List rowValues = new ArrayList() ;   
  122.         int count = 1 ;   
  123.         for(File file:files){   
  124.             simplePkg = new SimplePkg () ;   
  125.             rowValues = this.getRowValues(file,count) ;   
  126.             for (SimplePkg simplePkg : rowValues){   
  127.                 simplePkgs.add(simplePkg) ;   
  128.                 simplePkg.setAllLevel(files.size()) ;   
  129.             }   
  130.             count ++ ;   
  131.         }   
  132.         return simplePkgs;    
  133.     }   
  134.        
  135.     public static void main(String args[]) throws IOException{   
  136.         List files = new ArrayList() ;   
  137.         files.add(new File("E:\\zc-01.xls")) ;   
  138.         files.add(new File("E:\\zc-01h.xls")) ;   
  139. //        files.add(new File("E:\\W2005HJ.xls"));   
  140.         ParseExcel parseExcel = new ParseExcel()  ;   
  141.         List simplePkgs = parseExcel.returnSimList(files) ;   
  142.         SimplePkg simplePkg = new SimplePkg () ;   
  143.            
  144.         System.out.println(simplePkg);   
  145.     }   
  146. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值