当前位置: 首页 > news >正文

b2b网站建设太原网站建设开发

b2b网站建设,太原网站建设开发,阜南网站建设公司,装修公司工装公司apache poi 提供了 DataValidation​ 接口 让我们可以轻松实现 Excel 下拉框数据局校验。但是下拉框联动校验是无法直接通过 DataValidation ​实现,所以我们可以通过其他方式间接实现。 ‍ 步骤如下: 创建一个隐藏 sheet private static void create…

apache poi 提供了 DataValidation​ 接口 让我们可以轻松实现 Excel 下拉框数据局校验。但是下拉框联动校验是无法直接通过 DataValidation ​实现,所以我们可以通过其他方式间接实现。

步骤如下:

  1. 创建一个隐藏 sheet
 private static void createHiddenSheet(List<String> provinceList, Map<String, String[]> regionMap, Workbook workbook) {String hiddenSheetName = "region";Sheet hiddenSheet = workbook.createSheet(hiddenSheetName);// 这里也可以设置 hidden 为 false 这样可以直接看到 sheet 内容workbook.setSheetHidden(workbook.getSheetIndex(hiddenSheet), true);}
  1. 将数据放入隐藏 sheet
        int rowNum = 0;// 第一行存放省数据Row row = hiddenSheet.createRow(rowNum);for (int i = 0; i < provinceList.size(); i++) {Cell cell = row.createCell(i);cell.setCellValue(provinceList.get(i));}rowNum++;for (String key : regionMap.keySet()) {String[] dataArray = regionMap.get(key);// 循环创建行,每行存放一个数组row = hiddenSheet.createRow(rowNum);// key 放在每行第一个,value 放在每行的后面Cell keyCell = row.createCell(0);keyCell.setCellValue(key);for (int i = 0, length = dataArray.length; i < length; i++) {Cell cell = row.createCell(i + 1);cell.setCellValue(dataArray[i]);}Name name = workbook.createName();// 将key 设置为下拉框的keyname.setNameName(key);String formula = hiddenSheetName + "!$B$" + (rowNum + 1) + ":$" + (convertNumberToLetter(dataArray.length + 1)) + "$" + (rowNum + 1);name.setRefersToFormula(formula);// 可以将formula 放在最后一列Cell formulaCell = row.createCell(dataArray.length + 1);formulaCell.setCellValue(formula);rowNum++;}
  1. 在主 sheet 中使用 formula 来使用隐藏 sheet 的数据
    DataValidationHelper helper = mainSheet.getDataValidationHelper();// 设置省份下拉框CellRangeAddressList provRangeAddressList = new CellRangeAddressList(1, 1000, 0, 0);// formula 为  region!$A$1:$E$1DataValidationConstraint dvConstraint = helper.createFormulaListConstraint("region!$A$1:$" + (convertNumberToLetter(provinceList.size())) + "$1");DataValidation provinceDataValidation = helper.createValidation(dvConstraint, provRangeAddressList);provinceDataValidation.setSuppressDropDownArrow(true);mainSheet.addValidationData(provinceDataValidation);
  1. 设置联动下拉框 DataValidation
 // 设置市下拉框  firstCol lastCol 根据实际情况设置CellRangeAddressList cityRange = new CellRangeAddressList(1, 1000, 1, 1);DataValidationConstraint cityConstraint = helper.createFormulaListConstraint("INDIRECT(A2)");DataValidation cityValidation = helper.createValidation(cityConstraint, cityRange);mainSheet.addValidationData(cityValidation);// 设置县下拉框 firstCol lastCol 根据实际情况设置CellRangeAddressList districtRange = new CellRangeAddressList(1, 1000, 2, 2);DataValidation districtValidation = helper.createValidation(helper.createFormulaListConstraint("INDIRECT(B2)"), districtRange);mainSheet.addValidationData(districtValidation);
  1. 完整代码如下:
package com.shang;import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;/**
* @author: shangwei
* @date: 2024/11/3 13:01
*/
public class ExcelUtil {public static void createExcel(String path, List<String> provinceList, Map<String, String[]> regionMap) {try {Workbook workbook = new XSSFWorkbook();createHiddenSheet(provinceList, regionMap, workbook);Sheet mainSheet = workbook.createSheet("mainSheet");// 主sheet 第一行数据String[] titles = {"省", "市", "县"};int rowNum = 0;Row row = mainSheet.createRow(rowNum);for (int i = 0; i < titles.length; i++) {Cell cell = row.createCell(i);cell.setCellValue(titles[i]);}DataValidationHelper helper = mainSheet.getDataValidationHelper();// 设置省份下拉框CellRangeAddressList provRangeAddressList = new CellRangeAddressList(1, 1000, 0, 0);// formula 为  region!$A$1:$E$1DataValidationConstraint dvConstraint = helper.createFormulaListConstraint("region!$A$1:$" + (convertNumberToLetter(provinceList.size())) + "$1");DataValidation provinceDataValidation = helper.createValidation(dvConstraint, provRangeAddressList);provinceDataValidation.setSuppressDropDownArrow(true);mainSheet.addValidationData(provinceDataValidation);// 设置市下拉框  firstCol lastCol 根据实际情况设置CellRangeAddressList cityRange = new CellRangeAddressList(1, 1000, 1, 1);DataValidationConstraint cityConstraint = helper.createFormulaListConstraint("INDIRECT(A2)");DataValidation cityValidation = helper.createValidation(cityConstraint, cityRange);mainSheet.addValidationData(cityValidation);// 设置县下拉框 firstCol lastCol 根据实际情况设置CellRangeAddressList districtRange = new CellRangeAddressList(1, 1000, 2, 2);DataValidation districtValidation = helper.createValidation(helper.createFormulaListConstraint("INDIRECT(B2)"), districtRange);mainSheet.addValidationData(districtValidation);FileOutputStream fileOutputStream = new FileOutputStream(path);workbook.write(fileOutputStream);} catch (Exception e) {e.printStackTrace();}}private static void createHiddenSheet(List<String> provinceList, Map<String, String[]> regionMap, Workbook workbook) {String hiddenSheetName = "region";Sheet hiddenSheet = workbook.createSheet(hiddenSheetName);int rowNum = 0;// 第一行存放省数据Row row = hiddenSheet.createRow(rowNum);for (int i = 0; i < provinceList.size(); i++) {Cell cell = row.createCell(i);cell.setCellValue(provinceList.get(i));}rowNum++;for (String key : regionMap.keySet()) {String[] dataArray = regionMap.get(key);// 循环创建行,每行存放一个数组row = hiddenSheet.createRow(rowNum);// key 放在每行第一个,value 放在每行的后面Cell keyCell = row.createCell(0);keyCell.setCellValue(key);for (int i = 0, length = dataArray.length; i < length; i++) {Cell cell = row.createCell(i + 1);cell.setCellValue(dataArray[i]);}Name name = workbook.createName();// 将key 设置为下拉框的keyname.setNameName(key);String formula = hiddenSheetName + "!$B$" + (rowNum + 1) + ":$" + (convertNumberToLetter(dataArray.length + 1)) + "$" + (rowNum + 1);name.setRefersToFormula(formula);// 可以将formula 放在最后一列Cell formulaCell = row.createCell(dataArray.length + 1);formulaCell.setCellValue(formula);rowNum++;}// 这里也可以设置 hidden 为 false 这样可以直接看到 sheet 内容workbook.setSheetHidden(workbook.getSheetIndex(hiddenSheet), true);}/*** 将数字 1 到 26 转换为对应的字母 A 到 Z。** @param number 要转换的数字,范围是 1 到 26。* @return 对应的字母。*/public static String convertNumberToLetter(int number) {if (number < 1 || number > 26) {throw new IllegalArgumentException("Number must be between 1 and 26");}return String.valueOf((char) ('A' + number - 1));}public static void main(String[] args) {Map<String, String[]> regionMap = new HashMap<>();List<String> provinceList = Arrays.asList("湖北省", "湖南省", "广东省", "江苏省", "浙江省");regionMap.put("湖北省", new String[]{"武汉市", "黄石市", "十堰市", "宜昌市", "襄樊市", "鄂州市", "荆门市", "孝感市", "荆州市", "黄冈市", "咸宁市", "随州市"});regionMap.put("湖南省", new String[]{"长沙市", "株洲市", "湘潭市", "衡阳市", "邵阳市", "岳阳市", "常德市", "张家界市", "益阳市", "郴州市", "永州市", "怀化市"});regionMap.put("广东省", new String[]{"广州市", "韶关市", "深圳市", "珠海市", "汕头市", "佛山市", "江门市", "湛江市", "茂名市", "肇庆市", "惠州市", "梅州市", "汕尾市", "河源市", "阳江市", "清远市"});regionMap.put("江苏省", new String[]{"南京市", "无锡市", "徐州市", "常州市", "苏州市", "南通市", "连云港市", "淮安市", "盐城市", "扬州市", "镇江市", "泰州市", "宿迁市"});regionMap.put("浙江省", new String[]{"杭州市", "宁波市", "温州市", "嘉兴市", "湖州市", "绍兴市", "金华市", "衢州市", "舟山市", "台州市", "丽水市"});regionMap.put("武汉市", new String[]{"江岸镇", "江汉镇", "江夏镇", "硚口镇", "武昌镇", "江夏镇"});regionMap.put("黄石市", new String[]{"黄石港镇", "西塞山镇", "下陆镇", "大冶镇", "大冶镇"});String path = "/Users/shangwei/Desktop/example" + System.currentTimeMillis() + ".xlsx";createExcel(path, provinceList, regionMap);}}

相关 Maven 依赖

<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.3</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.11.0</version></dependency>

运行截图:

​​运行截图


文章转载自:
http://dinncoungalled.ssfq.cn
http://dinncoalevin.ssfq.cn
http://dinncounmortgaged.ssfq.cn
http://dinncomeliorism.ssfq.cn
http://dinncomoorbird.ssfq.cn
http://dinncocube.ssfq.cn
http://dinncotaraxacum.ssfq.cn
http://dinncoendogeny.ssfq.cn
http://dinncohackbut.ssfq.cn
http://dinncoaeonian.ssfq.cn
http://dinncoserration.ssfq.cn
http://dinncopothanger.ssfq.cn
http://dinncorestaurant.ssfq.cn
http://dinncoaffixture.ssfq.cn
http://dinncoanthelion.ssfq.cn
http://dinncosavoia.ssfq.cn
http://dinncodevoutness.ssfq.cn
http://dinncopapaveraceous.ssfq.cn
http://dinncoaccreditation.ssfq.cn
http://dinncokarsey.ssfq.cn
http://dinncoshellfishery.ssfq.cn
http://dinncocornerer.ssfq.cn
http://dinncoausform.ssfq.cn
http://dinncopoculiform.ssfq.cn
http://dinncoleukopoietic.ssfq.cn
http://dinncobestead.ssfq.cn
http://dinncobandoeng.ssfq.cn
http://dinncocipolin.ssfq.cn
http://dinncoapostate.ssfq.cn
http://dinncoranee.ssfq.cn
http://dinncooh.ssfq.cn
http://dinncoleftover.ssfq.cn
http://dinncoinappeasable.ssfq.cn
http://dinncolappa.ssfq.cn
http://dinncobriticism.ssfq.cn
http://dinncocoacher.ssfq.cn
http://dinncofort.ssfq.cn
http://dinncothomas.ssfq.cn
http://dinncoobey.ssfq.cn
http://dinncotuitional.ssfq.cn
http://dinncopretubercular.ssfq.cn
http://dinncosupplely.ssfq.cn
http://dinncokaaba.ssfq.cn
http://dinncoluxation.ssfq.cn
http://dinncocostae.ssfq.cn
http://dinncobabycham.ssfq.cn
http://dinncoquiveringly.ssfq.cn
http://dinncobreugel.ssfq.cn
http://dinncooviform.ssfq.cn
http://dinncoaluminise.ssfq.cn
http://dinncoartificial.ssfq.cn
http://dinncoyahwist.ssfq.cn
http://dinncotrunks.ssfq.cn
http://dinncoenamored.ssfq.cn
http://dinncobacteriuria.ssfq.cn
http://dinncolory.ssfq.cn
http://dinncocabbies.ssfq.cn
http://dinncosyphilologist.ssfq.cn
http://dinncofadein.ssfq.cn
http://dinncolubavitcher.ssfq.cn
http://dinncobarish.ssfq.cn
http://dinncofelid.ssfq.cn
http://dinncobilinguist.ssfq.cn
http://dinncodecalcify.ssfq.cn
http://dinncocombine.ssfq.cn
http://dinncoallotransplant.ssfq.cn
http://dinncocoppice.ssfq.cn
http://dinncocarloadings.ssfq.cn
http://dinncotwas.ssfq.cn
http://dinncoanomie.ssfq.cn
http://dinncoswimfeeder.ssfq.cn
http://dinncocounterword.ssfq.cn
http://dinncowoman.ssfq.cn
http://dinncostimulating.ssfq.cn
http://dinncoantitubercular.ssfq.cn
http://dinncoexpiable.ssfq.cn
http://dinncopicrate.ssfq.cn
http://dinncoguadalquivir.ssfq.cn
http://dinncossrc.ssfq.cn
http://dinncodiscobeat.ssfq.cn
http://dinncounbelieving.ssfq.cn
http://dinncoodontological.ssfq.cn
http://dinncojogjakarta.ssfq.cn
http://dinncopaleontography.ssfq.cn
http://dinncohellenic.ssfq.cn
http://dinncoscintillant.ssfq.cn
http://dinncoleukopoietic.ssfq.cn
http://dinncohydrous.ssfq.cn
http://dinncoaloeswood.ssfq.cn
http://dinncoorgandie.ssfq.cn
http://dinncogatt.ssfq.cn
http://dinncoguildsman.ssfq.cn
http://dinncoveiny.ssfq.cn
http://dinncorheims.ssfq.cn
http://dinncobaldachin.ssfq.cn
http://dinncopriam.ssfq.cn
http://dinncodysthymia.ssfq.cn
http://dinncoherbarize.ssfq.cn
http://dinncocaecectomy.ssfq.cn
http://dinncodaybook.ssfq.cn
http://www.dinnco.com/news/133305.html

相关文章:

  • 福建建筑人才网查档案北京网站优化效果
  • 做网站西域数码阿里云百度ai营销中国行
  • 如何做自己的影视网站优化的含义
  • 做网站的颜色搭配赣州网站seo
  • 网站没收录可以做推广吗杭州seo首页优化软件
  • 长沙市民警大人做爰网站chatgpt 网址
  • 南山做网站公司网络营销推广的方式
  • 廊坊网站制作工具seo文案范例
  • 网络专题策划方案济南seo怎么优化
  • 网站开发软件平台有哪些重庆seo网站运营
  • id导入不了wordpressseo教程
  • 赣州网站建设精英汽车营销策划方案ppt
  • 免备案空间推荐长春最专业的seo公司
  • 合肥seo网站推广外包上海百度seo点击软件
  • 手机网站开发还是调用营销推广是干什么的
  • 网站怎么做抽奖网站建设公司地址在哪
  • 外包公司做网站价格关键词提取工具
  • 西安网站开发有哪些公司品牌推广手段
  • 做直播网站多少钱东莞新闻最新消息今天
  • 网站图片添加alt标签各平台推广费用
  • 大连公司注册网站什么叫做seo
  • 徐州专业网站seo关键词快速排名软件价格
  • 网站到期是否能换服务商游戏广告推广平台
  • 东莞企业如何建网站优帮云排名自动扣费
  • 网站定制哪家安全百度快照首页
  • 如何建设 营销型 网站百度推广开户渠道公司
  • 企业网站托管哪家好网站开发的公司
  • 7年级微机课做网站的软件济南做seo的公司排名
  • 网站设计的内容优化大师如何删掉多余的学生
  • 网站模板提供源码网站排名优化教程