在Java中将excel公式设置为相对完整的列?

在Java中将excel公式设置为相对完整的列?,java,excel-formula,apache-poi,Java,Excel Formula,Apache Poi,在Java中有没有办法将excel公式设置为相对完整的列 例如,在excel中,A列和B列包含数字,C列包含加法公式A+B 我想对整个C列重复相同的公式,比如C1=A1+B1,C2=A2+B2 目前,我正在通过一些肮脏的逻辑来实现这一点,即用行计数替换数字(1,2..等等)。但在实际情况中,公式非常庞大、复杂,很难用上述策略取代 Apache POI是否提供了与excel中类似的复制此类公式的功能?excel的最佳实践方法是使用R1C1参考来解决此问题。有了这些引用,我们可以将=SUM(RC1:

在Java中有没有办法将excel公式设置为相对完整的列

例如,在excel中,A列和B列包含数字,C列包含加法公式A+B

我想对整个C列重复相同的公式,比如C1=A1+B1,C2=A2+B2

目前,我正在通过一些肮脏的逻辑来实现这一点,即用行计数替换数字(1,2..等等)。但在实际情况中,公式非常庞大、复杂,很难用上述策略取代


Apache POI是否提供了与excel中类似的复制此类公式的功能?

excel的最佳实践方法是使用
R1C1
参考来解决此问题。有了这些引用,我们可以将
=SUM(RC1:RC2)
应用于范围
C2:C10
R
表示此行,
C1
表示第1列(
A
),而
C2
表示第2列(
B

不幸的是,ApachePOI不支持
R1C1
引用。所以我能看到的唯一方法就是使用
CellReference
来实现这一点。
CellReference
可以获取单元格引用的三个部分:工作表名称(如果未提供,则为null)、基于1的行号和基于A的列字母。所以我们可以在公式中使用这些部分

例如:

import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import java.io.*;

class UsingCellReferenceInFormula {

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

    String[][] data = new String[][]{
                          {"Number1","Number2","Sum","Complex"},
                          {"123","456", null, null},
                          {"23.45","67.89", null, null},
                          {"123","-456", null, null},
                          {"-123.456","456.78", null, null}
                      };

    Workbook workbook = new XSSFWorkbook();
    Sheet sheet = workbook.createSheet();

    int r = 0;
    for(String[] dataRow : data){
        Row row = sheet.createRow(r++);
        int c = 0;
        for(String dataCell : dataRow){
            Cell cell = row.createCell(c++);
            if ( r == 1 ) cell.setCellValue(dataCell);
            else if ( c < 3 ) cell.setCellValue(Double.parseDouble(dataCell));
            else if ( c == 3 ) {
                CellReference cellReference = new CellReference(cell);
                String thisR = cellReference.getCellRefParts()[1]; 
                cell.setCellFormula("SUM(A" + thisR + ":B" + thisR + ")");
            } else if ( c == 4 ) {
                CellReference cellReference = new CellReference(cell);
                String thisR = cellReference.getCellRefParts()[1]; 
                cell.setCellFormula("IF(AND(A" + thisR + ">0,B" + thisR + ">0),SUM(A" + thisR + ":B" + thisR + "),MAX(A" + thisR + ":B" + thisR + "))");
            }
        }
    }


    FileOutputStream fileOut = new FileOutputStream("UsingCellReferenceInFormula.xlsx");
    workbook.write(fileOut);
    workbook.close();

 }
}
import org.apache.poi.xssf.usermodel.*;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.ss.util.*;
导入java.io.*;
使用CellReferenceInformula的类{
公共静态void main(字符串[]args)引发IOException{
字符串[][]数据=新字符串[][]{
{“Number1”、“Number2”、“Sum”、“Complex”},
{“123”,“456”,空,空},
{“23.45”,“67.89”,空,空},
{“123”,“-456”,空,空},
{“-123.456”,“456.78”,空,空}
};
工作簿=新的XSSF工作簿();
工作表=工作簿.createSheet();
int r=0;
for(字符串[]数据行:数据){
Row-Row=sheet.createRow(r++);
int c=0;
for(字符串数据单元:dataRow){
Cell-Cell=row.createCell(c++);
如果(r==1)cell.setCellValue(dataCell);
如果(c<3)cell.setCellValue(Double.parseDouble(dataCell));
else如果(c==3){
CellReference CellReference=新的CellReference(单元格);
字符串thisR=cellReference.getCellRefParts()[1];
cell.setCellFormula(“总和(A“+thisR+”:B“+thisR+”));
}else如果(c==4){
CellReference CellReference=新的CellReference(单元格);
字符串thisR=cellReference.getCellRefParts()[1];
cell.setCellFormula(“如果(和(A“+thisR+”>0,B“+thisR+”>0),求和(A“+thisR+”:B“+thisR+”),最大值(A“+thisR+”:B“+thisR+”);
}
}
}
FileOutputStream fileOut=新的FileOutputStream(“UsingCellReferenceInFormula.xlsx”);
工作簿。写入(归档);
workbook.close();
}
}
导入org.apache.poi.xssf.usermodel.*;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.ss.util.*;
导入java.io.*;
使用CellReferenceInformula的类{
公共静态void main(字符串[]args)引发IOException{
字符串[][]数据=新字符串[][]{
{“Number1”、“Number2”、“Sum”、“Complex”},
{“123”,“456”,空,空},
{“23.45”,“67.89”,空,空},
{“123”,“-456”,空,空},
{“-123.456”,“456.78”,空,空}
};
工作簿=新的XSSF工作簿();
工作表=工作簿.createSheet();
int r=0;
for(字符串[]数据行:数据){
Row-Row=sheet.createRow(r++);
int c=0;
for(字符串数据单元:dataRow){
Cell-Cell=row.createCell(c++);
如果(r==1)cell.setCellValue(dataCell);
如果(c<3)cell.setCellValue(Double.parseDouble(dataCell));
else如果(c==3){
CellReference CellReference=新的CellReference(单元格);
字符串thisR=cellReference.getCellRefParts()[1];
cell.setCellFormula(“总和(A“+thisR+”:B“+thisR+”));
}else如果(c==4){
CellReference CellReference=新的CellReference(单元格);
字符串thisR=cellReference.getCellRefParts()[1];
cell.setCellFormula(“如果(和(A“+thisR+”>0,B“+thisR+”>0),求和(A“+thisR+”:B“+thisR+”),最大值(A“+thisR+”:B“+thisR+”);
}
}
}
FileOutputStream fileOut=新的FileOutputStream(“UsingCellReferenceInFormula.xlsx”);
工作簿。写入(归档);
workbook.close();
}
}

感谢Alex的支持。我正在研究这种方法。通过在
R1C1
模式下使用函数,我成功地解决了这个问题。在我的公式中,我编写了例如
间接(“R[0]C[-1]”,0)
来访问当前单元格的左侧单元格。
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import java.io.*;

class UsingCellReferenceInFormula {
    public static void main(String[] args) throws IOException{
        String[][] data = new String[][]{
                              {"Number1","Number2","Sum","Complex"},
                              {"123","456", null, null},
                              {"23.45","67.89", null, null},
                              {"123","-456", null, null},
                              {"-123.456","456.78", null, null}
                          };

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();

        int r = 0;
        for(String[] dataRow : data){
            Row row = sheet.createRow(r++);
            int c = 0;
            for(String dataCell : dataRow){
                Cell cell = row.createCell(c++);
                if ( r == 1 ) cell.setCellValue(dataCell);
                else if ( c < 3 ) cell.setCellValue(Double.parseDouble(dataCell));
                else if ( c == 3 ) {
                    CellReference cellReference = new CellReference(cell);
                    String thisR = cellReference.getCellRefParts()[1]; 
                    cell.setCellFormula("SUM(A" + thisR + ":B" + thisR + ")");
                } else if ( c == 4 ) {
                    CellReference cellReference = new CellReference(cell);
                    String thisR = cellReference.getCellRefParts()[1]; 
                    cell.setCellFormula("IF(AND(A" + thisR + ">0,B" + thisR + ">0),SUM(A" + thisR + ":B" + thisR + "),MAX(A" + thisR + ":B" + thisR + "))");
                }
            }
        }
        FileOutputStream fileOut = new FileOutputStream("UsingCellReferenceInFormula.xlsx");
        workbook.write(fileOut);
        workbook.close();
    }
}