java中mypoiexception,java - 如何使用Poi获取Java中单元格的数据验证源? - 堆栈内存溢出...

news/2024/7/5 1:40:18

此问题包含多个不同的问题。

首先,我们需要获取工作表的数据验证,然后为每个数据验证获取数据验证所适用的Excel单元格范围。 如果该单元格位于该单元格范围之一中,并且数据验证是列表约束,则进行进一步处理。 否则返回默认值。

如果我们有一个明确的列表,例如“ item1,item2,item3,...”,则返回此列表。

否则,如果我们有一个公式来创建列表,并且公式1是对Excel中定义的名称的引用,则获取该名称所引用的Excel单元格范围。 获取该单元格范围内的所有单元格,并将其值放入数组中并返回此值。

完整的例子。 ExcelWorkbook在第一个工作表单元格D1包含数据Validation。

import org.apache.poi.ss.usermodel.*;

import org.apache.poi.ss.util.*;

import org.apache.poi.ss.SpreadsheetVersion;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.FileInputStream;

import java.util.List;

public class ExcelGetDataValidationList {

static String[] getDataValidationListValues(Sheet sheet, Cell cell) {

DataFormatter dataFormatter = new DataFormatter();

List extends DataValidation> dataValidations = sheet.getDataValidations(); // get sheet's data validations

for (DataValidation dataValidation : dataValidations) {

CellRangeAddressList addressList = dataValidation.getRegions(); // get Excel cell ranges the data validation applies to

CellRangeAddress[] addresses = addressList.getCellRangeAddresses();

for (CellRangeAddress address : addresses) {

if (address.isInRange(cell)) { // if the cell is in that cell range

DataValidationConstraint constraint = dataValidation.getValidationConstraint();

if (constraint.getValidationType() == DataValidationConstraint.ValidationType.LIST) { // if it is a list constraint

String[] explicitListValues = constraint.getExplicitListValues(); // if we have a explicit list like "item1, item2, item3, ..."

if (explicitListValues != null) return explicitListValues; // then return this

String formula1 = constraint.getFormula1(); // else if we have a formula creating the list

Workbook workbook = sheet.getWorkbook();

List extends Name> names = workbook.getNames(formula1); // is formula1 a reference to a defined name in Excel?

for (Name name : names) {

String refersToFormula = name.getRefersToFormula(); // get the Excel cell range the name refers to

AreaReference areaReference = new AreaReference(refersToFormula,

(workbook instanceof XSSFWorkbook)?SpreadsheetVersion.EXCEL2007:SpreadsheetVersion.EXCEL97

);

CellReference[] cellReferences = areaReference.getAllReferencedCells(); // get all cells in that cell range

String[] listValues = new String[cellReferences.length]; // and put their values in an array

for (int i = 0 ; i < cellReferences.length; i++) {

CellReference cellReference = cellReferences[i];

listValues[i] = dataFormatter.formatCellValue(

workbook.getSheet(cellReference.getSheetName()).getRow(cellReference.getRow()).getCell(cellReference.getCol())

);

}

return listValues; // and return this

}

}

}

}

}

return new String[]{}; // per default return an empy array

}

public static void main(String[] args) throws Exception {

//String filePath = "ExcelWorkbook.xls";

String filePath = "ExcelWorkbook.xlsx";

Workbook workbook = WorkbookFactory.create(new FileInputStream(filePath));

Sheet sheet = workbook.getSheetAt(0);

Row row = sheet.getRow(0); if (row == null) row = sheet.createRow(0); // row 1

Cell cell = row.getCell(3); if (cell == null) cell = row.createCell(3); // cell D1

System.out.println(cell.getAddress() + ":" + cell);

String[] dataValidationListValues = getDataValidationListValues(sheet, cell);

for (String dataValidationListValue : dataValidationListValues) {

System.out.println(dataValidationListValue);

}

workbook.close();

}

}


http://lihuaxi.xjx100.cn/news/241242.html

相关文章

jfinal框架下使用c3P0连接池连接sql server 2008

2019独角兽企业重金招聘Python工程师标准>>> 闲话少说 进入正题 首先是工程需要的jar包 然后是c3p0的配置文件。我是这样配置的 仅供参考 jdbcDriver com.microsoft.sqlserver.jdbc.SQLServerDriver jdbcUrl jdbc:sqlserver://localhost:7777;databaseNametest us…

(C++)一行代码递归实现辗转相除法

定理&#xff1a;两个整数的最大公约数等于其中较小的那个数和两数相除余数的最大公约数。 int gcd(int a,int b){return !b?a:gcd(b,a%b); } 这里递归边界是 gcd(a,0)a; 递归式是 gcd(a,b)gcd(b,a%b);

mysql left join超时,MySQL 行锁超时排查方法优化

一、大纲#### 20191219 10:10:10,234 | com.alibaba.druid.filter.logging.Log4jFilter.statementLogError(Log4jFilter.java:152) | ERROR | {conn-10593, pstmt-38675}executeerror.updatexxxsetxxx ? , xxx ?whereRowGuid ?com.mysql.jdbc.exceptions.jdbc4.MySQLTra…

Python常用操作记录

当前日期格式化&#xff1a; time.strftime(%Y-%m-%d,time.localtime(time.time())) pprint&#xff1a; import pprint pp pprint.PrettyPrinter(indent4) pp.pprint(stuff)或 import pprint pprint.pprint(stuff)django 模板遍历dict&#xff1a; {% for k,v in item.items…

编写程序记录文件位置

当我们编写程序是会注意到&#xff0c;首先是配置一些函数的结构体。 所以我们就要找到下面的界面&#xff0c;然后打开FWLB中.c文件下面所对应的.h文件&#xff0c;这样就能查找到相应的结构体。下图为我所找到的中断的结构体、 然后就是查找相对应的中断向量。具体就是打开 还…

1034 有理数四则运算

1. 起先&#xff0c;一个用例浮点错误&#xff0c;但是在进入出发之前&#xff0c;明明已经有判断被除数的分子是否为0&#xff0c;另有一个答案错误&#xff0c;但是和书上代码对照&#xff0c;没有区别。题目中说&#xff1a;“题目保证正确的输出中没有超过整型范围的整数”…

php 关闭当前网页代码,js实现关闭网页的代码

本文主要和大家分享js实现关闭网页的代码&#xff0c;希望能帮助到大家。关闭很简单的关闭本页面的方法&#xff0c;没有其他额外的提示&#xff1a;window.openernull; //返回对创建该窗口的 Window 对象的引用。window.open(, _self); //方法用于打开一个新的浏览器窗口或查找…

1049 Counting Ones

1. 这一题起初我用递归的方式&#xff0c;还写了一个数整数有多少个1的函数&#xff0c;OneNum[i] OneNum[i-1]countOne(i);毫不意外地出现了段错误&#xff0c;也就是递归调用的次数太多。 2. 看了参考书&#xff0c;得到了思路上的启发&#xff1a; 给定一个数12&#xff…