您好,关于查找不存在的sheet ,可以参考下面的代码:
- public static void main(String[] args) throws Exception {
- Workbook workbook = new Workbook();
- IWorksheet workSheet = workbook.getWorksheets().get(0);
- workSheet.setName("CIT.WP00.0_调整汇总");
- workSheet.getRange(1, 1).setFormula("CIT.WP01.1_预提费用!F7");
- workSheet.getRange(3, 3).setFormula("Sheet3!A1");
- FindOptions tempVar = new FindOptions();
- tempVar.setLookIn(FindLookIn.Texts);
- IRange searchRange = workSheet.getCells().specialCells(SpecialCellType.Formulas, SpecialCellsValue.Errors);
- IRange range = null;
- ArrayList<String> sheetNames = new ArrayList<String>();
- do {
- range = searchRange.find("Ref", range,tempVar);
- if (range == null) {
- break;
- } else {
- FormulaSyntaxTree syntaxTree = FormulaSyntaxTree.Parse(range.getFormula().replaceFirst("=",""));
- addNotFoundSheet(syntaxTree.getRoot(), workbook);
- }
- } while (true);
- }
- private static void addNotFoundSheet(SyntaxNode node, Workbook workbook){
- if (node == null) {
- return;
- }
- if (node instanceof ReferenceNode) {
- String sheetName = ((ReferenceNode)node).getReference().getWorksheetName();
- if (workbook.getWorksheets().get(sheetName) == null) {
- IWorksheet tempSheet = workbook.getWorksheets().add();
- tempSheet.setName(sheetName);
- }
- }
- for (SyntaxNode child : node.getChildren()) {
- addNotFoundSheet(child, workbook);
- }
- }
复制代码 您可以对此demo进行测试,看是否可以查到所有的sheet。
添加sheet后,插入列后公式发生变化的问题将在v7.0.2版本修复,修复后,在此demo基础上,插入列将不会导致公式变化。
|