我有两个值,一个固定的值,但是一个单元格中的动态值,一旦创建了excel,用户必须能够修改它,并且根据此修改,公式将刷新另一个单元格。
Double ausencias = 4.0;
Cell cellDesgloseCalendario = rowDesglose.createCell(cellnum++);
cellDesgloseCalendario.setCellValue(160.0);//value that the user should be able to change later
////
String strFormula= "=ausencias/cellDesgloseCalendario";
cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
cell.setCellFormula(strFormula);
Cell cellDesglosePorcentajeAbsentismo = rowDesglose.createCell(cellnum++);
cellDesglosePorcentajeAbsentismo.setCellFormula(strFormula);
如何动态获取列的位置:“cellDesgloseCalendario”(e:“A10”,“B20”),因为它们是用几个循环创建的,我不知道它们的位置,创建的公式是我的变量“ausencias”
对此列进行划分答案 0 :(得分:2)
一个小区知道它的地址。可以通过Cell.getAddress获得。所以可以将地址连接成公式字符串。
但是你不能在apache poi
中用“=”开始一个公式字符串,因为这不是预期的。请参阅Formula Support - basics。
完整示例:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CreateExcelFormulaUsingCellAddress {
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet();
Row rowDesglose = sheet.createRow(0);
Double ausencias = 4.0;
int cellnum = 0;
Cell cellDesgloseCalendario = rowDesglose.createCell(cellnum++); // cellDesgloseCalendario is A1
cellDesgloseCalendario.setCellValue(160.0);
String strFormula = ausencias.toString() + "/" + cellDesgloseCalendario.getAddress().formatAsString();
// 4.0 / A1
System.out.println(strFormula); //"4.0/A1"
Cell cellDesglosePorcentajeAbsentismo = rowDesglose.createCell(cellnum++); // cellDesglosePorcentajeAbsentismo is B1
cellDesglosePorcentajeAbsentismo.setCellFormula(strFormula); //formula in B1 is now =4/A1
wb.write(new FileOutputStream("CreateExcelFormulaUsingCellAddress.xlsx"));
wb.close();
}
}