在 Excel 公式的文件名周围加上括号 [英] Put brackets around filename for Excel formula

查看:57
本文介绍了在 Excel 公式的文件名周围加上括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目基于 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

并且您应该使用 FilePaths 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屋!

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