java导入excel包含公式的处理方法

java导入excel包含公式的处理方法

CELL_TYPE_FORMULA的cell时 ,取的是计算出来的值,不是取公式,所以不能用cell.getCellFormula()

取计算出来的值,就要判断值的类型,用这个函数cell.getCachedFormulaResultType()

switch (cell.getCachedFormulaResultType()) {
    case STRING:
        cellValue = cell.getStringCellValue();
        break;
    case NUMERIC:
                  NumberFormat nf = NumberFormat.getInstance();
                    nf.setGroupingUsed(false);
        cellValue = String.valueOf(nf.format(cell.getNumericCellValue()));
        break;
    case BOOLEAN:
        cellValue = String.valueOf(cell.getBooleanCellValue());
        break;
    default:
        cellValue = cell.getCellFormula();
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将Excel的错误信息导入出来,可以使用Java的Apache POI库来读取Excel文件,并使用数据验证器(Data Validation)来检测单元格的错误。以下是读取Excel文件并检测错误的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DataValidation; import org.apache.poi.ss.usermodel.DataValidationConstraint; import org.apache.poi.ss.usermodel.DataValidationHelper; import org.apache.poi.ss.usermodel.DataValidationConstraint.OperatorType; import org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType; import org.apache.poi.ss.usermodel.Name; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.ss.util.CellRangeAddressList; import org.apache.poi.ss.util.CellReference; public class ExcelValidator { public static void main(String[] args) throws IOException { // 读取Excel文件 File file = new File("path/to/excel/file.xlsx"); FileInputStream fis = new FileInputStream(file); Workbook workbook = WorkbookFactory.create(fis); Sheet sheet = workbook.getSheetAt(0); // 获取数据验证器 DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper(); List<String> errorList = new ArrayList<String>(); // 遍历每一行 Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); // 遍历每一个单元格 Iterator<Cell> cellIterator = row.iterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); // 如果单元格有数据验证器 DataValidation dataValidation = sheet.getDataValidationByCell(cell.getAddress()); if (dataValidation != null) { // 获取验证器约束条件 DataValidationConstraint constraint = dataValidation.getValidationConstraint(); // 如果约束条件是一个列表 if (constraint.getValidationType() == ValidationType.LIST) { // 获取列表的有效值 List<String> validValues = new ArrayList<String>(); Name namedRange = workbook.getName(constraint.getFormula1().substring(1)); Sheet namedSheet = namedRange.getSheet(); int firstRow = namedRange.getRefersToFormula().indexOf("!") + 2; int lastRow = namedSheet.getLastRowNum(); for (int i = firstRow; i <= lastRow; i++) { Row namedRow = namedSheet.getRow(i); if (namedRow != null) { Cell namedCell = namedRow.getCell(namedRange.getRefersToFormula().indexOf("!") + 1); if (namedCell != null) { validValues.add(namedCell.getStringCellValue()); } } } // 检查单元格的值是否在有效值列表 if (!validValues.contains(cell.getStringCellValue())) { errorList.add("Invalid value in cell " + cell.getAddress() + ": " + cell.getStringCellValue()); } } // 如果约束条件是数值比较 else if (constraint.getOperator() == OperatorType.BETWEEN || constraint.getOperator() == OperatorType.NOT_BETWEEN) { double value = cell.getNumericCellValue(); double minValue = constraint.getFormula1().equals("") ? -Double.MAX_VALUE : Double.parseDouble(constraint.getFormula1()); double maxValue = constraint.getFormula2().equals("") ? Double.MAX_VALUE : Double.parseDouble(constraint.getFormula2()); // 检查单元格的值是否在有效范围内 if (constraint.getOperator() == OperatorType.BETWEEN && (value < minValue || value > maxValue)) { errorList.add("Value out of range in cell " + cell.getAddress() + ": " + value); } else if (constraint.getOperator() == OperatorType.NOT_BETWEEN && (value >= minValue && value <= maxValue)) { errorList.add("Value out of range in cell " + cell.getAddress() + ": " + value); } } // 如果约束条件是自定义公式 else if (constraint.getValidationType() == ValidationType.FORMULA) { String formula = constraint.getFormula1(); // 如果公式引用了其他单元格 if (formula.contains("$")) { // 获取引用的单元格范围 int firstRow = CellReference.convertColStringToIndex(formula.substring(formula.indexOf("!") + 1, formula.indexOf("$"))); int firstColumn = CellReference.convertColStringToIndex(formula.substring(formula.indexOf("$") + 1, formula.indexOf(":"))); int lastRow = CellReference.convertColStringToIndex(formula.substring(formula.indexOf(":") + 3, formula.lastIndexOf("$"))); int lastColumn = CellReference.convertColStringToIndex(formula.substring(formula.lastIndexOf("$") + 1)); // 创建一个新的数据验证器,只验证引用的单元格范围 DataValidation newValidation = dataValidationHelper.createValidation(constraint, new CellRangeAddressList(firstRow, lastRow, firstColumn, lastColumn)); // 检查单元格的值是否符合新的验证器 if (!newValidation.isValid(cell)) { errorList.add("Invalid value in cell " + cell.getAddress() + ": " + cell.getStringCellValue()); } } // 如果公式不引用其他单元格 else { // 检查单元格的值是否符合公式 if (!cell.getCellFormula().equals("") && !cell.getCellFormula().equals(formula)) { errorList.add("Invalid value in cell " + cell.getAddress() + ": " + cell.getStringCellValue()); } } } } } } // 输出错误信息 if (errorList.size() > 0) { System.out.println("Errors found:"); for (String error : errorList) { System.out.println(error); } } else { System.out.println("No errors found."); } workbook.close(); fis.close(); } } ``` 这个示例代码可以检测Excel文件的数据验证器,并输出错误信息。你可以根据自己的需求修改代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值