java 运用poi读取Excel 文件代码 demo

/**
 * 导入保修excel表格
 * @return 
 */
public String importWarrExcelFile(){
	try {
	FileOperate fileoperate=new FileOperate();	
	if("".equals(importFileFileName)|| importFileFileName==null){
		return "";
	}
	else if(!"xls".equals(fileoperate.getFileExt(importFileFileName)) ){//获取上传文件的后缀;
		return "";
	}
		Date now = new Date();
		HSSFWorkbook workbook=new HSSFWorkbook(new FileInputStream(importFile));//获取一张excel表;
		HSSFSheet sheet=workbook.getSheetAt(0); //获取sheet1工作区的数据;
		int maxRownum=sheet.getLastRowNum();//获取此sheet下的最后一行的数目;
		for(int i=4;i<maxRownum;i++){ //遍历行
			HSSFRow row=sheet.getRow(i);
			XProjectBudgetWarranty  warranty=new XProjectBudgetWarranty();
			warranty.setTUsers(SessionUtil.getSessionUser());
			warranty.setImportTime(new Timestamp(now.getTime()));
			if(row!=null){
			
				int maxCellnum=row.getLastCellNum(); //获取列的最后一列的数目;
				for(int j=0;j<maxCellnum;j++){
					HSSFCell cell=row.getCell((short) j);//获取某行中某列的对象
					String value=getCellValue(cell).trim(); //获取该列的内容;getCellValue(cell)为自定义方法;
					if(value==null){
						break;
					}					

					switch(j){
					case 0: 
						if(value==null || "".equals(value)){
							break;
						}
						if(subjectDAO.findBycode(value)==null){
							break;
						}
						else{
							int pid=subjectDAO.findBycode(value).getSubjectParent();//获取父栏目
							if(pid>0){ //如果存在父栏目 则将父栏目和自己保存
								warranty.setSubjectParent(subjectDAO.findById(pid));
								warranty.setSubjectChild(subjectDAO.findBycode(value));
								break;
							}
							else{
								warranty.setSubjectParent(subjectDAO.findBycode(value));
								break;
							}
						}
						
					case 2:
						warranty.setCostFormula(value);
						
					break;
					case 3:
						if(value==null || value.trim()==""){
							value = "0";
						}
						warranty.setBudgetCostAmount(Double.parseDouble(value));
						break;
					case 4:
						warranty.setNote(value);
						break;
					}
					warranty.setXProject(project);
				}
			}
			if(warranty.getSubjectParent()!=null){
				
				warrantyDAO.save(warranty);
			}
		}
	System.out.println(importFileFileName+"导入成功!");
	
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	return "budget_star_forward";
}

/**
		 * 得到单元格值
		 * @param cell
		 * @return 
		 */
		private String getCellValue(HSSFCell cell){
			if(cell == null)
				return "";
			if(cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
				return cell.getRichStringCellValue().toString();
			if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
				Double value = new Double(cell.getNumericCellValue());
				String v = Arith.csvPriceNotDot(value);
				v = v.replaceAll(",", "");
				return v; 
			}	
			if(cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN)
				return new Boolean(cell.getBooleanCellValue()).toString();
			
			return cell.getRichStringCellValue().toString();
		}

你可能感兴趣的:(poi读取excel)