赞
踩
java读取Excel内容添加到list集合里面去
package com.changan.contract.newsign.utils; import com.changan.contract.xqht.entity.ProcessAudit; import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.text.DateFormat; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; @Slf4j public class ExcelUtil { public static List<ProcessAudit> parseInfoFromInputFile(MultipartFile file) throws IOException { File tempFile = null; tempFile = File.createTempFile("temp", null); file.transferTo(tempFile); FileInputStream fileInput = new FileInputStream(tempFile);//创建文件输入流 XSSFWorkbook wb = new XSSFWorkbook(fileInput);//由输入流文件得到工作簿对象 XSSFSheet sheet = wb.getSheetAt(0);//获取第一个sheet int lastRowNum = sheet.getLastRowNum(); //获取表格内容的最后一行的行数 List<ProcessAudit> respList = Lists.newArrayList(); //rowBegin代表要开始读取的行号,下面这个循环的作用是读取每一行内容 for (int i = 1; i <= lastRowNum; ++i) { ProcessAudit processAudit = new ProcessAudit(); XSSFRow row = sheet.getRow(i);//获取每一行 int columnNum = row.getLastCellNum();//获取每一行的最后一列的列号,即总列数 for (int j=0; j<columnNum; ++j) { XSSFCell cell = row.getCell(j);//获取每个单元格 if(j==0){ cell.setCellType(CellType.STRING); String s0 = cell.getStringCellValue(); System.out.println("s0------------------>"+s0); processAudit.setEmployeeNum(cell.getStringCellValue()); }else if(j==1){ String s1 = cell.getStringCellValue(); System.out.println("s1------------------>"+s1); processAudit.setEmployeeName(cell.getStringCellValue()); }else if(j==2){ String s2 = cell.getStringCellValue(); System.out.println("s2------------------>"+s2); processAudit.setNewContractDeadline(cell.getStringCellValue()); }else if(j==3){ String s3 = cell.getStringCellValue(); System.out.println("s3------------------>"+s3); //processAudit.setNewContractDeadline(cell.getStringCellValue()); } } System.out.println("Excel循环结束!"); respList.add(processAudit); } wb.close(); fileInput.close(); return respList; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。