2017-01-14 30 views
0

我创建了一个XSSFTable下面的示例代码:如何设置公式有表列字段中的Apache POI

我XSSFTable

https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateTable.java

一列是一个公式,引用该表中的另一列。

例如,在XSSFTable TBLColumnA,其公式为:=[@[ColumnB]],我可以通过cell.setCellFormula("TBL[[#This Row],[ColumnB]]")ColumnA每个小区设定的公式,但同时在Excel中打开将有问题和Excel具有以除去式,以便正确显示工作表。

此问题仅在创建空白的新XSSFWorkbook时发生,如果它是从Excel创建的现有.xlsx文件加载的,则可以通过cell.setCellFormula()修改公式并能够在Excel中正确打开。

如果有什么样的代码可以在这种情况下正常工作?

回答

1

与链接的一例的主要问题是,它的名字中的所有列等于“列”:

... 
     for(int i=0; i<3; i++) { 
      //Create column 
      column = columns.addNewTableColumn(); 
      column.setName("Column"); 
      column.setId(i+1); 
... 

所以公式解析器不能在它们之间的区别。

但是填充表列标题和使用一个循环填充工作表内容的整个逻辑并不能真正理解。所以这里是一个更合适的例子:

public class CreateTable { 

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

     Workbook wb = new XSSFWorkbook(); 
     XSSFSheet sheet = (XSSFSheet) wb.createSheet(); 

     //Create 
     XSSFTable table = sheet.createTable(); 
     table.setDisplayName("Test");  
     CTTable cttable = table.getCTTable(); 

     //Style configurations 
     CTTableStyleInfo style = cttable.addNewTableStyleInfo(); 
     style.setName("TableStyleMedium2"); 
     style.setShowColumnStripes(false); 
     style.setShowRowStripes(true); 

     //Set which area the table should be placed in 
     AreaReference reference = new AreaReference(new CellReference(0, 0), 
       new CellReference(4,2)); 
     cttable.setRef(reference.formatAsString()); 
     cttable.setId(1); 
     cttable.setName("Test"); 
     cttable.setTotalsRowCount(1); 

     CTTableColumns columns = cttable.addNewTableColumns(); 
     columns.setCount(3); 
     CTTableColumn column; 
     XSSFRow row; 
     XSSFCell cell; 

     //Create 3 columns in table 
     for(int i=0; i<3; i++) { 
      column = columns.addNewTableColumn(); 
      column.setName("Column"+i); 
      column.setId(i+1); 
     } 

     //Create sheet contents 
     for(int i=0; i<5; i++) {//Create 5 rows 
      row = sheet.createRow(i); 
      for(int j=0; j<3; j++) {//Create 3 cells each row 
       cell = row.createCell(j); 
       if(i == 0) { //first row is for column headers 
        cell.setCellValue("Column"+j); 
       } else if(i<4){ //next rows except last row are data rows, last row is totals row so don't put something in 
        if (j<2) cell.setCellValue((i+1)*(j+1)); //two data columns 
        else cell.setCellFormula("Test[[#This Row],[Column0]]*Test[[#This Row],[Column1]]"); //one formula column 
       } 
      } 
     } 

     FileOutputStream fileOut = new FileOutputStream("ooxml-table.xlsx"); 
     wb.write(fileOut); 
     fileOut.close(); 
     wb.close(); 
    } 
} 
+0

嗨,非常感谢,它解决了! – Leo