1 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 2 import org.apache.poi.hssf.usermodel.HSSFSheet; 3 import org.apache.poi.hssf.usermodel.HSSFRow; 4 import org.apache.poi.hssf.usermodel.HSSFCell; 5 6 import java.io.*; 7 8 public class FormulaToString { 9 10 /** 11 * @param args 12 */ 13 public void fileInput() throws IOException { 14 15 HSSFWorkbook hw = new HSSFWorkbook(new FileInputStream( 16 "d:/My Documents/Desktop/poi.xls")); 17 HSSFSheet hsheet = hw.getSheet("poi test"); 18 HSSFRow hrow = hsheet.getRow(0); 19 HSSFCell hcell = hrow.getCell(0); 20 String cellValue = this.getCellValue(hcell); 21 System.out.println(cellValue); 22 23 } 24 25 public String getCellValue(HSSFCell cell) { 26 String value = null; 27 if (cell != null) { 28 switch (cell.getCellType()) { 29 case HSSFCell.CELL_TYPE_FORMULA: 30 // cell.getCellFormula(); 31 try { 32 value = String.valueOf(cell.getNumericCellValue()); 33 } catch (IllegalStateException e) { 34 value = String.valueOf(cell.getRichStringCellValue()); 35 } 36 break; 37 case HSSFCell.CELL_TYPE_NUMERIC: 38 value = String.valueOf(cell.getNumericCellValue()); 39 break; 40 case HSSFCell.CELL_TYPE_STRING: 41 value = String.valueOf(cell.getRichStringCellValue()); 42 break; 43 } 44 } 45 46 return value; 47 } 48 49 public static void main(String[] args) { 50 try { 51 // TODO Auto-generated method stub 52 FormulaToString fts = new FormulaToString(); 53 fts.fileInput(); 54 } catch (IOException e) { 55 e.printStackTrace(); 56 } 57 } 58 59 }
java poi读取excel公式,返回计算值(转)
Wesley13
2021-10-11
1377 1 0
点赞
收藏
评论区
加载中...