简体   繁体   English

如何使用apache poi重命名excel中的名称

[英]how to rename a name in excel with apache poi

How do I change the reference of a name, which refers to a set of cells, with Apache poi? 如何使用Apache poi更改对一组单元格的名称的引用? I have already looked at the API and found a few functions such as getNameName() , I am unsure whether or not I have to use that or how I am supposed to use it. 我已经看过API并找到了一些函数,例如getNameName() ,我不确定是否必须使用它或应该如何使用它。

Suppose I have a name "Names of students" that refers to cells $A$2:$A$10, I want to change in java this name to refer to cells $A$2:$A$20. 假设我有一个名称“ Names of students”,它引用单元格$ A $ 2:$ A $ 10,我想在java中更改此名称以引用单元格$ A $ 2:$ A $ 20。 Does anyone know how to do this? 有谁知道如何做到这一点?

edit: I looked at the documentation and tried to come up with a sample code but even this doesn't work. 编辑:我看了看文档,并试图提出一个示例代码,但即使这样也不起作用。

code: 码:

 InputStream inp = new FileInputStream("test.xlsx");
        Workbook wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheetAt(0);
        Name name;
        name = wb.createName();
        name = name.getNameName("NameOFStudents");
        name.setRefersToFormula("=Sheet1!$A$1:$A$20");
        FileOutputStream fileout = new FileOutputStream("test2..xlsx");
        wb.write(fileout);
        fileout.close();

so i finally figured out how to do it. 所以我终于想出了怎么做。 you use the workbook variable with the function .getName() to get the name that you require and assign it to a Name variable and then use .setRefersToFormula() to change the reference 您将工作簿变量与函数.getName()来获取所需的名称,并将其分配给Name变量,然后使用.setRefersToFormula()来更改引用

code: 码:

        InputStream inp = new FileInputStream("test.xlsx");
        Workbook wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheetAt(0);
        Name name;
        name = wb.getName("NameOFStudents");
        name.setRefersToFormula("Sheet1!$A$1:$A$20");
        FileOutputStream fileout = new FileOutputStream("test2..xlsx");
        wb.write(fileout);
        fileout.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM