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

Office Xml 2003转XLSX

一、使用到的依赖包

1、xelem-3.1.jar 下载地址:管网下载地址

2、poi-3.17.jar 下载地址:https://mvnrepository.com/artifact/org.apache.poi/poi

二、实现方法

1、Xml2003公式转XLSX公式算法

(1)Xml2003函数格式

SUM(R[-1]C+R[-7]C[-4])

R代表当前行,C代表当前列,中括号中的数字代表相对于当前行的偏移量。比如当前操作的是第10行第三列6列,那么上面公式的含义就是第6列9行与第2列3行相加。转换成XLSX的公式就是

SUM(F9-B3)

(2)代码实现

    private final static String[] ExcelColIndexes = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M","N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z","AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ", "AK", "AL", "AM","AN", "AO", "AP", "AQ", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY", "AZ","BA", "BB", "BC", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BK", "BL", "BM","BN", "BO", "BP", "BQ", "BR", "BS", "BT", "BU", "BV", "BW", "BX", "BY", "BZ"};
private static String convertFormula(String xmlFormula, int rowIndex, int colIndex) {if (xmlFormula.equals("=SUM(RC[-2]-RC[-5])")) {System.out.println();}StringBuilder formula = new StringBuilder(xmlFormula.replace("=", ""));Pattern flaPattern = Pattern.compile("R(\\[[-]?\\d+\\])?C(\\[[-]?\\d+\\])?");Pattern rowPattern = Pattern.compile("R(\\[[-]?\\d+\\])?");Pattern colPattern = Pattern.compile("C(\\[[-]?\\d+\\])?");Pattern numberPattern = Pattern.compile("[-]?\\d+");Matcher flaMatcher = flaPattern.matcher(xmlFormula);while (flaMatcher.find()) {String subFla = "";String content = flaMatcher.group();Matcher colMatcher = colPattern.matcher(content);if (colMatcher.find()) {String colFla = colMatcher.group();Matcher numberMatcher = numberPattern.matcher(colFla);if (numberMatcher.find()) {int num = Integer.parseInt(numberMatcher.group());subFla = ExcelColIndexes[colIndex + num];} else {subFla = ExcelColIndexes[colIndex];}}Matcher rowMatcher = rowPattern.matcher(content);if (rowMatcher.find()) {String rowFla = rowMatcher.group();Matcher numberMatcher = numberPattern.matcher(rowFla);if (numberMatcher.find()) {int num = Integer.parseInt(numberMatcher.group());subFla += rowIndex + 1 + num;} else {subFla += rowIndex + 1;}}int start = formula.indexOf(content);int end = start + content.length();formula.replace(start, end, subFla);}return formula.toString();}

2、XML2003十六进制颜色向POI颜色转换

private static byte[] convertColorHexToByteArray(String colorStr) {colorStr = colorStr.replace("#", "");if (colorStr.length() != 6 && colorStr.length() != 8) {throw new IllegalArgumentException("Must be of the form 112233 or FFEEDDCC");} else {byte[] rgb = new byte[colorStr.length() / 2];for (int i = 0; i < rgb.length; ++i) {String part = colorStr.substring(i * 2, (i + 1) * 2);rgb[i] = (byte) Integer.parseInt(part, 16);}return rgb;}}

3、Xml2003转换XLSX代码

 List<CellRangeAddress> mergedRanges = new ArrayList<>();Map<String, CellStyle> cellStyleMap = new HashMap<>();File xmlFile = new File("C:\\Users\\Daibz\\Desktop\\test2.xls");File outFile = new File("C:\\Users\\Daibz\\Desktop\\test.xlsx");ExcelReader reader = new ExcelReader();Workbook workbook = reader.getWorkbook(new InputSource(new FileInputStream(xmlFile)));XSSFWorkbook xssfWorkbook = new XSSFWorkbook();DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();Document doc = builder.parse(xmlFile);NodeList styleList = doc.getElementsByTagName("Style");for (int i = 0; i < styleList.getLength(); i++) {XSSFCellStyle style = xssfWorkbook.createCellStyle();Node node = styleList.item(i);NodeList childNodes = node.getChildNodes();for (int i1 = 0; i1 < childNodes.getLength(); i1++) {Node childNode = childNodes.item(i1);String name = childNode.getNodeName();switch (name) {case "Alignment": {NamedNodeMap attributes = childNode.getAttributes();for (int i2 = 0; i2 < attributes.getLength(); i2++) {Node attr = attributes.item(i2);String value = attr.getNodeValue();switch (attr.getNodeName()) {case "ss:Horizontal": {style.setAlignment(HorizontalAlignment.valueOf(value.toUpperCase()));}break;case "ss:Vertical": {style.setVerticalAlignment(VerticalAlignment.valueOf(value.toUpperCase()));}break;case "ss:WrapText": {style.setWrapText("1".equals(value));}break;}}}break;case "Borders": {NodeList borders = childNode.getChildNodes();for (int i2 = 0; i2 < borders.getLength(); i2++) {Node border = borders.item(i2);NamedNodeMap attributes = border.getAttributes();if (attributes == null)continue;for (int i3 = 0; i3 < attributes.getLength(); i3++) {Node attr = attributes.item(i3);String value = attr.getNodeValue();switch (attr.getNodeName()) {case "ss:Position": {switch (value) {case "Bottom":style.setBorderBottom(BorderStyle.THIN);break;case "Left":style.setBorderLeft(BorderStyle.THIN);break;case "Right":style.setBorderRight(BorderStyle.THIN);break;case "Top":style.setBorderTop(BorderStyle.THIN);break;}}break;case "ss:LineStyle": {}break;case "ss:Weight": {}break;}}}}break;case "Font": {Font font = xssfWorkbook.createFont();NamedNodeMap attributes = childNode.getAttributes();for (int i2 = 0; i2 < attributes.getLength(); i2++) {Node attr = attributes.item(i2);String value = attr.getNodeValue();switch (attr.getNodeName()) {case "ss:FontName": {font.setFontName(value);}break;case "ss:CharSet": {font.setCharSet(Integer.parseInt(value));}break;case "ss:Size": {font.setFontHeightInPoints(Short.parseShort(value));}break;case "ss:Bold": {font.setBold("1".equals(value));}break;}}style.setFont(font);}break;case "Interior": {NamedNodeMap attributes = childNode.getAttributes();for (int i2 = 0; i2 < attributes.getLength(); i2++) {Node attr = attributes.item(i2);String value = attr.getNodeValue();switch (attr.getNodeName()) {case "ss:Color": {XSSFColor color = new XSSFColor(convertColorHexToByteArray(value),xssfWorkbook.getStylesSource().getIndexedColors());style.setFillForegroundColor(color);}break;case "ss:Pattern": {switch (value) {case "Solid":style.setFillPattern(FillPatternType.SOLID_FOREGROUND);break;}}break;}}}break;}}String id = node.getAttributes().getNamedItem("ss:ID").getNodeValue();cellStyleMap.put(id, style);}for (String sheetName : workbook.getSheetNames()) {Worksheet sheet = workbook.getWorksheet(sheetName);XSSFSheet xssfSheet = xssfWorkbook.createSheet(sheetName);int rowIndex = 0;for (Row row : sheet.getRows()) {XSSFRow xssfRow = xssfSheet.createRow(row.getIndex() - 1);int colIndex = 0;int preColIndex = 0;for (Cell cell : row.getCells()) {int mergeAcross = cell.getMergeAcross();int mergeDown = cell.getMergeDown();if (cell.getIndex() - preColIndex != 1) {colIndex += cell.getIndex() - preColIndex - 1;}XSSFCell xssfCell = xssfRow.createCell(colIndex);CellRangeAddress range = new CellRangeAddress(rowIndex, rowIndex + mergeDown,colIndex, colIndex + mergeAcross);CellStyle cellStyle = cellStyleMap.get(cell.getStyleID());xssfCell.setCellStyle(cellStyle);if (mergeDown != 0 || mergeAcross != 0) {boolean isMerged = false;for (CellRangeAddress mergedRange : mergedRanges) {if (colIndex >= mergedRange.getFirstColumn()&& colIndex <= mergedRange.getLastColumn()&& rowIndex >= mergedRange.getFirstRow()&& rowIndex <= mergedRange.getLastRow()) {colIndex += mergeAcross + 1;isMerged = true;break;}}if (isMerged) {continue;}mergedRanges.add(range);xssfSheet.addMergedRegion(range);}if (cell.getFormula() != null) {xssfCell.setCellFormula(convertFormula(cell.getFormula(), rowIndex, colIndex));} else {Object data = cell.getData();if (data instanceof String) {xssfCell.setCellValue((String) data);} else if (data instanceof Double) {xssfCell.setCellValue((Double) data);} else if (data instanceof Integer) {xssfCell.setCellValue((Integer) data);} else if (data instanceof Short) {xssfCell.setCellValue((Short) data);} else if (data instanceof Float) {xssfCell.setCellValue((Float) data);} else if (data instanceof Date) {xssfCell.setCellValue((Date) data);} else if (data instanceof Calendar) {xssfCell.setCellValue((Calendar) data);} else if (data instanceof RichTextString) {xssfCell.setCellValue((RichTextString) data);} else {xssfCell.setCellValue(cell.getData$());}}preColIndex = cell.getIndex();xssfSheet.autoSizeColumn(colIndex);colIndex += mergeAcross + 1;}rowIndex++;}for (CellRangeAddress range : mergedRanges) {CellStyle cellStyle = xssfSheet.getRow(range.getFirstRow()).getCell(range.getFirstColumn()).getCellStyle();RegionUtil.setBorderTop(cellStyle.getBorderBottomEnum(), range, xssfSheet);RegionUtil.setBorderBottom(cellStyle.getBorderBottomEnum(), range, xssfSheet);RegionUtil.setBorderLeft(cellStyle.getBorderBottomEnum(), range, xssfSheet);RegionUtil.setBorderRight(cellStyle.getBorderBottomEnum(), range, xssfSheet);}mergedRanges.clear();}xssfWorkbook.write(new FileOutputStream(outFile));xssfWorkbook.close();Desktop.getDesktop().open(outFile);

相关文章:

Office Xml 2003转XLSX

一、使用到的依赖包 1、xelem-3.1.jar 下载地址&#xff1a;管网下载地址 2、poi-3.17.jar 下载地址&#xff1a;https://mvnrepository.com/artifact/org.apache.poi/poi 二、实现方法 1、Xml2003公式转XLSX公式算法 &#xff08;1&#xff09;Xml2003函数格式 SUM(R[-1…...

skyWalking搭建(一)

title: “SkyWalking搭建(一)” createTime: 2021-07-27T14:34:2108:00 updateTime: 2021-07-27T14:34:2108:00 draft: false author: “name” tags: [“skywalking”] categories: [“java”] description: “测试的” 基于 docker 部署 skywalking 并实现 SpringBoot 全链路…...

Golang开发--sync.WaitGroup

sync.WaitGroup 是 Go 语言标准库中的一个并发原语&#xff0c;用于等待一组并发操作的完成。它提供了一种简单的方式来跟踪一组 goroutine 的执行状态&#xff0c;并在所有 goroutine 完成后恢复执行。 下面是关于 sync.WaitGroup 的实现细节的详细解释&#xff1a; 创建 Wa…...

Linux命令教程:使用cat命令查看和处理文件

文章目录 教程&#xff1a;使用cat命令在Linux中查看和处理文件1. 引言2. cat命令的基本概述3. 查看文件内容4. 创建文件5. 文件重定向和管道6. 格式化和编辑文件7. 实际应用示例7.1 使用cat命令浏览日志文件7.2 利用cat命令合并多个配置文件7.3 使用cat命令将文件内容发送到其…...

Websocket集群解决方案以及实战(附图文源码)

最近在项目中在做一个消息推送的功能&#xff0c;比如客户下单之后通知给给对应的客户发送系统通知&#xff0c;这种消息推送需要使用到全双工的websocket推送消息。 所谓的全双工表示客户端和服务端都能向对方发送消息。不使用同样是全双工的http是因为http只能由客户端主动发…...

科技的成就(五十一)

397、初等数论的不可解问题 1936 年 4 月&#xff0c;邱奇证明判定性问题不可解。33 岁的邱奇发表论文《初等数论的不可解问题》&#xff0c;运用λ演算给出了判定性问题一个否定的答案。λ演算是一套从数学逻辑中发展起来的形式系统&#xff0c;采用变量绑定和替换&#xff0c…...

Tomcat8 任意写文件PUT方法 (CVE-2017-12615)

Tomcat 任意写文件PUT方法 (CVE-2017-12615) 文章目录 Tomcat 任意写文件PUT方法 (CVE-2017-12615)1 在线漏洞解读:2 版本影响3 环境搭建4 漏洞复现4.1 访问4.2 POC攻击点4.2.1 直接发送以下数据包&#xff0c;然后shell将被写入Web根目录。4.2.2 访问是否通&#xff0c;可以访…...

SAP服务器修改主机名操作手册

1、业务背景 SAP服务器P2V:虚拟化后的服务器主机名(或叫计算机名、设备名,hostname,下文同)会和原参照克隆的服务器主机名一样,若两台服务器处于同一网域,会出现域冲突,导致以下事故发生 (1)、使得原服务器出现掉域情况(DEV->CLN->PRD后台服务器访问失效) …...

【大数据】Doris 构建实时数仓落地方案详解(一):实时数据仓库概述

本系列包含&#xff1a; Doris 构建实时数仓落地方案详解&#xff08;一&#xff09;&#xff1a;实时数据仓库概述Doris 构建实时数仓落地方案详解&#xff08;二&#xff09;&#xff1a;Doris 核心功能解读Doris 构建实时数仓落地方案详解&#xff08;三&#xff09;&#…...

C++ list容器的实现及讲解

所需要的基础知识 对C类的基本了解 默认构造函数 操作符重载 this指针 引用 模板等知识具有一定的了解&#xff0c;阅读该文章会很轻松。 链表节点 template<class T>struct list_node{T _data;list_node<T>* _next;list_node<T>* _prev;list_node(const T&…...

前端项目练习(练习-002-NodeJS项目初始化)

首先&#xff0c;创建一个web-002项目&#xff0c;内容和web-001一样。 下一步&#xff0c;规范一下项目结构&#xff0c;将html&#xff0c;js&#xff0c;css三个文件放到 src/view目录下面&#xff1a; 由于html引入css和js时&#xff0c;使用的是相对路径&#xff0c;所以…...

C++QT day11

绘制时钟 widget.h #ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include <QPaintEvent>//绘制事件类 #include <QDebug>//信息调试类 #include <QPainter>//画家类 #include <QTimer>//定时器类 #include <QTime> #include &…...

Stable DIffusion 炫酷应用 | AI嵌入艺术字+光影光效

目录 1 生成AI艺术字基本流程 1.1 生成黑白图 1.2 启用ControlNet 参数设置 1.3 选择大模型 写提示词 2 不同效果组合 2.1 更改提示词 2.2 更改ControlNet 2.2.1 更改模型或者预处理器 2.2.2 更改参数 3. 其他应用 3.1 AI光影字 本节需要用到ControlNet&#xff0c;可…...

C#通过重写Panel改变边框颜色与宽度的方法

在C#中,Panel控件是一个容器控件,用于在窗体或用户控件中创建一个可用于容纳其他控件的面板。Panel提供了一种将相关控件组合在一起并进行布局的方式。以下是Panel控件的详细使用方法: 在窗体上放置 Panel 控件: 在 Visual Studio 的窗体设计器中,从工具箱中拖动并放置一…...

Vue2+ElementUI 静态首页案例

源码 <template><div class"app-container home"><el-row type"flex" justify"space-around" class"row-bg"><el-card class"box-card cardDiv1"><el-col :span"5"><div clas…...

Linux的socket通信

关于套接字通信定义如下&#xff1a; 套接字对应程序猿来说就是一套网络通信的接口&#xff0c;使用这套接口就可以完成网络通信。网络通信的主体主要分为两部分&#xff1a;客户端和服务器端。在客户端和服务器通信的时候需要频繁提到三个概念&#xff1a;IP、端口、通信数据&…...

MySQL学习大纲

了解 MySQL 的基础知识和命令是使用此数据库的前提。以下是一些必须了解的 MySQL 概念和命令&#xff0c;包括基础的 CRUD&#xff08;创建&#xff0c;读取&#xff0c;更新&#xff0c;删除&#xff09;操作&#xff0c;以及一些高级功能&#xff1a; 1. 安装和启动 命令su…...

【Ambari】银河麒麟V10 ARM64架构_安装Ambari2.7.6HDP3.3.1(HiDataPlus)

&#x1f341; 博主 "开着拖拉机回家"带您 Go to New World.✨&#x1f341; &#x1f984; 个人主页——&#x1f390;开着拖拉机回家_大数据运维-CSDN博客 &#x1f390;✨&#x1f341; &#x1fa81;&#x1f341; 希望本文能够给您带来一定的帮助&#x1f338;文…...

驱动开发练习,platform实现如下功能

实验要求 驱动代码 #include <linux/init.h> #include <linux/module.h> #include <linux/platform_device.h> #include <linux/mod_devicetable.h> #include <linux/of_gpio.h> #include <linux/unistd.h> #include <linux/interrupt…...

QT之QString的用法介绍

QT之QString的用法介绍 成员函数常见用法 成员函数 1&#xff09;QString &append(const QString &str) 将 str 字符串追加到当前字符串末尾&#xff0c;并返回修改后的 QString 对象的引用。 2&#xff09;QString &prepend(const QString &str) 将 str 字符…...

第19节 Node.js Express 框架

Express 是一个为Node.js设计的web开发框架&#xff0c;它基于nodejs平台。 Express 简介 Express是一个简洁而灵活的node.js Web应用框架, 提供了一系列强大特性帮助你创建各种Web应用&#xff0c;和丰富的HTTP工具。 使用Express可以快速地搭建一个完整功能的网站。 Expre…...

如何在看板中体现优先级变化

在看板中有效体现优先级变化的关键措施包括&#xff1a;采用颜色或标签标识优先级、设置任务排序规则、使用独立的优先级列或泳道、结合自动化规则同步优先级变化、建立定期的优先级审查流程。其中&#xff0c;设置任务排序规则尤其重要&#xff0c;因为它让看板视觉上直观地体…...

Ascend NPU上适配Step-Audio模型

1 概述 1.1 简述 Step-Audio 是业界首个集语音理解与生成控制一体化的产品级开源实时语音对话系统&#xff0c;支持多语言对话&#xff08;如 中文&#xff0c;英文&#xff0c;日语&#xff09;&#xff0c;语音情感&#xff08;如 开心&#xff0c;悲伤&#xff09;&#x…...

网络编程(UDP编程)

思维导图 UDP基础编程&#xff08;单播&#xff09; 1.流程图 服务器&#xff1a;短信的接收方 创建套接字 (socket)-----------------------------------------》有手机指定网络信息-----------------------------------------------》有号码绑定套接字 (bind)--------------…...

Map相关知识

数据结构 二叉树 二叉树&#xff0c;顾名思义&#xff0c;每个节点最多有两个“叉”&#xff0c;也就是两个子节点&#xff0c;分别是左子 节点和右子节点。不过&#xff0c;二叉树并不要求每个节点都有两个子节点&#xff0c;有的节点只 有左子节点&#xff0c;有的节点只有…...

Java线上CPU飙高问题排查全指南

一、引言 在Java应用的线上运行环境中&#xff0c;CPU飙高是一个常见且棘手的性能问题。当系统出现CPU飙高时&#xff0c;通常会导致应用响应缓慢&#xff0c;甚至服务不可用&#xff0c;严重影响用户体验和业务运行。因此&#xff0c;掌握一套科学有效的CPU飙高问题排查方法&…...

JVM虚拟机:内存结构、垃圾回收、性能优化

1、JVM虚拟机的简介 Java 虚拟机(Java Virtual Machine 简称:JVM)是运行所有 Java 程序的抽象计算机,是 Java 语言的运行环境,实现了 Java 程序的跨平台特性。JVM 屏蔽了与具体操作系统平台相关的信息,使得 Java 程序只需生成在 JVM 上运行的目标代码(字节码),就可以…...

【VLNs篇】07:NavRL—在动态环境中学习安全飞行

项目内容论文标题NavRL: 在动态环境中学习安全飞行 (NavRL: Learning Safe Flight in Dynamic Environments)核心问题解决无人机在包含静态和动态障碍物的复杂环境中进行安全、高效自主导航的挑战&#xff0c;克服传统方法和现有强化学习方法的局限性。核心算法基于近端策略优化…...

七、数据库的完整性

七、数据库的完整性 主要内容 7.1 数据库的完整性概述 7.2 实体完整性 7.3 参照完整性 7.4 用户定义的完整性 7.5 触发器 7.6 SQL Server中数据库完整性的实现 7.7 小结 7.1 数据库的完整性概述 数据库完整性的含义 正确性 指数据的合法性 有效性 指数据是否属于所定…...

怎么让Comfyui导出的图像不包含工作流信息,

为了数据安全&#xff0c;让Comfyui导出的图像不包含工作流信息&#xff0c;导出的图像就不会拖到comfyui中加载出来工作流。 ComfyUI的目录下node.py 直接移除 pnginfo&#xff08;推荐&#xff09;​​ 在 save_images 方法中&#xff0c;​​删除或注释掉所有与 metadata …...