日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

java poi 空_Java的poi技术遍历Excel时进行空Cell,空row,判断

發(fā)布時間:2024/9/19 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java poi 空_Java的poi技术遍历Excel时进行空Cell,空row,判断 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

/**

*?導(dǎo)入信息

*/

@Override

public?List?add(HttpServletRequest?request)?{

//?TODO?Auto-generated?method?stub

List?num=new?ArrayList();

MultipartHttpServletRequest?multipartRequest?=(MultipartHttpServletRequest)?request;

CommonsMultipartFile?file?=?(CommonsMultipartFile)multipartRequest.getFile("zlUpload");

if(file!=null){

try?{

num?=?save(file.getInputStream());

}?catch?(IOException?e)?{

//?TODO?Auto-generated?catch?block

e.printStackTrace();

}

}

returnnum;

}

/**

*?保存上傳的Excel信息

*/

private?List?save(InputStream?inputStream)?throwsIOException?{

//?TODO?Auto-generated?method?stub

List?error_num?=?new?ArrayList();

List?temp?=(List)readXls(inputStream,error_num);

System.out.println(temp.get(0).getClass().getName());

if(temp.get(0).getClass().getName().equals("org.apache.poi.hssf.usermodel.HSSFCell")){

return?error_num;

}else{

TStudentNo?student?=?null;

List?studentList?=?newArrayList();

for(int?i=0;i

student?=?(TStudentNo)temp.get(i);

studentList.add(student);

}

try?{

//在插入數(shù)據(jù)前進(jìn)行判斷,看數(shù)據(jù)庫中是否有不允許重復(fù)的字段出現(xiàn),以打斷保存進(jìn)度

int?repeat?=?0;

for(int?j?=?0;j

TStudentNo?Studenttemp?=studentMapper.findByStudentNo(studentList.get(j).getStudent_no());

//如果查到了,重復(fù)數(shù)加一,然后跳過不保存

if(Studenttemp!=null){

repeat++;

}

}

if(repeat==0){

for(int?z=0;z

studentMapper.saveStudent(studentList.get(z));

}

}else{

error_num.add("數(shù)據(jù)庫中有相同的數(shù)據(jù),請檢查學(xué)號等不允許重復(fù)的部分!");

return?error_num;

}

}?catch?(Exception?e)?{

//判斷Excel中是否有重復(fù)數(shù)據(jù),如果有重復(fù)跳過保存異常

error_num.add("數(shù)據(jù)庫中有相同的數(shù)據(jù),請檢查學(xué)號等不允許重復(fù)的部分!");

return?error_num;

}

return?temp;

}

}

/**

*?逐行遍歷其Excel

*/

private?Object?readXls(InputStream?inputStream,Listerror_num)?throws?IOException?{

InputStream?is?=?new?BufferedInputStream(inputStream);

HSSFWorkbook?hssfWorkbook?=?new?HSSFWorkbook(is);

TStudentNo?student?=?null;

List?list?=?new?ArrayList();

//循環(huán)工作表Sheet

for(int?numSheet?=0;numSheet

HSSFSheet?hssfSheet?=?hssfWorkbook.getSheetAt(numSheet);

if(hssfSheet?==?null){

continue;

}

for(int?rowNum?=2;rowNum<=hssfSheet.getLastRowNum();rowNum++){

System.out.println(hssfSheet.getLastRowNum());

HSSFRow?hssfRow?=?hssfSheet.getRow(rowNum);

//檢查每行的空格數(shù),如果小于4證明有一個或多個空格,但不是整行

if(CheckRowNull(hssfRow)<4){

student?=?new?TStudentNo();

HSSFCell?name?=?hssfRow.getCell(0);

HSSFCell?student_no?=?hssfRow.getCell(1);

HSSFCell?phone?=?hssfRow.getCell(2);

HSSFCell?class_no?=?hssfRow.getCell(3);

HSSFCell?subject_category?=?hssfRow.getCell(4);

List?temp?=?new?ArrayList();

temp.add(0,?name);

temp.add(1,?student_no);

temp.add(2,?phone);

temp.add(3,?class_no);

temp.add(4,?subject_category);

int?temp1?=?0;//用于跳出雙層for循環(huán)

for(int?i=0;i<5;i++){

//為記錄前臺進(jìn)行提示某行某列出錯

temp1?=?CheckRowError(temp.get(i),error_num,rowNum,i);

if(temp1==-1){

break;

}

}

if(temp1==-1){

return?temp;

}

student.setName(getCellValue(name));

student.setPhone(getCellValue(phone));

student.setStudent_no(getCellValue(student_no));

student.setClass_no(getCellValue(class_no));

student.setSubject_category(Integer.parseInt(getCellValue(subject_category)));

list.add(student);

}else{

continue;

}

}

}

return?list;

}

/**

*?對Excel的各個單元格的格式進(jìn)行判斷并轉(zhuǎn)換

*/

private?String?getCellValue(HSSFCell?cell)?{

String?cellValue?=?"";

DecimalFormat?df?=?newDecimalFormat("#");

switch?(cell.getCellType())?{

case?HSSFCell.CELL_TYPE_STRING:

cellValue?=cell.getRichStringCellValue().getString().trim();

break;

case?HSSFCell.CELL_TYPE_NUMERIC:

cellValue?=df.format(cell.getNumericCellValue()).toString();

break;

case?HSSFCell.CELL_TYPE_BOOLEAN:

cellValue?=String.valueOf(cell.getBooleanCellValue()).trim();

break;

case?HSSFCell.CELL_TYPE_FORMULA:

cellValue?=cell.getCellFormula();

break;

default:

cellValue?=?"";

}

return?cellValue;

}

//判斷某行某列有問題

private?int?CheckRowError(HSSFCell?cell,Listerror_num,int?rowNum,int?cell_num){

//判斷各個單元格是否為空

if(cell==null||cell.equals("")||cell.getCellType()?==HSSFCell.CELL_TYPE_BLANK){

error_num.add("出錯啦!請檢查第"+(rowNum+1)+"行第"+(cell_num+1)+"列。"+"如果您在該行沒有數(shù)據(jù),建議您選擇刪除該行,重試!");

return?-1;

}

return?0;

}

//判斷行為空

private?int?CheckRowNull(HSSFRow?hssfRow){

int?num?=?0;

Iterator?cellItr?=hssfRow.iterator();

while(cellItr.hasNext()){

Cell?c?=cellItr.next();

if(c.getCellType()?==HSSFCell.CELL_TYPE_BLANK){

num++;

}

}

return?num;

}

總結(jié)

以上是生活随笔為你收集整理的java poi 空_Java的poi技术遍历Excel时进行空Cell,空row,判断的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。