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 下载地址:管网下载地址 2、poi-3.17.jar 下载地址:https://mvnrepository.com/artifact/org.apache.poi/poi 二、实现方法 1、Xml2003公式转XLSX公式算法 (1)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 语言标准库中的一个并发原语,用于等待一组并发操作的完成。它提供了一种简单的方式来跟踪一组 goroutine 的执行状态,并在所有 goroutine 完成后恢复执行。 下面是关于 sync.WaitGroup 的实现细节的详细解释: 创建 Wa…...
Linux命令教程:使用cat命令查看和处理文件
文章目录 教程:使用cat命令在Linux中查看和处理文件1. 引言2. cat命令的基本概述3. 查看文件内容4. 创建文件5. 文件重定向和管道6. 格式化和编辑文件7. 实际应用示例7.1 使用cat命令浏览日志文件7.2 利用cat命令合并多个配置文件7.3 使用cat命令将文件内容发送到其…...

Websocket集群解决方案以及实战(附图文源码)
最近在项目中在做一个消息推送的功能,比如客户下单之后通知给给对应的客户发送系统通知,这种消息推送需要使用到全双工的websocket推送消息。 所谓的全双工表示客户端和服务端都能向对方发送消息。不使用同样是全双工的http是因为http只能由客户端主动发…...
科技的成就(五十一)
397、初等数论的不可解问题 1936 年 4 月,邱奇证明判定性问题不可解。33 岁的邱奇发表论文《初等数论的不可解问题》,运用λ演算给出了判定性问题一个否定的答案。λ演算是一套从数学逻辑中发展起来的形式系统,采用变量绑定和替换,…...

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 直接发送以下数据包,然后shell将被写入Web根目录。4.2.2 访问是否通,可以访…...
SAP服务器修改主机名操作手册
1、业务背景 SAP服务器P2V:虚拟化后的服务器主机名(或叫计算机名、设备名,hostname,下文同)会和原参照克隆的服务器主机名一样,若两台服务器处于同一网域,会出现域冲突,导致以下事故发生 (1)、使得原服务器出现掉域情况(DEV->CLN->PRD后台服务器访问失效) …...

【大数据】Doris 构建实时数仓落地方案详解(一):实时数据仓库概述
本系列包含: Doris 构建实时数仓落地方案详解(一):实时数据仓库概述Doris 构建实时数仓落地方案详解(二):Doris 核心功能解读Doris 构建实时数仓落地方案详解(三)&#…...

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

前端项目练习(练习-002-NodeJS项目初始化)
首先,创建一个web-002项目,内容和web-001一样。 下一步,规范一下项目结构,将html,js,css三个文件放到 src/view目录下面: 由于html引入css和js时,使用的是相对路径,所以…...

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,可…...

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通信
关于套接字通信定义如下: 套接字对应程序猿来说就是一套网络通信的接口,使用这套接口就可以完成网络通信。网络通信的主体主要分为两部分:客户端和服务器端。在客户端和服务器通信的时候需要频繁提到三个概念:IP、端口、通信数据&…...
MySQL学习大纲
了解 MySQL 的基础知识和命令是使用此数据库的前提。以下是一些必须了解的 MySQL 概念和命令,包括基础的 CRUD(创建,读取,更新,删除)操作,以及一些高级功能: 1. 安装和启动 命令su…...

【Ambari】银河麒麟V10 ARM64架构_安装Ambari2.7.6HDP3.3.1(HiDataPlus)
🍁 博主 "开着拖拉机回家"带您 Go to New World.✨🍁 🦄 个人主页——🎐开着拖拉机回家_大数据运维-CSDN博客 🎐✨🍁 🪁🍁 希望本文能够给您带来一定的帮助🌸文…...

驱动开发练习,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)QString &append(const QString &str) 将 str 字符串追加到当前字符串末尾,并返回修改后的 QString 对象的引用。 2)QString &prepend(const QString &str) 将 str 字符…...
树莓派超全系列教程文档--(62)使用rpicam-app通过网络流式传输视频
使用rpicam-app通过网络流式传输视频 使用 rpicam-app 通过网络流式传输视频UDPTCPRTSPlibavGStreamerRTPlibcamerasrc GStreamer 元素 文章来源: http://raspberry.dns8844.cn/documentation 原文网址 使用 rpicam-app 通过网络流式传输视频 本节介绍来自 rpica…...

练习(含atoi的模拟实现,自定义类型等练习)
一、结构体大小的计算及位段 (结构体大小计算及位段 详解请看:自定义类型:结构体进阶-CSDN博客) 1.在32位系统环境,编译选项为4字节对齐,那么sizeof(A)和sizeof(B)是多少? #pragma pack(4)st…...

dedecms 织梦自定义表单留言增加ajax验证码功能
增加ajax功能模块,用户不点击提交按钮,只要输入框失去焦点,就会提前提示验证码是否正确。 一,模板上增加验证码 <input name"vdcode"id"vdcode" placeholder"请输入验证码" type"text&quo…...

linux arm系统烧录
1、打开瑞芯微程序 2、按住linux arm 的 recover按键 插入电源 3、当瑞芯微检测到有设备 4、松开recover按键 5、选择升级固件 6、点击固件选择本地刷机的linux arm 镜像 7、点击升级 (忘了有没有这步了 估计有) 刷机程序 和 镜像 就不提供了。要刷的时…...
【Go语言基础【12】】指针:声明、取地址、解引用
文章目录 零、概述:指针 vs. 引用(类比其他语言)一、指针基础概念二、指针声明与初始化三、指针操作符1. &:取地址(拿到内存地址)2. *:解引用(拿到值) 四、空指针&am…...
【JavaSE】多线程基础学习笔记
多线程基础 -线程相关概念 程序(Program) 是为完成特定任务、用某种语言编写的一组指令的集合简单的说:就是我们写的代码 进程 进程是指运行中的程序,比如我们使用QQ,就启动了一个进程,操作系统就会为该进程分配内存…...
MinIO Docker 部署:仅开放一个端口
MinIO Docker 部署:仅开放一个端口 在实际的服务器部署中,出于安全和管理的考虑,我们可能只能开放一个端口。MinIO 是一个高性能的对象存储服务,支持 Docker 部署,但默认情况下它需要两个端口:一个是 API 端口(用于存储和访问数据),另一个是控制台端口(用于管理界面…...
智能职业发展系统:AI驱动的职业规划平台技术解析
智能职业发展系统:AI驱动的职业规划平台技术解析 引言:数字时代的职业革命 在当今瞬息万变的就业市场中,传统的职业规划方法已无法满足个人和企业的需求。据统计,全球每年有超过2亿人面临职业转型困境,而企业也因此遭…...
多元隐函数 偏导公式
我们来推导隐函数 z z ( x , y ) z z(x, y) zz(x,y) 的偏导公式,给定一个隐函数关系: F ( x , y , z ( x , y ) ) 0 F(x, y, z(x, y)) 0 F(x,y,z(x,y))0 🧠 目标: 求 ∂ z ∂ x \frac{\partial z}{\partial x} ∂x∂z、 …...
用神经网络读懂你的“心情”:揭秘情绪识别系统背后的AI魔法
用神经网络读懂你的“心情”:揭秘情绪识别系统背后的AI魔法 大家好,我是Echo_Wish。最近刷短视频、看直播,有没有发现,越来越多的应用都开始“懂你”了——它们能感知你的情绪,推荐更合适的内容,甚至帮客服识别用户情绪,提升服务体验。这背后,神经网络在悄悄发力,撑起…...