Previously, as a project party, and Java contact is not much, in Java Web Development is to play a part.
For Excel data import into the database This problem has always been the format reason and import failure is also a bit melancholy, the development team just said go back to check the format.
However, each time the import is also due to formatting problems and worry, recently put the code to study a bit, online check.
It turns out that there is no direct assignment of format matching after extracting data from the cell. Therefore, it would be nice to do a format match before extracting the data to the database.
So frustrated by the PREVIOUS ignorance!
1 //Convert Excel cell legacy data to String type2 PrivateString getcellstring (cell cell) {3String cellsring= "";4 Switch(Cell.getcelltype ()) {5 CaseHssfcell.cell_type_string://string6Cellsring =Cell.getstringcellvalue ();7 Break; 8 CaseHssfcell.cell_type_numeric://Digital9cellsring=string.valueof (Cell.getnumericcellvalue ());Ten Break; One CaseHssfcell.cell_type_boolean://Boolean Acellsring=string.valueof (Cell.getbooleancellvalue ()); - Break; - CaseHssfcell.cell_type_formula://Formula thecellsring=string.valueof (Cell.getcellformula ()); - Break; - CaseHssfcell.cell_type_blank://Null value -Cellsring= ""; + Break; - CaseHssfcell.cell_type_error://malfunction +Cellsring= ""; A Break; at default: -Cellsring= "ERROR"; - Break; - } - returncellsring; -}
Of course the import POI package is required:
Import Org.apache.poi.hssf.usermodel.HSSFCell;
Solution to the formatting problems of Java for importing Excel data into a database