apache poi 和EasyExcel 的使用

2024-08-21 09:48
文章标签 使用 apache easyexcel poi

本文主要是介绍apache poi 和EasyExcel 的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • apache poi
    • 隐藏sheet做下拉列表
  • EasyExcel
    • 超链接跳转sheet页
    • 自定义类型转换
    • 隐藏sheet做下拉列表

apache poi

官方文档:https://poi.apache.org/components/spreadsheet/index.html

隐藏sheet做下拉列表

XSSFSheet hiddenSheet = (XSSFSheet) wb.createSheet(hiddenSheetName);
Row hidRow0 = hiddenSheet.createRow(0);
wb.setSheetHidden(wb.getSheetIndex(hiddenSheet), true);
// 添加名称管理器
Name name = wb.createName();
//官方实例说可以不要,不能用时放开
//String nname = "name" + IdUtil.nextIdStr();
//name.setNameName(nname);
String indexToStr = excelColIndexToStr(1 + hidColIndex);
String formula = hiddenSheetName + "!$" + indexToStr + "$2:$" + indexToStr + "$" + (fieldInfo.getKeyIds().size() + 1);
//name.setRefersToFormula(formula);//创建数据验证助手
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(reportNameSheet);
//创建公式列表约束
DataValidationConstraint dataValidationConstraint = dvHelper.createFormulaListConstraint(formula);//应用下拉框的区域边界(行列范围)
CellRangeAddressList addressList = new CellRangeAddressList(2, 1000, repColIndex, repColIndex);
//下拉列表值验证
DataValidation dataValidation = dvHelper.createValidation(dataValidationConstraint, addressList);
dataValidation.setShowErrorBox(true);
dataValidation.setSuppressDropDownArrow(true);
dataValidation.createErrorBox("输入值有误", "请从下拉框选择");
reportNameSheet.addValidationData(dataValidation);
/*** 获取excel中的列标题** @param columnIndex 列序号* @return* @author lijiewei* @date 2021/4/16 10:38*/
private String excelColIndexToStr(int columnIndex) {if (columnIndex <= 0) {return null;}String columnStr = "";columnIndex--;do {if (columnStr.length() > 0) {columnIndex--;}columnStr = ((char) (columnIndex % 26 + (int) 'A')) + columnStr;columnIndex = ((columnIndex - columnIndex % 26) / 26);} while (columnIndex > 0);return columnStr;
}

EasyExcel

官方文档:https://www.yuque.com/easyexcel/doc/read

超链接跳转sheet页

注册writeHandler写处理器

EasyExcel.writerSheet(0,"清单").registerWriteHandler(new InventoryCellWriteHandler()).head(Inventory.class).registerConverter(new LocalDateTimeConverter()).build();

InventoryCellWriteHandler 处理单元格,添加超链接

public class InventoryCellWriteHandler implements CellWriteHandler {...//在单元上的所有操作完成后调用@Overridepublic void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {// 对“校验对象中文名称”列加超链接,跳转到对应sheet页if (!isHead && cell.getColumnIndex() == 0) {CreationHelper createHelper = writeSheetHolder.getSheet().getWorkbook().getCreationHelper();Hyperlink hyperlink = createHelper.createHyperlink(HyperlinkType.DOCUMENT);int index = cell.getRowIndex() - 1;String cellValue = cell.getStringCellValue();//设置超链接,有下划线_,所以加'hyperlink.setAddress("#'"+index+"_"+cellValue+"'!A1");cell.setHyperlink(hyperlink);//设置连接样式Workbook workbook = writeSheetHolder.getParentWriteWorkbookHolder().getWorkbook();CellStyle cellStyle = workbook.createCellStyle();XSSFFont font = (XSSFFont) workbook.createFont();font.setColor(new XSSFColor(new java.awt.Color(0, 0, 204)));cellStyle.setFont(font);cell.setCellStyle(cellStyle);}}
}

自定义类型转换

EasyExcel.writerSheet(0,"清单").registerWriteHandler(new InventoryCellWriteHandler()).head(Inventory.class).registerConverter(new LocalDateTimeConverter()).build();

LocalDateTimeConverter 自定义的类型转换

public class LocalDateTimeConverter implements Converter<LocalDateTime> {@Overridepublic Class supportJavaTypeKey() {return LocalDateTime.class;}@Overridepublic CellDataTypeEnum supportExcelTypeKey() {return CellDataTypeEnum.STRING;}@Overridepublic LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,GlobalConfiguration globalConfiguration) throws ParseException {if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {return LocalDateTime.parse(cellData.getStringValue(),DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));} else {return LocalDateTime.parse(cellData.getStringValue(),DateTimeFormatter.ofPattern(contentProperty.getDateTimeFormatProperty().getFormat()));}}@Overridepublic CellData convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty,GlobalConfiguration globalConfiguration) {if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {return new CellData(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(value));} else {return new CellData(DateTimeFormatter.ofPattern(contentProperty.getDateTimeFormatProperty().getFormat()).format(value));}}
}

隐藏sheet做下拉列表

 //创建数据验证助手
DataValidationHelper helper = sheet.getDataValidationHelper();
String columnName = toExcelColumn(columnIndex);
//创建公式列表约束
DataValidationConstraint constraint = helper.createFormulaListConstraint(SHEETNAME_HIDDEN_DROPDOWN + "!$" + columnName + "$1:$" + columnName + "$" + values.length);
//应用下拉框的区域边界(行列范围)
CellRangeAddressList regions = new CellRangeAddressList(startRow, endRow, startColumn, endColumn);
DataValidation dataValidation = helper.createValidation(constraint, regions);
//设置验证错误显示
dataValidation.setErrorStyle(DataValidation.ErrorStyle.STOP);
dataValidation.setShowErrorBox(false);
dataValidation.setSuppressDropDownArrow(true);
sheet.addValidationData(dataValidation);
//获取excel中的列标题
String toExcelColumn(int columnIndex) {String left = "";if (columnIndex >= 26) {left = toExcelColumn(columnIndex / 26 - 1);}columnIndex %= 26;return left + (char) (columnIndex + 65);
}

这篇关于apache poi 和EasyExcel 的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1092820

相关文章

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

oracle DBMS_SQL.PARSE的使用方法和示例

《oracleDBMS_SQL.PARSE的使用方法和示例》DBMS_SQL是Oracle数据库中的一个强大包,用于动态构建和执行SQL语句,DBMS_SQL.PARSE过程解析SQL语句或PL/S... 目录语法示例注意事项DBMS_SQL 是 oracle 数据库中的一个强大包,它允许动态地构建和执行

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

浅析如何使用Swagger生成带权限控制的API文档

《浅析如何使用Swagger生成带权限控制的API文档》当涉及到权限控制时,如何生成既安全又详细的API文档就成了一个关键问题,所以这篇文章小编就来和大家好好聊聊如何用Swagger来生成带有... 目录准备工作配置 Swagger权限控制给 API 加上权限注解查看文档注意事项在咱们的开发工作里,API

Java数字转换工具类NumberUtil的使用

《Java数字转换工具类NumberUtil的使用》NumberUtil是一个功能强大的Java工具类,用于处理数字的各种操作,包括数值运算、格式化、随机数生成和数值判断,下面就来介绍一下Number... 目录一、NumberUtil类概述二、主要功能介绍1. 数值运算2. 格式化3. 数值判断4. 随机

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

Springboot 中使用Sentinel的详细步骤

《Springboot中使用Sentinel的详细步骤》文章介绍了如何在SpringBoot中使用Sentinel进行限流和熔断降级,首先添加依赖,配置Sentinel控制台地址,定义受保护的资源,... 目录步骤 1: 添加 Sentinel 依赖步骤 2: 配置 Sentinel步骤 3: 定义受保护的

Python中Markdown库的使用示例详解

《Python中Markdown库的使用示例详解》Markdown库是一个用于处理Markdown文本的Python工具,这篇文章主要为大家详细介绍了Markdown库的具体使用,感兴趣的... 目录一、背景二、什么是 Markdown 库三、如何安装这个库四、库函数使用方法1. markdown.mark