本文主要向你演示如何使用JavaExcel API来读写Excel文件。关于JavaExcel API,这是一个开源的lib库。其相关的feature如下:
本文主要向你演示如何使用JavaExcel API来读写Excel文件。关于JavaExcel API,这是一个开源的lib库。其相关的feature如下:
支持Excel 95, 97, 2000,XP, 2003的制表页。
可以读写相关的Excel公式 (仅支持Excel 97 及以后版本)
可以生成 Excel 2000 格式的xls文件。
支持字体,数字和日期格式。
支持单元格的阴影,边框和颜色。
可以修改已存在的制表页。
国际化多语言集。(公式目前支持,英文,法文,西班牙文和德文)
支持图表拷贝。
支持图片的插入和复制。
日志生成可以使用Jakarta Commons Logging, log4j, JDK 1.4 Logger, 等。
更多……
你可以在这里下载:http://jexcelapi.sourceforge.net/,然后,把jxl.jar加到你的Java的classpath中。
下面是两段例程,一段是如何创建Excel,一段是如何读取Excel。
创建Excel
004 |
import java.io.IOException; |
005 |
import java.util.Locale; |
009 |
import jxl.WorkbookSettings; |
010 |
import jxl.format.UnderlineStyle; |
011 |
import jxl.write.Formula; |
012 |
import jxl.write.Label; |
013 |
import jxl.write.Number; |
014 |
import jxl.write.WritableCellFormat; |
015 |
import jxl.write.WritableFont; |
016 |
import jxl.write.WritableSheet; |
017 |
import jxl.write.WritableWorkbook; |
018 |
import jxl.write.WriteException; |
019 |
import jxl.write.biff.RowsExceededException; |
021 |
public class WriteExcel { |
023 |
private WritableCellFormat timesBoldUnderline; |
024 |
private WritableCellFormat times; |
025 |
private String inputFile; |
027 |
public void setOutputFile(String inputFile) { |
028 |
this .inputFile = inputFile; |
031 |
public void write() throws IOException, WriteException { |
032 |
File file = new File(inputFile); |
033 |
WorkbookSettings wbSettings = new WorkbookSettings(); |
035 |
wbSettings.setLocale( new Locale( "en" , "EN" )); |
037 |
WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings); |
038 |
workbook.createSheet( "Report" , 0 ); |
039 |
WritableSheet excelSheet = workbook.getSheet( 0 ); |
040 |
createLabel(excelSheet); |
041 |
createContent(excelSheet); |
047 |
private void createLabel(WritableSheet sheet) |
048 |
throws WriteException { |
050 |
WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10 ); |
052 |
times = new WritableCellFormat(times10pt); |
057 |
WritableFont times10ptBoldUnderline = new WritableFont( |
058 |
WritableFont.TIMES, 10 , WritableFont.BOLD, false , |
059 |
UnderlineStyle.SINGLE); |
060 |
timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline); |
062 |
timesBoldUnderline.setWrap( true ); |
064 |
CellView cv = new CellView(); |
066 |
cv.setFormat(timesBoldUnderline); |
067 |
cv.setAutosize( true ); |
070 |
addCaption(sheet, 0 , 0 , "Header 1" ); |
071 |
addCaption(sheet, 1 , 0 , "This is another header" ); |
075 |
private void createContent(WritableSheet sheet) throws WriteException, |
076 |
RowsExceededException { |
078 |
for ( int i = 1 ; i < 10 ; i++) { |
080 |
addNumber(sheet, 0 , i, i + 10 ); |
082 |
addNumber(sheet, 1 , i, i * i); |
085 |
StringBuffer buf = new StringBuffer(); |
086 |
buf.append( "SUM(A2:A10)" ); |
087 |
Formula f = new Formula( 0 , 10 , buf.toString()); |
089 |
buf = new StringBuffer(); |
090 |
buf.append( "SUM(B2:B10)" ); |
091 |
f = new Formula( 1 , 10 , buf.toString()); |
095 |
for ( int i = 12 ; i < 20 ; i++) { |
097 |
addLabel(sheet, 0 , i, "Boring text " + i); |
099 |
addLabel(sheet, 1 , i, "Another text" ); |
103 |
private void addCaption(WritableSheet sheet, int column, int row, String s) |
104 |
throws RowsExceededException, WriteException { |
106 |
label = new Label(column, row, s, timesBoldUnderline); |
107 |
sheet.addCell(label); |
110 |
private void addNumber(WritableSheet sheet, int column, int row, |
111 |
Integer integer) throws WriteException, RowsExceededException { |
113 |
number = new Number(column, row, integer, times); |
114 |
sheet.addCell(number); |
117 |
private void addLabel(WritableSheet sheet, int column, int row, String s) |
118 |
throws WriteException, RowsExceededException { |
120 |
label = new Label(column, row, s, times); |
121 |
sheet.addCell(label); |
124 |
public static void main(String[] args) throws WriteException, IOException { |
125 |
WriteExcel test = new WriteExcel(); |
126 |
test.setOutputFile( "c:/temp/lars.xls" ); |
129 |
.println( "Please check the result file under c:/temp/lars.xls " ); |
读取Excel
04 |
import java.io.IOException; |
10 |
import jxl.read.biff.BiffException; |
12 |
public class ReadExcel { |
14 |
private String inputFile; |
16 |
public void setInputFile(String inputFile) { |
17 |
this .inputFile = inputFile; |
20 |
public void read() throws IOException { |
21 |
File inputWorkbook = new File(inputFile); |
24 |
w = Workbook.getWorkbook(inputWorkbook); |
26 |
Sheet sheet = w.getSheet( 0 ); |
29 |
for ( int j = 0 ; j < sheet.getColumns(); j++) { |
30 |
for ( int i = 0 ; i < sheet.getRows(); i++) { |
31 |
Cell cell = sheet.getCell(j, i); |
32 |
CellType type = cell.getType(); |
33 |
if (cell.getType() == CellType.LABEL) { |
34 |
System.out.println( "I got a label " |
35 |
+ cell.getContents()); |
38 |
if (cell.getType() == CellType.NUMBER) { |
39 |
System.out.println( "I got a number " |
40 |
+ cell.getContents()); |
45 |
} catch (BiffException e) { |
50 |
public static void main(String[] args) throws IOException { |
51 |
ReadExcel test = new ReadExcel(); |
52 |
test.setInputFile( "c:/temp/lars.xls" ); |