List of usage examples for org.apache.poi.ss.usermodel Row getCell
Cell getCell(int cellnum);
From source file:cn.bzvs.excel.export.template.ExcelExportOfTemplateUtil.java
License:Apache License
/** * ??/*from w ww .j a v a 2s. c o m*/ * @param cell * @param name * @param mergedRegionHelper * @return */ private Object[] getAllDataColumns(Cell cell, String name, MergedRegionHelper mergedRegionHelper) { List<ExcelForEachParams> columns = new ArrayList<ExcelForEachParams>(); cell.setCellValue(""); columns.add(getExcelTemplateParams(name.replace(END_STR, EMPTY), cell, mergedRegionHelper)); int rowspan = 1, colspan = 1; if (!name.contains(END_STR)) { int index = cell.getColumnIndex(); //?col int startIndex = cell.getColumnIndex(); Row row = cell.getRow(); while (true) { int colSpan = columns.get(columns.size() - 1) != null ? columns.get(columns.size() - 1).getColspan() : 1; index += colSpan; for (int i = 1; i < colSpan; i++) { //??,???,,? columns.add(null); continue; } cell = row.getCell(index); //??? if (cell == null) { //?, columns.add(null); continue; } String cellStringString; try {//?? ? cellStringString = cell.getStringCellValue(); if (StringUtils.isBlank(cellStringString) && colspan + startIndex <= index) { throw new ExcelExportException("for each ,?"); } else if (StringUtils.isBlank(cellStringString) && colspan + startIndex > index) { //?,,?,?,? columns.add(new ExcelForEachParams(null, cell.getCellStyle(), (short) 0)); continue; } } catch (Exception e) { throw new ExcelExportException(ExcelExportEnum.TEMPLATE_ERROR, e); } //?cell cell.setCellValue(""); if (cellStringString.contains(END_STR)) { columns.add(getExcelTemplateParams(cellStringString.replace(END_STR, EMPTY), cell, mergedRegionHelper)); break; } else if (cellStringString.contains(WRAP)) { columns.add(getExcelTemplateParams(cellStringString.replace(WRAP, EMPTY), cell, mergedRegionHelper)); //??,?? colspan = index - startIndex + 1; index = startIndex - 1; row = row.getSheet().getRow(row.getRowNum() + 1); rowspan++; } else { columns.add(getExcelTemplateParams(cellStringString.replace(WRAP, EMPTY), cell, mergedRegionHelper)); } } } colspan = 0; for (int i = 0; i < columns.size(); i++) { colspan += columns.get(i) != null ? columns.get(i).getColspan() : 0; } colspan = colspan / rowspan; return new Object[] { rowspan, colspan, columns }; }
From source file:cn.bzvs.excel.imports.ExcelImportServer.java
License:Apache License
/*** * ?List?// w ww . ja v a2 s. c o m * * @param object * @param param * @param row * @param titlemap * @param targetId * @param pictures * @param params */ private void addListContinue(Object object, ExcelCollectionParams param, Row row, Map<Integer, String> titlemap, String targetId, Map<String, PictureData> pictures, ImportParams params) throws Exception { Collection collection = (Collection) PoiReflectorUtil.fromCache(object.getClass()).getValue(object, param.getName()); Object entity = PoiPublicUtil.createObject(param.getType(), targetId); String picId; boolean isUsed = false;// ?? for (int i = row.getFirstCellNum(); i < param.getExcelParams().size(); i++) { Cell cell = row.getCell(i); String titleString = (String) titlemap.get(i); if (param.getExcelParams().containsKey(titleString)) { if (param.getExcelParams().get(titleString).getType() == 2) { picId = row.getRowNum() + "_" + i; saveImage(object, picId, param.getExcelParams(), titleString, pictures, params); } else { saveFieldValue(params, entity, cell, param.getExcelParams(), titleString, row); } isUsed = true; } } if (isUsed) { collection.add(entity); } }
From source file:cn.bzvs.excel.imports.ExcelImportServer.java
License:Apache License
private <T> List<T> importExcel(Collection<T> result, Sheet sheet, Class<?> pojoClass, ImportParams params, Map<String, PictureData> pictures) throws Exception { List collection = new ArrayList(); Map<String, ExcelImportEntity> excelParams = new HashMap<String, ExcelImportEntity>(); List<ExcelCollectionParams> excelCollection = new ArrayList<ExcelCollectionParams>(); String targetId = null;//from ww w .j av a2s . co m if (!Map.class.equals(pojoClass)) { Field fileds[] = PoiPublicUtil.getClassFields(pojoClass); ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class); if (etarget != null) { targetId = etarget.value(); } getAllExcelField(targetId, fileds, excelParams, excelCollection, pojoClass, null); } Iterator<Row> rows = sheet.rowIterator(); for (int j = 0; j < params.getTitleRows(); j++) { rows.next(); } Map<Integer, String> titlemap = getTitleMap(rows, params, excelCollection); checkIsValidTemplate(titlemap, excelParams, params, excelCollection); Row row = null; Object object = null; String picId; while (rows.hasNext() && (row == null || sheet.getLastRowNum() - row.getRowNum() > params.getLastOfInvalidRow())) { row = rows.next(); // ???,?,? // keyIndex ??,?? if (params.getKeyIndex() != null && (row.getCell(params.getKeyIndex()) == null || StringUtils.isEmpty(getKeyValue(row.getCell(params.getKeyIndex())))) && object != null) { for (ExcelCollectionParams param : excelCollection) { addListContinue(object, param, row, titlemap, targetId, pictures, params); } } else { object = PoiPublicUtil.createObject(pojoClass, targetId); try { for (int i = row.getFirstCellNum(), le = titlemap.size(); i < le; i++) { Cell cell = row.getCell(i); String titleString = (String) titlemap.get(i); if (excelParams.containsKey(titleString) || Map.class.equals(pojoClass)) { if (excelParams.get(titleString) != null && excelParams.get(titleString).getType() == 2) { picId = row.getRowNum() + "_" + i; saveImage(object, picId, excelParams, titleString, pictures, params); } else { saveFieldValue(params, object, cell, excelParams, titleString, row); } } } for (ExcelCollectionParams param : excelCollection) { addListContinue(object, param, row, titlemap, targetId, pictures, params); } if (verifyingDataValidity(object, row, params, pojoClass)) { collection.add(object); } } catch (ExcelImportException e) { if (!e.getType().equals(ExcelImportEnum.VERIFY_ERROR)) { throw new ExcelImportException(e.getType(), e); } } } } return collection; }
From source file:cn.edu.zucc.chenxg.preview.ToHtml.java
License:Apache License
private void printSheetContent(Sheet sheet) { printColumnHeads();/*from www. ja v a 2 s. c o m*/ out.format("<tbody>%n"); Iterator<Row> rows = sheet.rowIterator(); while (rows.hasNext()) { Row row = rows.next(); out.format(" <tr>%n"); out.format(" <td class=%s>%d</td>%n", ROW_HEAD_CLASS, row.getRowNum() + 1); for (int i = firstColumn; i < endColumn; i++) { String content = " "; String attrs = ""; CellStyle style = null; if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) { Cell cell = row.getCell(i); if (cell != null) { style = cell.getCellStyle(); attrs = tagStyle(cell, style); //Set the value that is rendered for the cell //also applies the format CellFormat cf = CellFormat.getInstance(style.getDataFormatString()); CellFormatResult result = cf.apply(cell); content = result.text; if (content.equals("")) content = " "; } } out.format(" <td class=%s %s>%s</td>%n", styleName(style), attrs, content); } out.format(" </tr>%n"); } out.format("</tbody>%n"); }
From source file:cn.hanbell.war.control.InvmasImportManagedBean.java
@Override public void handleFileUploadWhenNew(FileUploadEvent event) { super.handleFileUploadWhenNew(event); if (this.fileName != null) { InvmasImport e;/* w w w . j a v a 2 s .c o m*/ if (addedList != null) { addedList.clear(); } try { InputStream is = new FileInputStream(getAppResPath() + "/" + fileName); Workbook wb = WorkbookFactory.create(is); Sheet sheet = wb.getSheetAt(wb.getActiveSheetIndex()); Cell c; for (Row r : sheet) { if (r.getRowNum() == 0) { continue; } e = new InvmasImport(); c = r.getCell(0); e.setItcls(BaseLib.convertExcelCell(String.class, c).trim()); c = r.getCell(1); e.setItnbr(BaseLib.convertExcelCell(String.class, c).trim()); c = r.getCell(2); e.setItdsc(BaseLib.convertExcelCell(String.class, c).trim()); c = r.getCell(3); e.setSpdsc(BaseLib.convertExcelCell(String.class, c).trim()); c = r.getCell(4); e.setEitdsc(BaseLib.convertExcelCell(String.class, c).trim()); c = r.getCell(5); e.setEspdsc(BaseLib.convertExcelCell(String.class, c).trim()); c = r.getCell(6); e.setKind(BaseLib.convertExcelCell(String.class, c).trim()); c = r.getCell(7); e.setUnmsr1(BaseLib.convertExcelCell(String.class, c).trim()); c = r.getCell(8); e.setUsed(BaseLib.convertExcelCell(String.class, c).trim()); e.setStatus("N"); e.setCreator(userManagedBean.getCurrentUser().getUsername()); e.setCredateToNow(); addedList.add(e); } doAdd = true; showInfoMsg("Info", "?"); } catch (IOException | InvalidFormatException ex) { showErrorMsg("Info", ",??"); Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex); } } }
From source file:cn.lhfei.fu.poi.HomeworkDataFactory.java
License:Apache License
@Test public void read() throws IOException, InvalidFormatException { InputStream inp = new FileInputStream( "E:\\Webapp_workspace\\webapps_agent\\forestry-university-app\\src\\test\\resource\\excel\\2014-2015.xlsx"); //InputStream inp = new FileInputStream("workbook.xlsx"); Workbook wb = WorkbookFactory.create(inp); Sheet sheet = wb.getSheetAt(0);//w w w . ja v a 2s.co m int totalRows = sheet.getPhysicalNumberOfRows(); Cell teacherCell = null; Cell courseCell = null; Cell academicYearCell = null; Cell semesterCell = null; Cell classCell = null; String teacherName = null; String courseName = null; String academicYear = null; String semester = null; String classNames = null; HomeworkBaseModel model = null; Date currentDate = new Date(); for (int i = 0; i < totalRows; i++) { Row row = sheet.getRow(i); teacherCell = row.getCell(2); // courseCell = row.getCell(3); // academicYearCell = row.getCell(4); // semesterCell = row.getCell(5); // classCell = row.getCell(10); // ? if (teacherCell != null) { teacherName = teacherCell.getStringCellValue().trim(); } if (courseCell != null) { courseName = courseCell.getStringCellValue().trim(); } if (academicYearCell != null) { academicYear = academicYearCell.getStringCellValue().trim(); } if (semesterCell != null) { semester = semesterCell.getStringCellValue().trim(); } if (classCell != null) { classNames = classCell.getStringCellValue().trim(); String[] names = classNames.split(","); if (names != null && names.length > 0) { for (String className : names) { model = new HomeworkBaseModel(); model.setAcademicYear(academicYear); model.setSemester(semester); model.setActionType(OperationTypeEnum.SC.getCode().toString()); model.setClassName(className); model.setCourseName(courseName); model.setCreateTime(currentDate); model.setTeacherName(teacherName); } } } log.info("teacher={}, course={}, xn={}, xq={}, homework={}", teacherName, courseName, academicYear, semester, classNames); } }
From source file:cn.lhfei.fu.service.impl.ExcelServiceImpl.java
License:Apache License
/** * <tt> ? ?? ? </tt>//from w w w. ja v a2s . co m * <tt> ? ?? </tt> * @param file * @param degreeEnum * @return * @throws IOException * @throws InvalidFormatException */ public void importStudent(InputStream file, DegreeEnum degreeEnum) throws InvalidFormatException, IOException { Date currentDate = new Date(); List<StudentBaseModel> studentList = new ArrayList<StudentBaseModel>(); List<UserModel> userList = new ArrayList<UserModel>(); Workbook wb = WorkbookFactory.create(file); Sheet sheet = wb.getSheetAt(0); int totalRows = sheet.getPhysicalNumberOfRows(); Cell studentIdCell = null; // ? Cell studentNameCell = null; // ?? Cell classCell = null; // ? Cell genderCell = null; // Integer degree = degreeEnum.getCode(); int size = 0; for (int i = 1; i < totalRows; i++) { String studentId = ""; String studentName = ""; int gender = 0; // (0)|(1) String genderStr = ""; String className = ""; String grade = ""; String major = ""; String desc = "?"; Row row = sheet.getRow(i); StudentBaseModel model = new StudentBaseModel(); UserModel userModel = new UserModel(); if (degreeEnum.equals(DegreeEnum.BK)) { // ? studentIdCell = row.getCell(0); studentNameCell = row.getCell(1); classCell = row.getCell(2); genderCell = row.getCell(3); className = classCell.getStringCellValue(); } else if (degreeEnum.equals(DegreeEnum.SS)) { Cell gradeCell = null; // Cell majorCell = null; // gradeCell = row.getCell(0); majorCell = row.getCell(1); studentIdCell = row.getCell(2); studentNameCell = row.getCell(3); genderCell = row.getCell(4); grade = gradeCell.getStringCellValue(); major = majorCell.getStringCellValue(); } studentIdCell.setCellType(Cell.CELL_TYPE_STRING); studentId = studentIdCell.getStringCellValue(); studentName = studentNameCell.getStringCellValue(); genderStr = genderCell.getStringCellValue(); if ("".equals(genderStr)) { gender = 1; } else { gender = 0; } model.setClassName(className); model.setGrade(grade); model.setMajorName(major); model.setMajorCode(major); model.setStudentId(studentId); model.setName(studentName); model.setDegree(degree); model.setGender(gender); model.setCreateTime(currentDate); model.setModifyTime(currentDate); model.setBirthday(currentDate); model.setUserType(UserTypeEnum.STUDENT.getCode()); model.setAliasName(studentName); model.setEmail(studentId + "@fu.com"); model.setExtend(desc); model.setExtend1(desc); log.info("Total Rows: {}", totalRows); userModel.setBirthday(currentDate); userModel.setCreateTime(currentDate); userModel.setGender(gender); userModel.setModifyTime(currentDate); userModel.setPassWord(SecurityUtils.toMd5("123456")); userModel.setRoleId(24); userModel.setSignTime(currentDate); userModel.setUserId(studentId); userModel.setUserName(studentName); userModel.setAliasName(studentName); userModel.setEmail(studentId + "@fu.com"); userModel.setUserType(UserTypeEnum.STUDENT.getCode()); size++; userList.add(userModel); studentList.add(model); if (size % MAX_BATCH_SIZE == 0) { excelMapper.importStudent(studentList); excelMapper.importSysUser(userList); studentList = new ArrayList<StudentBaseModel>(); userList = new ArrayList<UserModel>(); } } if (studentList.size() > 0) { excelMapper.importStudent(studentList); excelMapper.importSysUser(userList); } }
From source file:cn.lhfei.fu.service.impl.ExcelServiceImpl.java
License:Apache License
/** * // ww w.j a v a 2 s .co m * -?-?-??-()-()-??--?-??-?? * * @param file * @param degreeEnum * @return * @throws IOException * @throws InvalidFormatException */ public void importThesis(InputStream file, DegreeEnum degreeEnum) throws InvalidFormatException, IOException { Date currentDate = new Date(); List<ThesisBaseModel> thesisList = new ArrayList<ThesisBaseModel>(); Workbook wb = WorkbookFactory.create(file); Sheet sheet = wb.getSheetAt(0); int totalRows = sheet.getPhysicalNumberOfRows(); Cell academicYearCell = null; Cell classCell = null; Cell studentIdCell = null; Cell studentNameCell = null; Cell titleCell = null; Cell enTitleCell = null; Cell originCell = null; Cell thesisTypeCell = null; Cell teacherIdCell = null; Cell teacherNameCell = null; Cell teacherTitleCell = null; String desc = "?"; Integer degree = degreeEnum.getCode(); int size = 0; for (int i = 1; i < totalRows; i++) { String academicYear = ""; String className = ""; String studentId = ""; String studentName = ""; String title = ""; String enTitle = ""; String origin = ""; String thesisType = ""; String teacherId = ""; String teacherName = ""; String teacherTitle = ""; Row row = sheet.getRow(i); academicYearCell = row.getCell(0); classCell = row.getCell(1); studentIdCell = row.getCell(2); studentNameCell = row.getCell(3); titleCell = row.getCell(4); enTitleCell = row.getCell(5); originCell = row.getCell(6); thesisTypeCell = row.getCell(7); teacherIdCell = row.getCell(8); teacherNameCell = row.getCell(9); teacherTitleCell = row.getCell(10); studentIdCell.setCellType(Cell.CELL_TYPE_STRING); teacherIdCell.setCellType(Cell.CELL_TYPE_STRING); academicYearCell.setCellType(Cell.CELL_TYPE_STRING); academicYear = academicYearCell.getStringCellValue(); className = classCell.getStringCellValue(); studentId = studentIdCell.getStringCellValue(); studentName = studentNameCell.getStringCellValue(); title = titleCell.getStringCellValue(); enTitle = enTitleCell.getStringCellValue(); origin = originCell.getStringCellValue(); thesisType = thesisTypeCell.getStringCellValue(); teacherId = teacherIdCell.getStringCellValue(); teacherName = teacherNameCell.getStringCellValue(); teacherTitle = teacherTitleCell.getStringCellValue(); ThesisBaseModel model = new ThesisBaseModel(); model.setAcademicYear(academicYear); model.setClassName(className); model.setStudentId(studentId); model.setStudentName(studentName); model.setThesisTitle(title); model.setThesisEnTitle(enTitle); model.setOrigin(origin); model.setThesisType(thesisType); model.setTeacherId(teacherId); model.setTeacherName(teacherName); model.setTeacherTitle(teacherTitle); model.setDescription(desc); model.setDegree(degree); model.setActionType("" + OperationTypeEnum.PLSC.getCode()); model.setOperationTime(currentDate); model.setCreateTime(currentDate); model.setModifyTime(currentDate); thesisList.add(model); size++; if (size % 50 == 0) { excelMapper.importThesis(thesisList); thesisList = new ArrayList<ThesisBaseModel>(); } } if (thesisList.size() > 0) { excelMapper.importThesis(thesisList); } }
From source file:cn.lhfei.fu.service.impl.HomeworkBaseServiceImpl.java
License:Apache License
@Override public boolean importHomework(String filePath) throws Exception { InputStream inp = new FileInputStream(filePath); Workbook wb = WorkbookFactory.create(inp); Sheet sheet = wb.getSheetAt(0);/*from www . ja va2s. c o m*/ int totalRows = sheet.getPhysicalNumberOfRows(); Cell teacherCell = null; Cell courseCell = null; Cell academicYearCell = null; Cell semesterCell = null; Cell classCell = null; String teacherName = null; String courseName = null; String academicYear = null; String semester = null; String classNames = null; HomeworkBaseModel model = null; Date currentDate = new Date(); List<HomeworkBaseModel> homeworkList = new ArrayList<HomeworkBaseModel>(); for (int i = 0; i < totalRows; i++) { Row row = sheet.getRow(i); teacherCell = row.getCell(2); // courseCell = row.getCell(3); // academicYearCell = row.getCell(4); // semesterCell = row.getCell(5); // classCell = row.getCell(10); // ? if (teacherCell != null) { teacherName = teacherCell.getStringCellValue().trim(); } if (courseCell != null) { courseName = courseCell.getStringCellValue().trim(); } if (academicYearCell != null) { academicYear = academicYearCell.getStringCellValue().trim(); } if (semesterCell != null) { semester = semesterCell.getStringCellValue().trim(); } if (classCell != null) { classNames = classCell.getStringCellValue().trim(); String[] names = classNames.split(","); if (names != null && names.length > 0) { for (String className : names) { model = new HomeworkBaseModel(); model.setAcademicYear(academicYear); model.setSemester(semester); model.setActionType(OperationTypeEnum.SC.getCode().toString()); model.setClassName(className); model.setCourseName(courseName); model.setTeacherName(teacherName); model.setOperationTime(currentDate); model.setCreateTime(currentDate); model.setModifyTime(currentDate); model.setName(""); model.setTeacherId(""); model.setCourseCode("0"); homeworkList.add(model); } } } log.info("teacher={}, course={}, xn={}, xq={}, homework={}", teacherName, courseName, academicYear, semester, classNames); } for (HomeworkBaseModel baseModel : homeworkList) { excelFactoryMapper.importHomework(baseModel); } return false; }
From source file:cn.lhfei.fu.service.impl.HomeworkDataBuildFactory.java
License:Apache License
@Override public boolean importDataByExcel(String filePath, Map<String, Object> params) throws Exception { InputStream inp = new FileInputStream(filePath); Workbook wb = WorkbookFactory.create(inp); Sheet sheet = wb.getSheetAt(0);//from ww w . j a va2 s. c o m int totalRows = sheet.getPhysicalNumberOfRows(); Cell homeworkNameCell = null; // ?? Cell academicYearCell = null; // Cell semesterCell = null; // Cell teacherIdCell = null; // ? Cell teacherNameCell = null; // ?? Cell classCell = null; // ??? Cell courseCodeCell = null; // ? Cell courseNameCell = null; // ?? String homeworkName = null; String teacherId = null; String teacherName = null; String courseName = null; String courseCode = null; String academicYear = null; String semester = null; String className = null; HomeworkBaseModel model = null; Date currentDate = new Date(); List<HomeworkBaseModel> homeworkList = new ArrayList<HomeworkBaseModel>(); for (int i = 1; i < totalRows; i++) { Row row = sheet.getRow(i); homeworkNameCell = row.getCell(0); // ?? academicYearCell = row.getCell(1); // semesterCell = row.getCell(2); // teacherIdCell = row.getCell(3); // ? teacherNameCell = row.getCell(4); // ?? classCell = row.getCell(5); // ??? courseCodeCell = row.getCell(6); // ? courseNameCell = row.getCell(7); // ?? if (homeworkNameCell != null) { homeworkName = homeworkNameCell.getStringCellValue().trim(); } if (academicYearCell != null) { academicYear = academicYearCell.getStringCellValue().trim(); } if (semesterCell != null) { semester = semesterCell.getStringCellValue().trim(); } if (teacherIdCell != null) { teacherId = teacherIdCell.getStringCellValue().trim(); } if (teacherNameCell != null) { teacherName = teacherNameCell.getStringCellValue().trim(); } if (courseCodeCell != null) { courseCode = courseCodeCell.getStringCellValue().trim(); } if (courseNameCell != null) { courseName = courseNameCell.getStringCellValue().trim(); } if (classCell != null) { className = classCell.getStringCellValue().trim(); model = new HomeworkBaseModel(); model.setName(homeworkName); model.setAcademicYear(academicYear); model.setSemester(semester); model.setTeacherId(teacherId); model.setTeacherName(teacherName); model.setClassName(className); model.setCourseCode(courseCode); model.setCourseName(courseName); model.setActionType(OperationTypeEnum.SC.getCode().toString()); model.setOperationTime(currentDate); model.setCreateTime(currentDate); model.setModifyTime(currentDate); homeworkList.add(model); } log.info("teacher={}, course={}, xn={}, xq={}, homework={}", teacherName, courseName, academicYear, semester, className); } for (HomeworkBaseModel baseModel : homeworkList) { excelFactoryMapper.importHomework(baseModel); } return false; }