JAVA 导入EXCLE,处理时间字段
import com.fengshun.systemBusiness.dao.BloodRecordsDao; import com.fengshun.systemBusiness.entity.BloodRecords; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; public List<BloodRecords> getList(InputStream fis) throws Exception { List<BloodRecords> list = new ArrayList<>(); Workbook workbook = new HSSFWorkbook(fis); Sheet sheet; //获取sheet数量 int number = workbook.getNumberOfSheets(); for (int i = 0; i < number; i++) { sheet = workbook.getSheetAt(i); if (sheet == null) { continue; } //循环行 for (int j = 1; j < sheet.getLastRowNum(); j++) { Row row = sheet.getRow(j); Cell cell0 = row.getCell(0); Cell cell1 = row.getCell(1); Cell cell2 = row.getCell(2); Cell cell3 = row.getCell(3); Cell cell4 = row.getCell(4); Cell cell5 = row.getCell(5); Date date = HSSFDateUtil.getJavaDate(cell0.getNumericCellValue()); BloodRecords b0 = new BloodRecords(); b0.setPatientId("1503256397"); b0.setVisitId(2L); b0.setCollectDate(date); b0.setBloodValue(Double.valueOf(cell1.toString())); b0.setDynamicBlood(true); BloodRecords b1 = new BloodRecords(); b1.setPatientId("1804142342"); b1.setVisitId(1L); b1.setCollectDate(date); b1.setBloodValue(Double.valueOf(cell2.toString())); b1.setDynamicBlood(true); BloodRecords b2 = new BloodRecords(); b2.setPatientId("1611147489"); b2.setVisitId(2L); b2.setCollectDate(date); b2.setBloodValue(Double.valueOf(cell3.toString())); b2.setDynamicBlood(true); BloodRecords b3 = new BloodRecords(); b3.setPatientId("1805056176"); b3.setVisitId(1L); b3.setCollectDate(date); b3.setBloodValue(Double.valueOf(cell4.toString())); b3.setDynamicBlood(true); BloodRecords b4 = new BloodRecords(); b4.setPatientId("1804302808"); b4.setVisitId(1L); b4.setCollectDate(date); b4.setBloodValue(Double.valueOf(cell5.toString())); b4.setDynamicBlood(true); list.add(b0); list.add(b1); list.add(b2); list.add(b3); list.add(b4); } } fis.close(); return list; }