更改字符串(在文件名周围加上括号并获得两个反斜杠) [英] Change a String (Put brackets around file name and get TWO Backslashes)

查看:37
本文介绍了更改字符串(在文件名周围加上括号并获得两个反斜杠)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目基于 Apache POI.我正在尝试在单元格上使用公式.我的公式如下.

My project is based on Apache POI.I'm trying to use a formula on a cell. My formula is as follows.

sheet7.createRow(0).createCell(0).setCellFormula("+'C:\\Users\\Desktop\\[Test.xlsx]Average_Graph'!A2");

我使用的是 JFileChooser,它允许用户选择文件.因此每次使用该程序时都会更改文件路径.

Im using a JFileChooser, which allows users to select the file. Therefore the filepath will be changed every time the program is used.

从 JFileChooser 中,我得到如下文件路径.

From the JFileChooser, I'm getting a filepath as follows.

String filepath= "C:\Users\Desktop\Sheet.xlsx

为了正确使用公式,文件路径应采用以下格式.

In order to work the formula correctly, the filepath should be in following format.

"C:\\Users\\Desktop\\[Sheet.xlsx]"

如何更改从 JFileCHooser 获取的字符串以正确运行公式?我需要两个反斜杠而不是一个.请帮忙.

How Can I Change the string which I'm getting from the JFileCHooser to run the formula correctly? I need TWO backslashes instead of one. Please help.

推荐答案

尝试做两个替换,一个处理文件名,另一个处理路径的格式:

Try doing two replacements, one to handle the filename, the other to handle the formatting of the path:

String filepath = "C:\\Users\\Desktop\\Sheet.xlsx";
filepath = filepath.replaceAll("(?<=\\\\)([^\\\\]+)$", "[$1]").replace("\\, "\\\\");
System.out.println(filepath);

C:\\Users\\Desktop\\[Sheet.xlsx]

这篇关于更改字符串(在文件名周围加上括号并获得两个反斜杠)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆