繁体   English   中英

如何使用poi从xlsx文件中删除公式?

[英]How to delete formula from xlsx file using poi?

我有一个xlsx文件,其中包含一些单元格的公式。 我知道行的索引以及包含公式的单元格。 我只需要删除特定单元格中的公式或值即可。 可能吗? 如下所示:

I have the below formula for 1st row 3rd cell

Formula is SUM(A1:D1) and value of cell is 4;

我只想保留单元格值“ 4”作为其值,仅使用POI删除公式“(SUM(A1:D1))”。

您需要使用FormulaEvaluator.evaluateInCell(Cell) 如javadocs中所述:

如果单元格包含公式,它将评估公式,并将公式结果放回单元格中,以代替旧公式。 否则,如果单元格不包含公式,则此方法将使单元格保持不变。

您可以在Apache POI文档中找到有关评估公式的更多详细信息和示例,但是基本上,您只需要遵循此处提供的示例代码即可,例如

File file = new File("/somepath/test.xls");
Workbook wb = WorkbookFactory.create(file);
Sheet sheet = wb.getSheetAt(0);
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

// suppose your formula is in B3
CellReference cellReference = new CellReference("B3");
Row row = sheet.getRow(cellReference.getRow());
Cell cell = row.getCell(cellReference.getCol()); 

evaluator.evaluateInCell(cell);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM