1. The position of the cell strings, to obtain row and column location; for example "A2" - a first row and second column
// 数据开始行,列 String startDataCell = "A2"; CellReference cellReference = new CellReference(startDataCell); int dataStartRow = cellReference.getRow(); int dataStartCol = cellReference.getCol();
2. Get all the combined sheet according to the sheet cell range
int numMergedRegions = sheet.getNumMergedRegions(); for (int i = 0; i < numMergedRegions; i++) { CellRangeAddress mergedRegion = sourceSheet.getMergedRegion(i); int firstRow = mergedRegion.getFirstRow(); int firstColumn = mergedRegion.getFirstColumn(); int lastRow = mergedRegion.getLastRow(); int lastColumn = mergedRegion.getLastColumn(); // 根据坐标,创建新的合并区域 CellRangeAddress cellRangeAddress = new CellRangeAddress(firstRow, lastRow, firstColumn, lastColumn); targetSheet.addMergedRegion(cellRangeAddress); }
3. Copy cell formatting
// assign the source cell to the target cell format CellStyle sourceCellStyle = sourceCell.getCellStyle (); / * here because it is a new workbook objects, CellStyle only new objects, then clone, re-assignment; direct assignment source CellStyle objects are not homologous reported abnormal * / CellStyle targetCellStyle = targetWorkBook.createCellStyle (); targetCellStyle.cloneStyleFrom (sourceCellStyle); targetCell.setCellStyle (targetCellStyle);
The cell types, obtaining a value corresponding to the type of
// get a cell type, the formula in cell processing the CellType cellTypeEnum = sourceCell.getCellTypeEnum (); Switch (cellTypeEnum) { Case STRING: targetCell.setCellValue (sourceCell.getStringCellValue ()); BREAK ; Case the NUMERIC: IF (DateUtil.isCellDateFormatted ( sourceCell)) { // date format value targetCell.setCellValue (sourceCell.getDateCellValue ()); } the else { targetCell.setCellValue (sourceCell.getNumericCellValue ()); } BREAK; Case BOOLEAN: targetCell.setCellValue (sourceCell.getBooleanCellValue ()); BREAK ; Case the FORMULA: // *** formula is obtained for a value of a cell is the try { targetCell.setCellValue (sourceCell.getNumericCellValue ()); } the catch (IllegalStateException E) { LOGGER.error ( "error cell formula: The formula is" + sourceCell.getCellFormula ()); targetCell.setCellValue ( "cell calculation error" ); } BREAK ; Case BLANK: break; case ERROR: targetCell.setCellValue(sourceCell.getErrorCellValue()); break; case _NONE: break; default: }
The cell has a formula, the direct calculation code acquired calculator excel
// Get calculator FormulaEvaluator = formulaEvaluator . Wb.getCreationHelper () createFormulaEvaluator (); // calculate all cell values formulaEvaluator.evaluateAll ();
6. excel in the formula is dependent of other, a current calculator, excel other injection calculator
// Excel calculation dependent set the Map <String, FormulaEvaluator> = Workbooks new new the HashMap <> (); // put dependent calculator workbooks.put (v.getModelFileName (), targetWorkbook.getCreationHelper ( ) createFormulaEvaluator ().); // to the current setting depends calculator calculator formulaEvaluator formulaEvaluator = wb.getCreationHelper () createFormulaEvaluator ();. formulaEvaluator.setupReferencedWorkbooks (Workbooks); // calculator formulaEvaluator.evaluateAll ();