工具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
两篇中文的介绍:
http://www.chinaitpower.com/A/2005-03-03/109739.html
http://www.myfaq.com.cn/A200507/2005-07-24/166992.html
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 }
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