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
-
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
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 fontWritableFont times10pt =newWritableFont(WritableFont.TIMES,10);// Define the cell formattimes =newWritableCellFormat(times10pt);// Lets automatically wrap the cellstimes.setWrap(true);// Create create a bold font with unterlinesWritableFont times10ptBoldUnderline =newWritableFont(WritableFont.TIMES,10, WritableFont.BOLD,false,UnderlineStyle.SINGLE);timesBoldUnderline =newWritableCellFormat(times10ptBoldUnderline);// Lets automatically wrap the cellstimesBoldUnderline.setWrap(true);CellView cv =newCellView();cv.setFormat(times);cv.setFormat(timesBoldUnderline);cv.setAutosize(true);// Write a few headersaddCaption(sheet,0,0,"Header 1");addCaption(sheet,1,0,"This is another header");}privatevoidcreateContent(WritableSheet sheet)throwsWriteException,RowsExceededException {// Write a few numberfor(inti =1; i <10; i++) {// First columnaddNumber(sheet,0, i, i +10);// Second columnaddNumber(sheet,1, i, i * i);}// Lets calculate the sum of itStringBuffer 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 textfor(inti =12; i <20; i++) {// First columnaddLabel(sheet,0, i,"Boring text "+ i);// Second columnaddLabel(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
-
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
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 sheetSheet sheet = w.getSheet(0);// Loop over first 10 column and linesfor(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();}}
1230

被折叠的 条评论
为什么被折叠?



