java setcellvalue NA,Java Cell.setCellType方法代碼示例

import org.apache.poi.ss.usermodel.Cell; //導入方法依賴的package包/類

/**

* 讀取Excel裏麵客戶的信息

* @param wb

* @return

*/

private Map readInfoValue(Workbook wb) {

//得到第一個shell

Sheet sheet = wb.getSheetAt(0);

Map modelMap = new HashMap<>();

//得到Excel的行數

this.totalRows = sheet.getPhysicalNumberOfRows();

//得到Excel的列數(前提是有行數)

if (totalRows >= 1 && sheet.getRow(0) != null) {

this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();

}

List infoList = new ArrayList();

InfoEntity info;

List errorList = new ArrayList();

ErrorJson error;

//循環Excel行數,從第二行開始。標題不入庫

for (int r = 1; r < totalRows; r++) {

Row row = sheet.getRow(r);

if (row == null) continue;

info = new InfoEntity();

//循環Excel的列

for (int c = 0; c < this.totalCells; c++) {

Cell cell = row.getCell(c);

if (null != cell) {

if (c == 0) {

cell.setCellType(Cell.CELL_TYPE_STRING);

info.setTitle(cell.getStringCellValue());//title

} else if (c == 1) {

cell.setCellType(Cell.CELL_TYPE_STRING);

info.setContext(cell.getStringCellValue());//context

} else if (c == 2) {

Date ti =new Date();

Timestamp time = new Timestamp(ti.getTime());

info.setTime(time);//time

} else if (c == 3) {

cell.setCellType(Cell.CELL_TYPE_STRING);

info.setSender(cell.getStringCellValue());//sender

}

else if (c == 4) {

info.setStudentId((int) cell.getNumericCellValue());//學號

}

}

}

//添加學生

System.out.println(info);

if (info.getStudentId()!=0&&info.getTitle()!=""&&info.getContext()!=""&&info.getTime()!=null&&info.getSender()!="") {

infoList.add(info);

} else if (info.getStudentId()==0) {

error = new ErrorJson();

error.setId(info.getId());

error.setErrors("學號不能為空");

errorList.add(error);

} else if ( info.getTitle()=="") {

error = new ErrorJson();

error.setId(info.getId());

error.setErrors("title不能為空");

errorList.add(error);

} else if (info.getId() != 0 && info.getContext() == "") {

error = new ErrorJson();

error.setId(info.getId());

error.setErrors("內容不能為空");

errorList.add(error);

} else if (info.getId() != 0 && info.getSender() == "") {

error = new ErrorJson();

error.setId(info.getId());

error.setErrors("發送者不能為空");

errorList.add(error);

}

}

modelMap.put("result", infoList);

modelMap.put("error", errorList);

return modelMap;

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值