java 读取excel demo_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

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

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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值