在Java中将excel公式设置为整列?

时间:2016-11-24 19:51:53

标签: java excel-formula apache-poi

有没有办法在Java中相对地将excel公式设置为整个列?

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

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

目前我正在通过一些脏行逻辑执行此操作,该逻辑将行数替换为Number(1,2 ..so on)。但在实际情况下,公式非常庞大且复杂,难以用上述策略取代。

Apache POI是否提供了复制此类公式的任何内容,就像在Excel中一样?

2 个答案:

答案 0 :(得分:3)

Excel的最佳实践方法是针对此问题使用R1C1引用。通过此引用,我们可以将=SUM(RC1:RC2)应用于范围C2:C10R表示此行,C1表示第1列(A),C2表示第2列(B)。

不幸的是,apache poi不支持R1C1引用。所以我能看到的唯一方法就是使用CellReferenceCellReference可以将CellRefParts作为单元格引用的三个部分,工作表名称(如果没有提供则为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();

 }
}

答案 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();
    }
}
相关问题