string在spring自动转换

我正在使用SpringMVC在Spring中开发一个项目。 我从(.xls)文件导入数据。

问题是:

  • 我正在读取这个值“ 945854955 ”作为一个string,但在DB中保存为“ 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"; } 

exire代码

 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; } } 

Excel的尝试数据types单元格,有时当您显式指定数据types时,Excel可能会尝试并转换单元格。 您可以尝试右键单击单元格,然后select“格式单元格”,然后select“文本”作为types(类别)。 然而,在分析时间,它仍然可能会被搞砸。

您最快的解决scheme可能是将文件保存为CSV并使用它。 您仍然可以在Excel中进行编辑。 尽pipe您需要进行一些validation,以确保Excel不会尝试在CSV另存为上执行上述转换。 在那里有很多好的Java CSVparsing器OpenCSV,Super CSV。

如果你想继续使用Excel,最费时,但也许是最正确的方法是构build一个中间件层,它parsing行并正确识别和格式化单元值。 可以使用Apache POI和HSSF&XSSF。 要警告的是,处理xls和xlsx需要两套不同的库,并且通常需要足够的抽象来处理两者。 请参阅https://poi.apache.org/spreadsheet/

举个例子:

 protected String getCellValue(final Cell cell){ if (null == cell) { return null; } // For Excel binaries 97 and below, The method of setting the cell type to CELL_TYPE_STRING converts the // Formatted to date to a short. To correct this we check that the cell type is numeric and the check that it is // date formatted. If we don't check that it is Numeric first an IllegalAccessorException is thrown. if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC && isCellDateFormated(cell) { // isCellDateFormated is seperate util function to look at the cell value in order to determine if the date is formatted as a double. // is a date format. return // do date format procedure. } cell.setTypeCell(Cell.CELL_TYPE_STRING); return cell.toString(); } 

希望这可以帮助。

============更新==================

不要像“getNumericCellValue()”这样的方法,尝试设置单元格types为string,并使用toString像上面的例子。 这是我的testing代码。 请注意,xls文件在csv中有一行和4个单元格:“abba,1,211,q123,11.22”

 public void testExtract() throws Exception{ InputStream is = new FileInputStream("/path/to/project/Test/src/test/java/excelTest.xls"); HSSFWorkbook wb = new HSSFWorkbook(is); HSSFSheet sheet = wb.getSheetAt(0); Iterator<Row> rowIter = sheet.iterator(); while (rowIter.hasNext()){ HSSFRow row = (HSSFRow) rowIter.next(); Iterator<Cell> cellIter = row.cellIterator(); while (cellIter.hasNext()){ Cell cell = cellIter.next(); System.out.println("Raw to string: " + cell.toString()); // Check for data format here. If you set a date cell to string and to string the response the output is funky. cell.setCellType(Cell.CELL_TYPE_STRING); System.out.println("Formatted to string: " + cell.toString()); } } is.close(); } 

输出是

 Raw to string: abba Formatted to string: abba Raw to string: 1.0 Formatted to string: 1 Raw to string: 211.0 Formatted to string: 211 Raw to string: q1123 Formatted to string: q1123 Raw to string: 11.22 Formatted to string: 11.22