public XSSFSheet bulkLoad(MultipartFile file) {
try {
InputStream inputStream = new ByteArrayInputStream(file.getBytes());
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
XSSFSheet sheet = workbook.getSheetAt(0);
//判断是否为模板文档
XSSFRow row1 = sheet.getRow(0);
XSSFCell cell = row1.getCell(0);
cell.setCellType(CellType.STRING);
String stringCellValue = cell.getStringCellValue();
int physicalNumberOfCells = row1.getPhysicalNumberOfCells();
if (!"身份证号".equals(stringCellValue) || physicalNumberOfCells != 10){
return null;
}
return sheet;
} catch (IOException | StringIndexOutOfBoundsException e) {
return null;
}
}
这个方法是进行模板校验,校验第一行第一列是不是身份证号,总共是不是有10个标题,如果不是,那就是模板出现了问题
public void bulkLoadContent(XSSFSheet sheet) {
Integer rows = sheet.getPhysicalNumberOfRows();
XSSFRow row;
PatientInformation patientInformation = new PatientInformation();
RegisterInformation registerInformation = new RegisterInformation();
BasicInformationImportLog importLog = new BasicInformationImportLog();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
boolean flag;
for (int i = 1; i < rows; i++) {
flag = true;
StringBuffer buffer = new StringBuffer();
//获取第i行的数据
row = sheet.getRow(i);
if (row == null) {
continue;
}
//第一列的值
String value= StringTrim(row.getCell(0).getStringCellValue());
.
.
.
.
.
.
}
}
这里就可以遍历获取到excel表格中的值并对他们做处理了