<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    如何用Java得到Excel中Formula的值

    Posted on 2006-05-23 14:47 Terry的Blog 閱讀(4772) 評論(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 不是很好,現在不知道怎么辦了
    主站蜘蛛池模板: 免费无码一区二区三区蜜桃| 中文字幕精品亚洲无线码一区| 国产精品亚洲精品久久精品| 亚洲成a人片在线播放| 黄色免费在线网站| 男人免费视频一区二区在线观看| 国产亚洲精品无码成人| 日韩免费毛片视频| 久久九九兔免费精品6| 国产成人免费ā片在线观看老同学| 亚洲国产区男人本色在线观看| 亚洲AV永久精品爱情岛论坛| 国产精品99久久免费| 亚洲 欧洲 视频 伦小说| 亚洲精品成人片在线播放| 又色又污又黄无遮挡的免费视 | 最近免费中文字幕高清大全| 一区二区在线免费视频| 精品成人一区二区三区免费视频| 久久精品国产亚洲AV蜜臀色欲| 亚洲成av人片在线看片| 亚洲熟女乱色一区二区三区 | 亚洲欧洲中文日韩久久AV乱码| 无码一区二区三区AV免费| 人禽杂交18禁网站免费| 美女网站免费福利视频| A级毛片内射免费视频| 日本免费一本天堂在线| 国产免费人成视频在线观看| 国产在线a不卡免费视频| 亚洲成人影院在线观看| 亚洲人成亚洲人成在线观看| 无码欧精品亚洲日韩一区| 久久精品国产亚洲AV无码偷窥| 亚洲中文字幕无码av在线| 亚洲欧洲日韩国产一区二区三区| 亚洲熟妇AV日韩熟妇在线| 成人免费观看男女羞羞视频| 日韩午夜理论免费TV影院| 日韩视频免费在线| 久久亚洲国产欧洲精品一|