posts - 56, comments - 54, trackbacks - 0, articles - 4
   ::  ::  :: 联系 :: 聚合  :: 管理

如何用Java得到Excel中Formula的值

Posted on 2006-05-23 14:47 Terry的Blog 阅读(4748) 评论(3)  编辑  收藏 所属分类: java语言
工具1:POI
POI是常用的读取Office文件的开源工具。但是在它正式发布的版本中没有提供得到Excel中Formula值的功能。
这个POI还在开发中的功能。相关信息 请看http://jakarta.apache.org/poi/hssf/eval.html

工具2:JExcelAPI
Java Excel API - A Java API to read, write, and modify Excel spreadsheets
http://jexcelapi.sourceforge.net/
目前的最新版本是2.6 

Some Features
* Reads data from Excel 95, 97, 2000, XP, and 2003 workbooks
* Reads and writes formulas (Excel 97 and later only)
* Generates spreadsheets in Excel 2000 format
* Supports font, number and date formatting
* Supports shading, bordering, and coloring of cells
* Modifies existing worksheets
* Is internationalized, enabling processing in almost any locale, country, language, or character encoding (formulas are currently only supported in English, French, * * Spanish, and German, but more can be added if translated)
* Supports copying of charts
* Supports insertion and copying of images into spreadsheets
* Supports logging with Jakarta Commons Logging, log4j, JDK 1.4 Logger, etc
...and much more

 
  1 import java.io.BufferedWriter;
  2 import java.io.File;
  3 import java.io.IOException;
  4 import java.io.OutputStream;
  5 import java.io.OutputStreamWriter;
  6 import java.io.UnsupportedEncodingException;
  7 
  8 import java.util.ArrayList;
  9 import java.util.Iterator;
 10 
 11 import jxl.Cell;
 12 import jxl.CellType;
 13 import jxl.FormulaCell;
 14 import jxl.Sheet;
 15 import jxl.Workbook;
 16 import jxl.biff.CellReferenceHelper;
 17 import jxl.biff.formula.FormulaException;
 18 
 19 /**
 20  * Goes through each cell in the workbook, and if the contents of that cell is a
 21  * formula, it prints out the last calculated value and the formula string
 22  */
 23 public class Formulas {
 24 
 25     public static void main(String[] args) {
 26         String file = "jxlrwtest.xls";
 27         String encoding = "UTF8";
 28         try {
 29             Workbook w = Workbook.getWorkbook(new File(file));
 30             Formulas f = new Formulas(w, System.out, encoding);
 31             w.close();
 32         } catch (Exception e) {
 33             e.printStackTrace();
 34         }
 35     }
 36 
 37     /**
 38      * Constructor
 39      * 
 40      * @param w
 41      *            The workbook to interrogate
 42      * @param out
 43      *            The output stream to which the CSV values are written
 44      * @param encoding
 45      *            The encoding used by the output stream. Null or unrecognized
 46      *            values cause the encoding to default to UTF8
 47      * @exception java.io.IOException
 48      */
 49     public Formulas(Workbook w, OutputStream out, String encoding)
 50             throws IOException {
 51         if (encoding == null || !encoding.equals("UnicodeBig")) {
 52             encoding = "UTF8";
 53         }
 54 
 55         try {
 56             OutputStreamWriter osw = new OutputStreamWriter(out, encoding);
 57             BufferedWriter bw = new BufferedWriter(osw);
 58 
 59             ArrayList parseErrors = new ArrayList();
 60 
 61             for (int sheet = 0; sheet < w.getNumberOfSheets(); sheet++) {
 62                 Sheet s = w.getSheet(sheet);
 63 
 64                 bw.write(s.getName());
 65                 bw.newLine();
 66 
 67                 Cell[] row = null;
 68                 Cell c = null;
 69 
 70                 for (int i = 0; i < s.getRows(); i++) {
 71                     row = s.getRow(i);
 72 
 73                     for (int j = 0; j < row.length; j++) {
 74                         c = row[j];
 75                         if (c.getType() == CellType.NUMBER_FORMULA
 76                                 || c.getType() == CellType.STRING_FORMULA
 77                                 || c.getType() == CellType.BOOLEAN_FORMULA
 78                                 || c.getType() == CellType.DATE_FORMULA
 79                                 || c.getType() == CellType.FORMULA_ERROR) {
 80                             FormulaCell nfc = (FormulaCell) c;
 81                             StringBuffer sb = new StringBuffer();
 82                             CellReferenceHelper.getCellReference(c.getColumn(),
 83                                     c.getRow(), sb);
 84 
 85                             try {
 86                                 bw.write("Formula in " + sb.toString()
 87                                         + " value:  " + c.getContents());
 88                                 bw.flush();
 89                                 bw.write(" formula: " + nfc.getFormula());
 90                                 bw.flush();
 91                                 bw.newLine();
 92                             } catch (FormulaException e) {
 93                                 bw.newLine();
 94                                 parseErrors
 95                                         .add(s.getName() + '!' + sb.toString()
 96                                                 + "" + e.getMessage());
 97                             }
 98                         }
 99                     }
100                 }
101             }
102             bw.flush();
103             bw.close();
104 
105             if (parseErrors.size() > 0) {
106                 System.err.println();
107                 System.err.println("There were " + parseErrors.size()
108                         + " errors");
109 
110                 Iterator i = parseErrors.iterator();
111                 while (i.hasNext()) {
112                     System.err.println(i.next());
113                 }
114             }
115         } catch (UnsupportedEncodingException e) {
116             System.err.println(e.toString());
117         }
118     }
119 }



两篇中文的介绍:
http://www.chinaitpower.com/A/2005-03-03/109739.html
http://www.myfaq.com.cn/A200507/2005-07-24/166992.html

评论

# re: 如何用Java得到Excel中Formula的值  回复  更多评论   

2006-05-24 08:47 by xplucky@gmail.com
稍微提醒一句,我以前在一个实际项目中使用JExcelAPI时,有一个数据涉及到使用公式累加,5组数据前面4组都正确了,后面一组始终不对。当时,由于项目紧,没有详究其原因,后来用poi就没有问题了。 公司其他项目使用JExcelAPI时,没有遇到相似的问题。
大家如果使用JExcelAPI并涉及到数字运算时,还是稍微留心一下。

# re: 如何用Java得到Excel中Formula的值  回复  更多评论   

2006-05-24 11:10 by Terry的回答
poi的问题是自从2005年7月就再也没推出过新版本。而老版本的功能又有所不足。JExcelAPI的最新版本2.6是2006年4月发布的。它有机会不断修正BUG.

# re: 如何用Java得到Excel中Formula的值  回复  更多评论   

2008-07-31 10:03 by shiyi
我现在也遇到用JXL读number formula 数据不正确的问题,用POI吧文件10M多很慢而且很多formula,感觉POI处理formula 不是很好,现在不知道怎么办了

只有注册用户登录后才能发表评论。


网站导航: