import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 测试POI处理公式
*
* 问题描述:通过POI导入的数据后,引用导入数据的原有公式单元格不能显示出来,需要重新定位到公式单元
* 格然后重新转入公式才行成得结果
*
* 解决办法:重新对公式单元格设置公式
*
* 相关文件:test.xls文件中手工设置单元格B2=C2+D2
*
*/
private static void readset(String fileName) {
POIFSFileSystem fs;
try {
fs = new POIFSFileSystem(new FileInputStream(fileName));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheet("Sheet1");
HSSFRow row = sheet.getRow( (short) 1);
HSSFCell cell = row.getCell( (short) 2);
cell.setCellValue( (short) 5);
cell = row.getCell( (short) 3);
cell.setCellValue( (short) 40);
HSSFCell cell1 = row.getCell( (short) 1);
if (HSSFCell.CELL_TYPE_FORMULA == cell1.getCellType()) {
//取得公式单元格的公式,重新设置
cell1.setCellFormula(cell1.getCellFormula());
}
FileOutputStream fileOut = new FileOutputStream(fileName);
wb.write(fileOut);
fileOut.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void ylb(String ym) {
System.out.println("poi");
//createFile("c:/test.xls");
createsheet("c:/gisoracle.xls");
readset("c:/test.xls");
System.out.println("==============");
}