我的项目基于 Apache POI。我正在尝试在单元格上使用公式。 我的公式如下。
sheet7.createRow(0).createCell(0).setCellFormula("+'C:\\Users\\Desktop\\[Test.xlsx]Average_Graph'!A2");
我正在使用 JFileChooser,它允许用户选择文件。因此,每次使用程序时,文件路径都会更改。
从 JFileChooser 中,我得到如下文件路径。
String filepath= "C:\\Users\\Desktop\\Sheet.xlsx"`
为了正确使用公式,文件路径应采用以下格式。
"C:\\Users\\Desktop[Sheet.xlsx]"
如何更改从 JFileCHooser 获取的字符串以正确运行公式?
最佳答案
如果你有一个JFileChooser
,你就有一个File
对象,而不仅仅是一个String
而且您应该使用File
或Paths
API 以独立于操作系统
File f = jFileChooser.getSelectedFile();
String path = f.getParent();
String name = f.getName();
String newFormat = String.format("%s[%s]", path, name);
关于java - 将 Excel 公式的文件名括起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51999652/