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

          如何用Java得到Excel中Formula的值

          Posted on 2006-05-23 14:47 Terry的Blog 閱讀(4774) 評論(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 不是很好,現在不知道怎么辦了
          主站蜘蛛池模板: 云梦县| 荥经县| 水城县| 西和县| 灵寿县| 容城县| 成武县| 沂南县| 清徐县| 桃园市| 页游| 武隆县| 新邵县| 苏尼特左旗| 三原县| 河池市| 肃南| 汕头市| 五家渠市| 郸城县| 龙陵县| 临沭县| 琼海市| 乌恰县| 东港市| 沁阳市| 邵阳市| 威信县| 黄陵县| 宣威市| 久治县| 米林县| 宜川县| 乐山市| 钦州市| 金乡县| 邹城市| 兴安盟| 隆子县| 咸宁市| 涿州市|