城市猎人

在一网情深的日子里,谁能说得清是苦是甜,只知道确定了就义无反顾
posts - 1, comments - 7, trackbacks - 0, articles - 89

利用OPI读取Excle文件

Posted on 2009-09-02 13:44 sailor 阅读(582) 评论(0)  编辑  收藏 所属分类: java
1、读取文件
 /**
     * 导入方法
     * 
@param request
     * 
@return
     * 
@throws Exception
     
*/

    
public Message importCubage(HttpServletRequest request) throws Exception {
        Message message 
= MessageFactory.getMessage();
        ServletContext context 
= getServletContext();
              
        UploadListener listener 
= new UploadListener(request, 30);
        
// Create a factory for disk-based file items
        FileItemFactory factory = new MonitoredDiskFileItemFactory(listener);
        
// Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload(factory);
        
try {
            List items 
= upload.parseRequest(request);
            UserSession userSession 
= UserSession.getUserSession(request
                    .getSession());
            
for (int i = 0; i < items.size(); i++{
                FileItem fileItem 
= (FileItem) items.get(i);
                
if (!fileItem.isFormField()) {
                    InputStream inputstream 
= fileItem.getInputStream();
                    
//读取excle文件
                    this.getVolumeCorrenctionFactorService().saveExcleWorkBook(inputstream, userSession);
                   
                    message.addMessage(MessageUtils.operateSuccess());
                }

            }

            
return message;
        }
 catch (FileUploadException e) {
            
throw new FileUploadException(e.getMessage());
        }

    }


2、读取ExcleWorkBook
     /**
     * 读取Excle
     * 
@param is
     
*/

    
public void saveExcleWorkBook(InputStream is, UserSession userSession){
        Message message 
= MessageFactory.getMessage();
           
        
//通过得到的文件输入流inputstream创建一个HSSFWordbook对象
        try {
            HSSFWorkbook hssfworkbook 
= new HSSFWorkbook(is);
            HSSFSheet hssfsheet 
= null;
            
for (int i = 0; i < hssfworkbook.getNumberOfSheets(); i++{
                 hssfsheet 
= hssfworkbook.getSheetAt(i);
                 
this.readExcleSheet(hssfsheet, userSession);
            }

        }
 catch (IOException e) {
            e.printStackTrace();
            message.addMessage(
"读取Excle文件失败!");
        }
 
    }

3、解析Excle
  /**
     * 读取Sheet数据
     * 
@param hssfsheet
     
*/

    
private void readExcleSheet(HSSFSheet hssfsheet, UserSession userSession){
        
        
//定义Sheet行
        HSSFRow rows = null;
        
//定义列
        HSSFCell cell = null;
        
        Double firstColumn;
        
        Double firstRow;
        
        List list 
= new ArrayList();
        
        VolumeCorrenctionFactorVO vo 
= null;

         
//遍历该行所有的行,i表示行数 getPhysicalNumberOfRows行的总数
        for (int i = 1; i < hssfsheet.getPhysicalNumberOfRows(); i++{         
           rows 
= hssfsheet.getRow(i);      

           
//获取第i行,第0列的值
           if(rows.getCell((short0).getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
           
{
               firstColumn 
= rows.getCell((short0).getNumericCellValue();
           }
else{
               
throw new BusinessException("Excle第" + (i+1+ "行,第" + "1列不是有效数字");
           }

           
           
//获取第i行,第j列的值
           for(short j = 1; j < hssfsheet.getRow(0).getLastCellNum(); j++ )
           
{
               
//获取第0行,第j列的值             
               if(hssfsheet.getRow(0).getCell(j).getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
               
{
                   firstRow 
= hssfsheet.getRow(0).getCell(j).getNumericCellValue();
               }
else{
                   
throw new BusinessException("Excle第1行,第" + (j + 1+ "列不是有效数字");
               }

               
               
//获取第i行,第j个单元格
               cell = rows.getCell(j); 
               
               
               
//判断是否是数字
               if (null == cell || cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
               
{
                   vo 
= new VolumeCorrenctionFactorVO();
                   vo.setDensity(firstRow);
                   vo.setTemprature(firstColumn);
                   
if(null != cell){
                       Double cellContent 
= cell.getNumericCellValue();
                       vo.setVolumeFactor(cellContent);
                   }

                       
                   list.add(vo);
               }
else{
                    
throw new BusinessException("Excle第" + (i +  1+ "行,第" + (j + 1+ "列不是有效数字");
               }

           }

 
        }

        
        
if(list.size() > 0)
        
{
            
//构建批量插入SQL
            String sql = this.buildSQL(list,userSession);
            
              
//执行一次性插入多条SQL语句
            this.volumeCorrenctionFactorDAO.getSqlExecutor().exectue(sql, null);
        }

    }


只有注册用户登录后才能发表评论。


网站导航: