当前位置: 首页 > 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 字符…...

国防科技大学计算机基础课程笔记02信息编码

1.机内码和国标码 国标码就是我们非常熟悉的这个GB2312,但是因为都是16进制&#xff0c;因此这个了16进制的数据既可以翻译成为这个机器码&#xff0c;也可以翻译成为这个国标码&#xff0c;所以这个时候很容易会出现这个歧义的情况&#xff1b; 因此&#xff0c;我们的这个国…...

地震勘探——干扰波识别、井中地震时距曲线特点

目录 干扰波识别反射波地震勘探的干扰波 井中地震时距曲线特点 干扰波识别 有效波&#xff1a;可以用来解决所提出的地质任务的波&#xff1b;干扰波&#xff1a;所有妨碍辨认、追踪有效波的其他波。 地震勘探中&#xff0c;有效波和干扰波是相对的。例如&#xff0c;在反射波…...

rknn优化教程(二)

文章目录 1. 前述2. 三方库的封装2.1 xrepo中的库2.2 xrepo之外的库2.2.1 opencv2.2.2 rknnrt2.2.3 spdlog 3. rknn_engine库 1. 前述 OK&#xff0c;开始写第二篇的内容了。这篇博客主要能写一下&#xff1a; 如何给一些三方库按照xmake方式进行封装&#xff0c;供调用如何按…...

Appium+python自动化(十六)- ADB命令

简介 Android 调试桥(adb)是多种用途的工具&#xff0c;该工具可以帮助你你管理设备或模拟器 的状态。 adb ( Android Debug Bridge)是一个通用命令行工具&#xff0c;其允许您与模拟器实例或连接的 Android 设备进行通信。它可为各种设备操作提供便利&#xff0c;如安装和调试…...

【第二十一章 SDIO接口(SDIO)】

第二十一章 SDIO接口 目录 第二十一章 SDIO接口(SDIO) 1 SDIO 主要功能 2 SDIO 总线拓扑 3 SDIO 功能描述 3.1 SDIO 适配器 3.2 SDIOAHB 接口 4 卡功能描述 4.1 卡识别模式 4.2 卡复位 4.3 操作电压范围确认 4.4 卡识别过程 4.5 写数据块 4.6 读数据块 4.7 数据流…...

django filter 统计数量 按属性去重

在Django中&#xff0c;如果你想要根据某个属性对查询集进行去重并统计数量&#xff0c;你可以使用values()方法配合annotate()方法来实现。这里有两种常见的方法来完成这个需求&#xff1a; 方法1&#xff1a;使用annotate()和Count 假设你有一个模型Item&#xff0c;并且你想…...

质量体系的重要

质量体系是为确保产品、服务或过程质量满足规定要求&#xff0c;由相互关联的要素构成的有机整体。其核心内容可归纳为以下五个方面&#xff1a; &#x1f3db;️ 一、组织架构与职责 质量体系明确组织内各部门、岗位的职责与权限&#xff0c;形成层级清晰的管理网络&#xf…...

Module Federation 和 Native Federation 的比较

前言 Module Federation 是 Webpack 5 引入的微前端架构方案&#xff0c;允许不同独立构建的应用在运行时动态共享模块。 Native Federation 是 Angular 官方基于 Module Federation 理念实现的专为 Angular 优化的微前端方案。 概念解析 Module Federation (模块联邦) Modul…...

如何理解 IP 数据报中的 TTL?

目录 前言理解 前言 面试灵魂一问&#xff1a;说说对 IP 数据报中 TTL 的理解&#xff1f;我们都知道&#xff0c;IP 数据报由首部和数据两部分组成&#xff0c;首部又分为两部分&#xff1a;固定部分和可变部分&#xff0c;共占 20 字节&#xff0c;而即将讨论的 TTL 就位于首…...

关键领域软件测试的突围之路:如何破解安全与效率的平衡难题

在数字化浪潮席卷全球的今天&#xff0c;软件系统已成为国家关键领域的核心战斗力。不同于普通商业软件&#xff0c;这些承载着国家安全使命的软件系统面临着前所未有的质量挑战——如何在确保绝对安全的前提下&#xff0c;实现高效测试与快速迭代&#xff1f;这一命题正考验着…...