Java讀寫Excel
本文主要向你演示如何使用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
?packagewriter;?
???
?importjava.io.File;?
?importjava.io.IOException;?
?importjava.util.Locale;?
???
?importjxl.CellView;?
?importjxl.Workbook;?
?importjxl.WorkbookSettings;?
?importjxl.format.UnderlineStyle;?
?importjxl.write.Formula;?
?importjxl.write.Label;?
?importjxl.write.Number;?
?importjxl.write.WritableCellFormat;?
?importjxl.write.WritableFont;?
?importjxl.write.WritableSheet;?
?importjxl.write.WritableWorkbook;?
?importjxl.write.WriteException;?
?importjxl.write.biff.RowsExceededException;?
???
?publicclassWriteExcel {?
???
???? privateWritableCellFormat timesBoldUnderline;?
???? privateWritableCellFormat times;?
???? privateString inputFile;?
???
?publicvoidsetOutputFile(String inputFile) {?
???? this.inputFile = inputFile;?
???? }?
???
???? publicvoidwrite() throwsIOException, WriteException {?
???????? File file = newFile(inputFile);?
???????? WorkbookSettings wbSettings = newWorkbookSettings();?
???
???????? wbSettings.setLocale(newLocale("en", "EN"));?
???
???????? WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);?
???????? workbook.createSheet("Report", 0);?
???????? WritableSheet excelSheet = workbook.getSheet(0);?
???????? createLabel(excelSheet);?
???????? createContent(excelSheet);?
???
???????? workbook.write();?
???????? workbook.close();?
???? }?
???
???? privatevoidcreateLabel(WritableSheet sheet)?
???????????? throwsWriteException {?
???????? // Lets create a times font?
???????? WritableFont times10pt = newWritableFont(WritableFont.TIMES, 10);?
???????? // Define the cell format?
???????? times = newWritableCellFormat(times10pt);?
???????? // Lets automatically wrap the cells?
???????? times.setWrap(true);?
???
???????? // Create create a bold font with unterlines?
???????? WritableFont times10ptBoldUnderline = newWritableFont(?
???????????????? WritableFont.TIMES, 10, WritableFont.BOLD, false,?
???????????????? UnderlineStyle.SINGLE);?
???????? timesBoldUnderline = newWritableCellFormat(times10ptBoldUnderline);?
???????? // Lets automatically wrap the cells?
???????? timesBoldUnderline.setWrap(true);?
???
???????? CellView cv = newCellView();?
???????? cv.setFormat(times);?
???????? cv.setFormat(timesBoldUnderline);?
???????? cv.setAutosize(true);?
???
???????? // Write a few headers?
???????? addCaption(sheet, 0, 0, "Header 1");?
???????? addCaption(sheet, 1, 0, "This is another header");?
???
???? }?
???
???? privatevoidcreateContent(WritableSheet sheet) throwsWriteException,?
???????????? RowsExceededException {?
???????? // Write a few number?
???????? for(inti = 1; i < 10; i++) {?
???????????? // First column?
???????????? addNumber(sheet, 0, i, i + 10);?
???????????? // Second column?
???????????? addNumber(sheet, 1, i, i * i);?
???????? }?
???????? // Lets calculate the sum of it?
???????? StringBuffer buf = newStringBuffer();?
???????? buf.append("SUM(A2:A10)");?
???????? Formula f = newFormula(0, 10, buf.toString());?
???????? sheet.addCell(f);?
???????? buf = newStringBuffer();?
???????? buf.append("SUM(B2:B10)");?
???????? f = newFormula(1, 10, buf.toString());?
???????? sheet.addCell(f);?
???
???????? // Now a bit of text?
???????? for(inti = 12; i < 20; i++) {?
???????????? // First column?
???????????? addLabel(sheet, 0, i, "Boring text "+ i);?
???????????? // Second column?
???????????? addLabel(sheet, 1, i, "Another text");?
???????? }?
???? }?
???
???? privatevoidaddCaption(WritableSheet sheet, intcolumn, introw, String s)?
???????????? throwsRowsExceededException, WriteException {?
???????? Label label;?
???????? label = newLabel(column, row, s, timesBoldUnderline);?
???????? sheet.addCell(label);?
???? }?
???
???? privatevoidaddNumber(WritableSheet sheet, intcolumn, introw,?
???????????? Integer integer) throwsWriteException, RowsExceededException {?
???????? Number number;?
???????? number = newNumber(column, row, integer, times);?
???????? sheet.addCell(number);?
???? }?
???
???? privatevoidaddLabel(WritableSheet sheet, intcolumn, introw, String s)?
???????????? throwsWriteException, RowsExceededException {?
???????? Label label;?
???????? label = newLabel(column, row, s, times);?
???????? sheet.addCell(label);?
???? }?
???
???? publicstaticvoidmain(String[] args) throwsWriteException, IOException {?
???????? WriteExcel test = newWriteExcel();?
???????? test.setOutputFile("c:/temp/lars.xls");?
???????? test.write();?
???????? System.out?
???????????????? .println("Please check the result file under c:/temp/lars.xls ");?
???? }?
?}
讀取Excel
?packagereader;?
???
?importjava.io.File;?
?importjava.io.IOException;?
???
?importjxl.Cell;?
?importjxl.CellType;?
?importjxl.Sheet;?
?importjxl.Workbook;?
?importjxl.read.biff.BiffException;?
???
?publicclassReadExcel {?
???
???? privateString inputFile;?
???
???? publicvoidsetInputFile(String inputFile) {?
???????? this.inputFile = inputFile;?
???? }?
???
???? publicvoidread() throwsIOException? {?
???????? File inputWorkbook = newFile(inputFile);?
???????? Workbook w;?
???????? try{?
???????????? w = Workbook.getWorkbook(inputWorkbook);?
???????????? // Get the first sheet?
???????????? Sheet sheet = w.getSheet(0);?
???????????? // Loop over first 10 column and lines?
???
???????????? for(intj = 0; j < sheet.getColumns(); j++) {?
???????????????? for(inti = 0; i < sheet.getRows(); i++) {?
???????????????????? Cell cell = sheet.getCell(j, i);?
???????????????????? CellType type = cell.getType();?
???????????????????? if(cell.getType() == CellType.LABEL) {?
???????????????????????? System.out.println("I got a label "
???????????????????????????????? + cell.getContents());?
???????????????????? }?
???
???????????????????? if(cell.getType() == CellType.NUMBER) {?
???????????????????????? System.out.println("I got a number "
???????????????????????????????? + cell.getContents());?
???????????????????? }?
???
???????????????? }?
???????????? }?
???????? } catch(BiffException e) {?
???????????? e.printStackTrace();?
???????? }?
???? }?
???? publicstaticvoidmain(String[] args) throwsIOException {?
???????? ReadExcel test = newReadExcel();?
???????? test.setInputFile("c:/temp/lars.xls");?
???????? test.read();?
???? }?
?}
posted on 2009-12-19 23:46 飛熊 閱讀(876) 評論(0) 編輯 收藏 所屬分類: java