Package cn.fory.formula;
Import java.io.fileinputstream;
import Java . io . filenotfoundexception;
Import java.io.fileoutputstream;
Import java.io.ioexception;
Import org.apache.poi.hssf.usermodel.hssfcell;
Import org.apache.poi.hssf.usermodel.hssflow;
Import org.apache.poi.hssf.usermodel.hssfsheet;
Import org.apache.poi.hssf.usermodel.hssfworkbook;
Import org.apache.poi.poifs.filesystem.poifs filesystem;
Public class TestFormula {/*** Test POI processing formula * * Description: After importing data through POI, the original formula cell referencing the imported data cannot be displayed, and it needs to be relocated to the formula cell.
* You can only get the result by transferring the cell into the formula again * * Solution: reset the formula cell * * Related files: manually set the cell B2 = C2+D2 * */public static void main (string [] args) to throw the file, and no exception was found {
POIFSFileSystem fstry { fs = new poifsfile system(new file inputstream(" test . xls "));
hssf workbook WB = new hssf workbook(fs);
hssf sheet sheet = WB . get sheet(" sheet 1 ");
HSS row row = sheet . getrow((short) 1);
hssf cell cell = row . get cell((short)2);
cell . setcellvalue((short)5);
cell = row . get cell((short)3);
cell . setcellvalue((short)40);
hssf cell cell 1 = row . get cell((short) 1);
if (HSSFCell。 CELL _ TYPE _ FORMULA = = CELL 1 . getcelltype()){
//Get the formula of the formula cell and reset it.
cell 1 . setcellformula(cell 1 . getcellformula()); } file output stream file out = new file output stream(" test . xls ");
WB . write(file out);
file out . close();