日期:2014-05-16  浏览次数:20786 次

apache poi 导入2007excel 及处理excel不可读取内容!
处理excel主要是三层循环,对工作表的循环,对行数的循环,对单元格的循环
try {
			is = new FileInputStream(file);
			
			HSSFWorkbook wb=null;
			try {
//office 2003和以前版本
				wb = new HSSFWorkbook(new POIFSFileSystem(is));
				
			} catch (RuntimeException e) {
//office 2007版本
				XSSFWorkbook wb1 = new XSSFWorkbook(file.getPath());
				return import2007(wb1);
			}
			//工作表数
			int sheetNum = wb.getNumberOfSheets();
			
			for (int i = 0; i < sheetNum ; i++) {
				
				HSSFSheet childSheet = wb.getSheetAt(i);
				
				int rowNum = childSheet.getLastRowNum();
				
				for (int j = childSheet.getFirstRowNum(); j < rowNum; j++) {
					
					HSSFRow row = childSheet.getRow(j);
					if(row!=null){
						int cellNum = row.getLastCellNum();
						
						String[] str=new String[cellNum];
						
						for (int k = 0; k < cellNum; k++) {
							if(row.getCell(k)!=null){
								str[k]=row.getCell(k).toString();
							}else{
								str[k]=null;
							}
						}
												}
					}
				}
			}



//2007版本excel
public String import2007(XSSFWorkbook wb){
		StockderDAO dao=(StockderDAO)getDao();
		int sheetNum = wb.getNumberOfSheets();
		
		for (int i = 0; i <sheetNum ; i++) {
			
			XSSFSheet childSheet = wb.getSheetAt(i);
			
			int rowNum = childSheet.getLastRowNum();
			
			for (int j = childSheet.getFirstRowNum(); j < rowNum; j++) {
				
				XSSFRow row = childSheet.getRow(j);
				
				int cellNum = row.getLastCellNum();
				
				String[] str=new String[cellNum];
				
				for (int k = 0; k < cellNum; k++) {
					if(row.getCell(k)!=null){
						str[k]=row.getCell(k).toString();
					}else{
						str[k]=null;
					}
				}
				if(j!=0){
					boolean flag=dao.getBuyDistinct(str,this.request.getParameterMap());
					if(flag){
						XSSFCell cel=row.createCell(cellNum );
						cel.setCellValue("R");
					}
				}
				
			}
		}
		ServletOutputStream fOut=null;
		try {
			fOut=response.getOutputStream();
			wb.write( fOut);
			fOut.flush();
			fOut.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return NONE;
	}



关于excel不可读取的内容:当我们把2007的excel另存为 2003excel的格式的时候,然后以2003excel导入到程序,这时如生成的是2003excel,而原本的2007excel如果有安全设置或宏 在打开excel的时候就 弹出 有些内容不可读取的提示!

下面的附件 就是导入2007的excel所需要的jar包!

1 楼 zhanghe086 2012-05-15  
“不可读取的内容”这个问题怎么解决呢?