本文主要向你演示如何使用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 |
importjava.io.IOException; |
005 |
importjava.util.Locale; |
009 |
importjxl.WorkbookSettings; |
010 |
importjxl.format.UnderlineStyle; |
011 |
importjxl.write.Formula; |
012 |
importjxl.write.Label; |
013 |
importjxl.write.Number; |
014 |
importjxl.write.WritableCellFormat; |
015 |
importjxl.write.WritableFont; |
016 |
importjxl.write.WritableSheet; |
017 |
importjxl.write.WritableWorkbook; |
018 |
importjxl.write.WriteException; |
019 |
importjxl.write.biff.RowsExceededException; |
021 |
publicclassWriteExcel { |
023 |
privateWritableCellFormat timesBoldUnderline; |
024 |
privateWritableCellFormat times; |
025 |
privateString inputFile; |
027 |
publicvoidsetOutputFile(String inputFile) { |
028 |
this.inputFile = inputFile; |
031 |
publicvoidwrite()throwsIOException, WriteException { |
032 |
File file =newFile(inputFile); |
033 |
WorkbookSettings wbSettings =newWorkbookSettings(); |
035 |
wbSettings.setLocale(newLocale("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 |
privatevoidcreateLabel(WritableSheet sheet) |
048 |
throwsWriteException { |
050 |
WritableFont times10pt =newWritableFont(WritableFont.TIMES,10); |
052 |
times =newWritableCellFormat(times10pt); |
057 |
WritableFont times10ptBoldUnderline =newWritableFont( |
058 |
WritableFont.TIMES,10, WritableFont.BOLD,false, |
059 |
UnderlineStyle.SINGLE); |
060 |
timesBoldUnderline =newWritableCellFormat(times10ptBoldUnderline); |
062 |
timesBoldUnderline.setWrap(true); |
064 |
CellView cv =newCellView(); |
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 |
privatevoidcreateContent(WritableSheet sheet)throwsWriteException, |
076 |
RowsExceededException { |
078 |
for(inti =1; i <10; i++) { |
080 |
addNumber(sheet,0, i, i +10); |
082 |
addNumber(sheet,1, i, i * i); |
085 |
StringBuffer buf =newStringBuffer(); |
086 |
buf.append("SUM(A2:A10)"); |
087 |
Formula f =newFormula(0,10, buf.toString()); |
089 |
buf =newStringBuffer(); |
090 |
buf.append("SUM(B2:B10)"); |
091 |
f =newFormula(1,10, buf.toString()); |
095 |
for(inti =12; i <20; i++) { |
097 |
addLabel(sheet,0, i,"Boring text "+ i); |
099 |
addLabel(sheet,1, i,"Another text"); |
103 |
privatevoidaddCaption(WritableSheet sheet,intcolumn,introw, String s) |
104 |
throwsRowsExceededException, WriteException { |
106 |
label =newLabel(column, row, s, timesBoldUnderline); |
107 |
sheet.addCell(label); |
110 |
privatevoidaddNumber(WritableSheet sheet,intcolumn,introw, |
111 |
Integer integer)throwsWriteException, RowsExceededException { |
113 |
number =newNumber(column, row, integer, times); |
114 |
sheet.addCell(number); |
117 |
privatevoidaddLabel(WritableSheet sheet,intcolumn,introw, String s) |
118 |
throwsWriteException, RowsExceededException { |
120 |
label =newLabel(column, row, s, times); |
121 |
sheet.addCell(label); |
124 |
publicstaticvoidmain(String[] args)throwsWriteException, IOException { |
125 |
WriteExcel test =newWriteExcel(); |
126 |
test.setOutputFile("c:/temp/lars.xls"); |
129 |
.println("Please check the result file under c:/temp/lars.xls "); |
读取Excel
04 |
importjava.io.IOException; |
10 |
importjxl.read.biff.BiffException; |
12 |
publicclassReadExcel { |
14 |
privateString inputFile; |
16 |
publicvoidsetInputFile(String inputFile) { |
17 |
this.inputFile = inputFile; |
20 |
publicvoidread()throwsIOException { |
21 |
File inputWorkbook =newFile(inputFile); |
24 |
w = Workbook.getWorkbook(inputWorkbook); |
26 |
Sheet sheet = w.getSheet(0); |
29 |
for(intj =0; j < sheet.getColumns(); j++) { |
30 |
for(inti =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 |
publicstaticvoidmain(String[] args)throwsIOException { |
51 |
ReadExcel test =newReadExcel(); |
52 |
test.setInputFile("c:/temp/lars.xls"); |