波比特的专栏

波比特的博客

读取Excel后在JSP页面显示

读取.xls文档的代码 返回值是string类型
/**
	 * 读取Excel表格内容,生成纯文本
	 * @param is 输入流
	 * @return 返回文本字符串
	 * @throws IOException 抛出IO异常
	 */
	@SuppressWarnings("deprecation")
	public static String extractTextFromXLS(InputStream is)  throws IOException {    
    StringBuffer content  = new StringBuffer();    
    HSSFWorkbook workbook = new HSSFWorkbook(is); //创建对Excel工作簿文件的引用     

    for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {    
        if (null != workbook.getSheetAt(numSheets)) {    
            HSSFSheet aSheet = workbook.getSheetAt(numSheets); //获得一个sheet    

            for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) {    
                if (null != aSheet.getRow(rowNumOfSheet)) {    
                    HSSFRow aRow = aSheet.getRow(rowNumOfSheet); //获得一行    

                    for (short cellNumOfRow = 0; cellNumOfRow <= aRow.getLastCellNum(); cellNumOfRow++) {    
                        if (null != aRow.getCell(cellNumOfRow)) {    
                            HSSFCell aCell = aRow.getCell(cellNumOfRow); //获得列值    
                                                                
                            if(aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){    
                             content.append(aCell.getNumericCellValue());    
                            }else if(aCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN){    
                             content.append(aCell.getBooleanCellValue());    
                            }else {    
                             content.append(aCell.getStringCellValue());    
                            }    
                        }    
                    }    
                }    
            }    
        }  
       
    }    

    return content.toString();	
		
	}
阅读更多
版权声明:Bobit https://blog.csdn.net/wanlixingzhe/article/details/7407829
上一篇POI,Office文档的Java处理包
下一篇java 与com 相互调用的方法比较
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭