Remove Excel Formulas but Keep Values in Java

Import JAR Dependency (2 Method)

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

Sample Code

import com.spire.xls.*;

public class RemoveFormulas {
public static void main(String[] args) throws Exception {

//Create a workbook.
Workbook workbook = new Workbook();

//Load the file from disk.
workbook.loadFromFile("test.xlsx");

//Loop through worksheets.
for (Worksheet sheet : (Iterable<Worksheet>) workbook.getWorksheets())
{
//Loop through cells.
for (CellRange cell : (Iterable<CellRange>) sheet.getRange())
{
//If the cell contains formula, get the formula value, clear cell content, and then fill the formula value into the cell.
if (cell.hasFormula())
{
Object value = cell.getFormulaValue();
cell.clear(ExcelClearOptions.ClearContent);
cell.setValue(value.toString());
}
}
}

//Save to file
workbook.saveToFile("removeFormulasButKeepValues.xlsx", ExcelVersion.Version2013);
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store