poi的基本导入
一。获取列的值
private String getCell(Cell cell){ if(null == cell){ return ""; } try{ cell.setCellType(Cell.CELL_TYPE_STRING); String v = cell.getStringCellValue(); return v; }catch(Exception e){ e.printStackTrace(); } }
二。创建列
private HSSFCell createCell(HSSFRow row ,int index,String text){ HSSFCell cell = row.createCell(index); cell.setCellType(XSSFCell.CELL_TYPE_STRING); HSSFRichTextString cellText = new HSSFRichTextString(); cell.setCellValue(cellText); return cell; }
三。导入excel中的数据
XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet =workbook.getSheetAt(0); Iterator<Row> rowIterator = sheet.iterator(); Row rowHead = rowIterator.next(); Row row = rowIterator.next(); row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);//给某列设置列类型 row.getCell(0).getStringCellValue().trim(); //得到值之后,存进一个集合里,然后批量插入数据库 //最后给前端提示即可