Excel import and precautions

Source: Internet
Author: User

Excel import and precautions

Errors often occur when the cell data format is incorrect during the Excel import process. type conversion is required before the value assignment.

Provides a type conversion tool class:

1/** 2 * cell type conversion tool Class 3 * @ param Cell 4 * @ return 5 */6 private Map <String, Object> cellTypeDefine (cell Cell) {7 Map <String, Object> map = new HashMap <> (); 8 switch (cell. getCellType () {9 case Cell. CELL_TYPE_BOOLEAN: 10 map. put ("COL", cell. getBooleanCellValue (); 11 break; 12 case Cell. CELL_TYPE_NUMERIC: 13 map. put ("COL", cell. getNumericCellValue (); 14 break; 15 case Cell. CELL_TYPE_STRING: 16 map. put ("COL", cell. getStringCellValue (); 17 break; 18 case Cell. CELL_TYPE_BLANK: 19 map. put ("COL", ""); 20 break; 21 case Cell. CELL_TYPE_FORMULA: 22 map. put ("COL", cell. getCellFormula (); 23 break; 24 case Cell. CELL_TYPE_ERROR: 25 map. put ("COL", ""); 26 break; 27 default: 28 map. put ("COL", ""); 29 break; 30} 31 return map; 32}
Type conversion

Import demo instance in Excel:

1 public class ExcelImport {2 3 public String excelImport (File file) {4 HashMap <String, Sheet> typeSheet = null; 5 List <CmWiringPo> wiringlist = new ArrayList <> (); 6 try {7 HSSFWorkbook workbook = new HSSFWorkbook (); 8 typeSheet = Maps. newHashMap (); 9 Cell cell = null; 10 Row row = null; 11 HSSFSheet sheet = workbook. getSheetAt (0); 12 String sheetName = sheet. getSheetName (); 13 typeSheet. put (sheetName, sheet); 14 15 for (int j = 1; j <sheet. getLastRowNum () + 1; j ++) {16 row = sheet. getRow (j); 17 CmWiringPo cmWiringPo = new CmWiringPo (); 18 // The primary key generates 19 cmWiringPo. setId (11L); 20 for (int k = 0; k <row. getLastCellNum (); k ++) {21 cell = row. getCell (k); 22 switch (k) {23 case 0: 24 Long srid = (long) cell. getNumericCellValue (); 25 cmWiringPo. setSrid (srid); 26 break; 27 case 1: 28 cmWiringPo. setSourceCabinetUName (String. valueOf (cellTypeDefine (cell ). get ("COL"); 29 break; 30 31 default: 32 break; 33} 34} 35} 36 37} catch (Exception e) {38 39 return null; 40} 41 return wiringlist. toString (); 42}
View Code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.