【问题标题】:apache-poi inserting filename in header or footerapache-poi 在页眉或页脚中插入文件名
【发布时间】:2013-03-06 14:05:45
【问题描述】:

我正在使用 apache poi 库 (poi-3.8-20120326.jar)

如何在 xls (hssf) 文档的页脚中添加文件名?

我的方法如下:

final static public String FILE_NAME = "&[File]";
public static void insertFilename(Sheet sheet) {
    sheet.getFooter().setLeft(FILE_NAME);
}

问题是,Microsoft Excel 2003 显示

文件]

如果我打开页脚编辑器,单击该字段,不做任何更改,然后保存——它可以工作。 编辑器显示为

&[文件]

是否有避免这种情况的解决方法或肮脏的技巧?

谢谢

【问题讨论】:

    标签: apache-poi export-to-excel excel-2003 poi-hssf


    【解决方案1】:

    它在 Excel 中可能看起来像“&[File]”,但这不是它在内部存储的方式。您正在为 .xls 文件使用 HSSF,因此请使用以下 static HeaderFooter method 获取文件名的内部 Excel 代码:

    import org.apache.poi.hssf.usermodel.HeaderFooter;
    
    String fileIndicator = HeaderFooter.file();
    

    快速查看源代码确定内部代码是字符串“&F”。

    如果有人将 XSSF 用于 .xlsx 文件,则没有对应的 file 方法。但是documentation for XSSFHeaderFooter表示可以直接使用字符串“&F”。

    【讨论】:

      猜你喜欢
      • 2010-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-25
      • 2020-09-10
      • 2015-12-14
      • 1970-01-01
      相关资源
      最近更新 更多