新闻详情

新闻详情

首页 / 资讯中心 / 详情

通过xml配置实现数据动态导入导出Excel

发布时间:2026/9/30 1:42:00来源:尧图网络
通过xml配置实现数据动态导入导出Excel
spring-dj-excel-common.jar 一个可以通过动态配置 xml 建立 Excel 与数据关系现实数据导入导出的 spring 组件包在 xml 配置文件里你可以很方便的定义 Excel - sheet 表列头文本与数据表、数据实体属性的对应关系对于创建 Excel 文件你可以通过增加某一标签列的子项实现列头跨列展示效果同时你也可以通过设置列头对应的样式属性 headStyle 来设置sheet表列头的文字、颜色、边框的展示效果你也可以针对每一列进行单独的样式设置。首选在你项目 pom.xml 添加 excel 相关依赖!--xls(2003)-- dependency groupIdorg.apache.poi/groupId artifactIdpoi/artifactId version4.1.2/version /dependency !--xlsx(2007)-- dependency groupIdorg.apache.poi/groupId artifactIdpoi-ooxml/artifactId version4.1.2/version /dependency为Excel文件与项目中的数据模型和数据表之间的关系创建一个配置文件如果要将数据导入到Excel文件中那么也可以在配置文件中对Excel文件进行基本的样式设置例如可以设置列宽 Excel 的文本大小、颜色、字体等。CSDN站内下载 jar 包单击此链接可以下载资源编译代码生成的 JAR 包文件单击此链接可以下载 spring-dj-excel-common.jar 包的源代码下面是一个简单的配置文件示例配置文件名称可以根据实际业务进行命名可以在源码包的 resources/excelconfigs 路径下看到示例配置文件。?xml version1.0 encodingUTF-8 standaloneno? FieldMappings tableUserInfo titleUser information headStyletext-align:center;text-valign:center;background-color:green;color:white;border-width:1px;border-color:yellow; column propNameOfModelname allowEmptyfalse columnWidth100 length5 fieldNameOfTablename textName typestring style/ column propNameOfModelsex value1:men,2:women allowEmptyfalse columnWidth80 length1 fieldNameOfTablegender textGender typestring style/ column propNameOfModelage allowEmptytrue columnWidth80 fieldNameOfTableage textAge typeint style/ column propNameOfModelphone allowEmptyfalse columnWidth180 length20 fieldNameOfTablephone textPhone typestring style/ !--If the column has children, it means that the parent column needs to be displayed across columns-- column propNameOfModelcourse fieldNameOfTablecourse textCourse column index1 propNameOfModelchinese allowEmptyfalse columnWidth120 length0 fieldNameOfTablechinese textChinese typefloat style/ column index2 propNameOfModelphysics allowEmptyfalse columnWidth120 length0 fieldNameOfTablephysics textPhysics typefloat style/ /column column propNameOfModeladdress allowEmptytrue width300 length100 fieldNameOfTableaddress textAddress typestring style/ /FieldMappingsFieldMapping 标签属性说明table- 表名称程序中数据导入导出时在调用方法时作为参数使用必须title- 标题数据导入到 Excel 中时数里该属性值不为空sheet表的首行首列将显示该属性值可选headStyle- 设置 Excel 中数据列头的显示效果可以设置单元格背景颜色、单元格前景色、单元格边框线宽和边框线颜色、文本大小、文本在单元格中的位置、文本字体类型、文本粗体可选column 标签属性说明:value- 列的值在程序中执行数据导入或导出时使用格式为key:value(多个用英文状态下的逗号相隔)其中key是数据模型的属性值value是Excel文件中的显示值。fieldNameOfTable- 对相应数据表中的列的名称也可使用 name 属性当采用 Map 类型时必须设置name- 对相应数据表中的列的名称。index -序号, 设置列在 Excel 表中显示顺序, 如果不设置按配置文件中从上至下顺序依次显示可选propNameOfModel- 对应于数据模型的属性名称也可使用alias属性当采用数据模型时必须设置text- Excel 文件中表格的列标题文本必须columnWidth- 设置 Excel 文件中表的列宽也可以使用 width 属性可选allowEmpty- 在将数据导入 Excel 时是否允许数据为空, 设置为 true 表示允许为空可选​​​​​​​type- 数据类型, 类型范围: string, int, float, double, boolean, date;可选​​​​​​​length- 数据允许的长度类型为 string 时有效可选​​​​​​​style- 为指定列设置单独的样式与 headStyle 类似此属性优先级高于 FieldMapping 标签中的 headStyle 属性可选​​​​​​​headStyle- 单独设置当前列在 Excel 文件中标题样式优先级高于 style 属性及 FieldMapping 标签中的 headStyle 属性可以设置单元格背景颜色、单元格前景色、单元格边框线宽和边框线颜色、文本大小、文本在单元格中的位置、文本字体类型、文本粗体可选​​​​​​​dataStyle- 单独设置当前列在 Excel 文件中数据单元样式优先级高于 style 属性可选样式设置支持字体样式color设置字体颜色仅支持在色域范围内的值font-family设置字体名称默认“宋体”font-italic设置字体是否为斜体值为 true 时文本显示为斜体font-weight设置字体需要加粗显示值为 true 时加粗显示font-size设置字体大小text-underline设置文本是否需要加下划线值为 true 时加下划线常规text-align文本在单元格中水平显示位置值left, center, righttext-valign文本在单元格中垂直显示位置值: top, center, bottombackground-color设置单元格背景色仅支持在色域范围内的值border-width设置单元格边框粗度值域 0~13border-color设置单元格边框颜色仅支持在色域范围内的值系统支持色域范围BLACK1, WHITE1, RED1, BRIGHT_GREEN1, BLUE1, YELLOW1, PINK1, TURQUOISE1, BLACK, WHITE, RED, BRIGHT_GREEN, BLUE, YELLOW, PINK, TURQUOISE, DARK_RED, GREEN, DARK_BLUE, DARK_YELLOW, VIOLET, TEAL, GREY_25_PERCENT, GREY_50_PERCENT, CORNFLOWER_BLUE, MAROON, LEMON_CHIFFON, LIGHT_TURQUOISE1, ORCHID, CORAL, ROYAL_BLUE, LIGHT_CORNFLOWER_BLUE, SKY_BLUE, LIGHT_TURQUOISE, LIGHT_GREEN, LIGHT_YELLOW, PALE_BLUE, ROSE, LAVENDER, TAN, LIGHT_BLUE, AQUA, LIME, GOLD, LIGHT_ORANGE, ORANGE, BLUE_GREY, GREY_40_PERCENT, DARK_TEAL, SEA_GREEN, DARK_GREEN, OLIVE_GREEN, BROWN, PLUM, INDIGO, GREY_80_PERCENT通常配置文件位于项目的资源目录中main--java--resources----excelconfig------excel-user-info.xml----application.yml向启动类添加 EnableExcelConfigScan 注解并指定 XML 配置文件目录位置。示例SpringBootApplicatio EnableExcelConfigScan(configPackages {excelconfig}) public class UserInformationApplication { public static void main(String[] args) { SpringApplication.run(UserInformationApplication.class, args); } }如何使用该组件呢该组件支持xls和xlsx文件格式两种格式的数据导入和导出在程序中Excel2003表示后缀为xls文件格式Excel2007表示xlsx文件格式后缀。1、从 Excel 文件获取数据Autowired private IExcel2003Export excel2003Export; Autowired private IExcel2007Import excel2007Import; Test void getDataFromExcel() throws Exception { String fPath D:\\user-info.xls; //The third parameter value UserInfo, corresponds to the value of the table attribute in the xml configuration file excel2003Export.exportToEntityFromFile(fPath, Sheet1, UserInfo, UserInfo.class, ((entity, rowIndex) - { //Get the data for each row in the Excel file System.out.println(row: rowIndex , data: entity.toString()); return true; })); }2、把数据导入到 Excel 中Autowired private IExcel2003Import excel2003Import; Autowired private IExcel2007Import excel2007Import; private byte[] createExcel(IExcelImport excelImport) { String extName xls; if (IExcel2007Import.class.isAssignableFrom(excelImport.getClass())) extName xlsx; try { //Getting an IExcelBuilder interface object is equivalent to creating a new sheet form IExcelBuilder builder excelImport.createBuilder(UserInfo); UserInfo userInfo new UserInfo(); userInfo.setName(DJ).setAge(18).setPhone(1231456789).setGender(1).setUid(admin) .setPwd(admin).setEmail(djqq.com).setAddress(China) .setOrder_by(1).setIs_enabled(true); builder.createRow(userInfo, UserInfo.class); //Here we get another IExcelBuilder interface object, and we create a new sheet again builder excelImport.createBuilder(UserInfo); builder.setSheetName(UserInfoQueryDTO); //Heres how to get the UserInfo data from the database and import it into the newly created sheet table ListUserInfoQueryDTO dtos findUserInfoByName(allan); builder.createRows(dtos, UserInfoQueryDTO.class); byte[] datas excelImport.getBytes(); //You can also choose to save the created Excel file to a specified disk location //excelImport.save(D:\\user-info. extName); return datas; } catch (Exception e) { System.out.println(Excel import exception: e); } finally { try { excelImport.close(); } catch (Exception e) { // } } return new byte[0]; }//使用 IExcel2003Import 去调用 createExcel 方法byte[] data createExcel(excel2003Import);//使用 IExcel2007Import 去调用 createExcel 方法byte[] data createExcel(excel2007Import);
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

yocto: 22-Packagegroup 2026/9/30 2:26:10

yocto: 22-Packagegroup

第12课:Packagegroup — 学会用 packagegroup-myproduct 管理 摘要: 本课详细介绍了Yocto项目中Packagegroup的概念与使用方法。Packagegroup是一种管理软件包集合的机制,用于将产品所需的所有软件包组织在一起,使Image Recipe只需…

阅读更多 →
Dungeon Saga 碎片合成避坑指南:我是如何从“凭感觉”转向“看数据”的 2026/9/30 2:26:10

Dungeon Saga 碎片合成避坑指南:我是如何从“凭感觉”转向“看数据”的

最近在玩 Dungeon Saga 的朋友应该都有同感:碎片合成这件事,看起来简单,实际上坑不少。 8 件碎片合 1 个英雄,听起来就是凑齐就行。但真正上手后你会发现,同样是 8 件,不同阵营、不同品质、不同词缀的组合…

阅读更多 →
使用 linux-dev Docker 容器在 Linux 上编译运行 Warp:完整开发环境搭建指南 2026/9/30 2:26:09

使用 linux-dev Docker 容器在 Linux 上编译运行 Warp:完整开发环境搭建指南

桌面应用开发者工具人工智能AI 应用AI Agent代码智能体 【免费下载链接】warp Warp is an agentic development environment, born out of the terminal. 项目地址: https://gitcode.com/GitHub_Trending/wa/warp 点击查看 免费下载 导读 Warp 是一个诞生于终端、…

阅读更多 →
GPIO 2026/9/30 2:26:09

GPIO

这次小编带来的是一些嵌入式基础理论概念,若有不正确的地方,欢迎一起探讨。1.GPIO的基础知识1.1GPIO的基本概念1.1.1 GPIO是什么GPIO即General Purpose Input/output,通用输入输出出口。是MCU的一个 内部结构,用来做输入输出的控制…

阅读更多 →
yocto: 21-DSP 2026/9/30 2:26:09

yocto: 21-DSP

第11课: 实际上是前面10课的总验收。 摘要: 本课是 Yocto 项目学习的综合验收,将前10课所学知识整合为一个完整的嵌入式 Linux 产品 BSP(Board Support Package)工程。课程详细规划了一个产品级 BSP 的目录结构&#x…

阅读更多 →
ClawPanel从源码构建:Tauri v2桌面应用与Web版开发环境搭建教程 2026/9/30 2:26:02

ClawPanel从源码构建:Tauri v2桌面应用与Web版开发环境搭建教程

ClawPanel从源码构建:Tauri v2桌面应用与Web版开发环境搭建教程 【免费下载链接】clawpanel 🦞 OpenClaw & Hermes Agent 多引擎 AI 管理面板 — 内置 AI 助手(工具调用 图片识别 多模态),一键安装 | Tauri v2 跨…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉