我正在使用 SpringMVC 在 Spring 中开发一个项目。我正在从 (.xls) 文件中导入数据。
问题是:
我将这个值“ 945854955 ”作为字符串读取,但在数据库中保存为“ 9.45854955E8 ”
此值“ 26929 ”另存为“ 26929.0 ”
此值“ 21/05/1987 ”保存为“ 31918.0 ”
/读代码
// import ...
@RequestMapping(value="/read")
public String Read(Model model,@RequestParam CommonsMultipartFile[] fileUpload)
throws IOException, EncryptedDocumentException, InvalidFormatException {
List<String> liste = new ArrayList();
Employe employe = new Employe();
String modelnom = null;
liste = extraire(modelnom); //See the second code
for (int m=0, i=29;i<liste.size();i=i+29) {
if(i % 29 == 0) {
m++;
}
employe.setNomEmploye(liste.get(29*m+1));
//...
employe.setDateNaissance((String)liste.get(29*m+8).toString()); // here i had the date problem
employe.setDateEntree((String)liste.get(29*m+9).toString()); // here i had the date problem
employe.setDateSortie((String)liste.get(29*m+10).toString()); // here i had the date problem
// ...
employe.setNumCpteBanc(liste.get(29*m+17)); // here i had the first & second case problem
employe.setNumCIMR(liste.get(29*m+19)); // here i had the first & second case problem
employe.setNumMUT(liste.get(29*m+20)); // here i had the first & second case problem
employe.setNumCNSS(liste.get(29*m+21)); // here i had the first & second case problem
boolean bool=true;
List<Employe> employes = dbE.getAll();// liste des employes
for (int n=0;n<employes.size();n++) {
if (employes.get(n).getMatriculeMY() == (int)mat ) {
bool= false;
}
}
if (bool) {
dbE.create(employe);
}
}
return "redirect";
}
外部代码
private List<String> extraire (String nomFichier) throws IOException {
List<String> liste = new ArrayList();
FileInputStream fis = new FileInputStream(new File(nomFichier));
HSSFWorkbook workbook = new HSSFWorkbook(fis);
HSSFSheet spreadsheet = workbook.getSheetAt(0);
Iterator < Row > rowIterator = null;
// recup une ligne
rowIterator = spreadsheet.iterator();
while (rowIterator.hasNext()) {
int i = 0;
row = (HSSFRow) rowIterator.next();
Iterator < Cell > cellIterator = row.cellIterator();
while ( cellIterator.hasNext()) {
Cell cell = cellIterator.next();
i++;
/**
* Pour verifier si une ligne est vide. (for verifing if the line is empty)
*/
if (i % 29 == 0 || i == 1) {
while ( cellIterator.hasNext() && cell.getCellType() == Cell.CELL_TYPE_BLANK) {
cell = cellIterator.next();
}
}
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
String cellule = String.valueOf(cell.getNumericCellValue());
liste.add(cellule);
break;
case Cell.CELL_TYPE_STRING:
liste.add(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BLANK:
cellule = " ";
liste.add(cellule);
break;
}
}
}
fis.close();
return liste;
}
}