2016-11-24 2 views
1

Gibt es eine Möglichkeit, Excel-Formel auf ganz Spalte relativ in Java zu setzen?Excel-Formel relativ zur gesamten Spalte relativ in Java setzen?

Zum Beispiel in Excel Spalte A und B enthält, Zahlen und Spalt C enthält zusätzlich Formel A + B

I gleiche Formel gesamte C-Säule wie C1 = A1 + B1, C2 = A2 + B2 wiederholen wollte.

Derzeit mache ich dies durch eine schmutzige Logik, die Nummer (1,2 .. So on) durch Zeilenanzahl ersetzt. Aber in realen Fällen sind Formeln sehr groß und komplex und schwer mit der obigen Taktik zu ersetzen.

Bietet Apache POI etwas, um solche Formeln relativ wie in Excel zu kopieren?

Antwort

3

Der Best Practice-Ansatz von Excel würde R1C1 Referenzen für dieses Problem verwenden. Mit diesen Referenzen könnten wir zum Beispiel =SUM(RC1:RC2) auf den Bereich C2:C10 anwenden. Die R bedeutet diese Zeile, während C1 Spalte 1 bedeutet (A) und C2 bedeutet Spalte 2 (B).

Leider unterstützt Apache Poi nicht R1C1 Referenzen. Der einzige Ansatz, den ich sehen kann, ist die Verwendung von CellReference dafür. A CellReference kann die CellRefParts als die drei Teile der Zellreferenz, der Blattname (oder Null, wenn keiner geliefert wird), die 1 basierte Zeilennummer und den A-basierten Spaltenbuchstaben erhalten. Also können wir diese Teile in den Formeln verwenden.

Beispiel:

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(); 

} 
} 
+0

Dank Alex für Ihre Eingabe. Ich untersuche diesen Ansatz. – DevD

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(); 
    } 
} 
Verwandte Themen