首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >APACHE按列名格式设置公式

APACHE按列名格式设置公式
EN

Stack Overflow用户
提问于 2022-04-27 10:58:52
回答 1查看 378关注 0票数 0

当我想按列名格式将公式设置为单元格时,有小问题。让我举个例子:

在excel文件中,我可以执行如下=Name的操作

因此,从colmn B值复制到列A。

但是当我试图在Apache POI中做这样的事情时

代码语言:javascript
运行
复制
  cell.setCellFormula("[Name]");

我有例外:

在指定的公式'Name‘中,在char 0 '[’‘附近解析错误。预期数字、字符串、定义的名称或数据表“

我如何处理这种情况呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-27 12:44:56

公式=[Name]对Excel表的结构化引用。但这是一个很短的不合格的形式。完全限定的表单将是=tableName[[#This Row],[Name]],而该完全限定的表单也将被存储,因此必须使用apache poi进行设置。

让我们有一个完整的例子:

代码语言:javascript
运行
复制
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;

class CreateExcelTableUsingStructuredReferences {

 public static void main(String[] args) throws Exception {

  try (XSSFWorkbook workbook = new XSSFWorkbook();
       FileOutputStream fileout = new FileOutputStream("./Excel.xlsx") ) {

   //data
   String[] tableHeadings = new String[]{"Id", "Name", "Description"};
   String[] tableContent = new String[]{null, "Name1", "Description1"};
   //variables
   String tableName = "Table1";
   int firstRow = 0; //start table in row 1
   int firstCol = 0; //start table in column A
   int rows = 2; //we have to populate headings row and 1 data row
   int cols = 3; //three columns in each row
   
   //prepairing the sheet
   XSSFSheet sheet = workbook.createSheet();
   
   //set sheet content  
   for (int r = 0; r < rows; r++) { 
    XSSFRow row = sheet.createRow(firstRow+r);
    for (int c = 0; c < cols; c++) { 
     XSSFCell localXSSFCell = row.createCell(firstCol+c);
     if (r == 0) {
      localXSSFCell.setCellValue(tableHeadings[c]);
     } else {
      localXSSFCell.setCellValue(tableContent[c]);
     }
    }
   }
   
   //create the table
   CellReference topLeft = new CellReference(sheet.getRow(firstRow).getCell(firstCol));
   CellReference bottomRight = new CellReference(sheet.getRow(firstRow+rows-1).getCell(firstCol+cols-1));
   AreaReference tableArea = workbook.getCreationHelper().createAreaReference(topLeft, bottomRight);
   XSSFTable dataTable = sheet.createTable(tableArea);
   dataTable.setName(tableName);
   dataTable.setDisplayName(tableName);
   
   //set table column formula
   dataTable.getCTTable().getTableColumns().getTableColumnList().get(0).addNewCalculatedColumnFormula().setStringValue(
      tableName + "[[#This Row],[Name]]");

   //set the formula in sheet
   XSSFCell formulaCell = sheet.getRow(firstRow+1).getCell(firstCol);
   formulaCell.setCellFormula(tableName + "[[#This Row],[Name]]");
   //following is not necessary up to apache poi 5.1.0, but later versions of apache poi uses formula parser which damages structured table formulas
   formulaCell.getCTCell().getF().setStringValue(tableName + "[[#This Row],[Name]]");
   
   workbook.write(fileout);
  }

 }
}

这可以使用apache poi 4或更高版本。

注意,附加的formulaCell.getCTCell().getF().setStringValue(tableName + "[[#This Row],[Name]]");只需要在5.1.0之后使用apache poi版本。apache poi的后期版本使用了一个公式解析器,该解析器损坏了结构化表公式,因此必须使用此代码行重置它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72027601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档