码迷,mamicode.com
首页 > 编程语言 > 详细

java poi读取excel公式,返回计算值(转)

时间:2019-07-30 18:51:36      阅读:419      评论:0      收藏:0      [点我收藏+]

标签:pac   val   generate   illegal   ring   xls   get   ack   class   

  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  import org.apache.poi.hssf.usermodel.HSSFSheet;
  import org.apache.poi.hssf.usermodel.HSSFRow;
  import org.apache.poi.hssf.usermodel.HSSFCell;
 
 import java.io.*;
 
 public class FormulaToString {
 
     /**
      * @param args
      */
     public void fileInput() throws IOException {
 
         HSSFWorkbook hw = new HSSFWorkbook(new FileInputStream(
                 "d:/My Documents/Desktop/poi.xls"));
         HSSFSheet hsheet = hw.getSheet("poi test");
         HSSFRow hrow = hsheet.getRow(0);
         HSSFCell hcell = hrow.getCell(0);
         String cellValue = this.getCellValue(hcell);
         System.out.println(cellValue);
 
     }
 
     public String getCellValue(HSSFCell cell) {
         String value = null;
         if (cell != null) {
             switch (cell.getCellType()) {
             case HSSFCell.CELL_TYPE_FORMULA:
                 // cell.getCellFormula();
                 try {
                     value = String.valueOf(cell.getNumericCellValue());
                 } catch (IllegalStateException e) {
                     value = String.valueOf(cell.getRichStringCellValue());
                 }
                 break;
             case HSSFCell.CELL_TYPE_NUMERIC:
                 value = String.valueOf(cell.getNumericCellValue());
                 break;
             case HSSFCell.CELL_TYPE_STRING:
                 value = String.valueOf(cell.getRichStringCellValue());
                 break;
             }
         }
 
         return value;
     }
 
     public static void main(String[] args) {
         try {
             // TODO Auto-generated method stub
             FormulaToString fts = new FormulaToString();
             fts.fileInput();
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
 
 }

 

java poi读取excel公式,返回计算值(转)

标签:pac   val   generate   illegal   ring   xls   get   ack   class   

原文地址:https://www.cnblogs.com/linxixinxiang/p/11271946.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!