在Excel公式的文件名前后加上方括号 [英] Put brackets around filename for Excel formula
问题描述
我的项目基于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?
推荐答案
如果您有一个 JFileChooser
,则有一个 File
对象,而不仅仅是一个 String
If you have a JFileChooser
, you have a File
object, not just a String
并且您应该始终使用 File
或 Paths
API来独立于操作系统
And you should use the File
or Paths
API anyway to be OS independent
File f = jFileChooser.getSelectedFile();
String path = f.getParent();
String name = f.getName();
String newFormat = String.format("%s[%s]", path, name);
这篇关于在Excel公式的文件名前后加上方括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!