Java讀寫Excel簡介
作者:曹祺
Email:Qi.Cao@Sun.Com
Blog: http://blogs.sun.com/greysh
本文難度:入門
本文鏈接: http://developers.sun.com.cn/blog/functionalca/entry/java%E8%AF%BB%E5%86%99excel%E7%AE%80%E4%BB%8B
源代碼下載: http://developers.sun.com.cn/blog/functionalca/resource/Greysh/FCA_Greysh_Excel.zip
JXL官方網站: http://www.andykhan.com/jexcelapi/
HSSF官方網站: http://poi.apache.org/spreadsheet/
簡介
Helloworld
簡介
Excel雖然最常用的電子表格軟件之一,但是由于微軟并未對Excel開源,因此給Java開發中帶來一些不便,除了用JNI去調用本地方法,另外一種方法就是用Java框架去解析Excel。開發中常用的框架為JXL,是Andy Khan開發的,詳情可以在他的個人主頁看到http://www.andykhan.com/,另外一個便是Apache和微軟聯合開發的,是屬于APACHE POI(http://poi.apache.org/spreadsheet/)項目的子項目。就筆者感覺,Andy Khan開發的比較小巧,適合小項目,而且調用比apache poi的hssf要方便。但是如果要使用很多高級功能的話,還是建議使用Apache的POI.本教程為入門教程,因此主要介紹調用的流程,如果想了解如何設置單元格樣式,可以訪問其官方網站。
Helloworld
完整代碼如下,開發環境:JXL為JExcelApi v2.6.9 .POI為poi-3.5-beta5-20090219.jar
由于本文主要介紹如何講指定的數據讀取和寫入到指定的單元格,因此定義了個接口,包含讀取和寫入兩個方法。接口如下
package com.greysh.excel.service;
public interface ExcelService {
public void write(int colum, int row, String content) throws Exception;
public String read(int colum, int row)throws Exception;
}
然后用JXL讀取的話,類ExcelUtils來自的包為com.greysh.excel.jxl
package com.greysh.excel.jxl;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import com.greysh.excel.service.ExcelService;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExcelUtils implements ExcelService{
private String path = "D:\\Excel.xls";
public static void main(String[] args) throws Exception {
ExcelUtils excelUtils = new ExcelUtils();
String content = "Hello World";
excelUtils.write(2, 3, content);
String newContent = excelUtils.read(2, 3);
System.out.println(newContent);
}
@Override
public void write(int colum, int row, String content) throws Exception {
FileOutputStream fileOutputStream = new FileOutputStream(path);
WritableWorkbook writableWorkbook = Workbook
.createWorkbook(fileOutputStream);
WritableSheet writableSheet = writableWorkbook.createSheet("Excel", 0);
Label label = new Label(colum, row, content);
writableSheet.addCell(label);
writableWorkbook.write();
writableWorkbook.close();
fileOutputStream.close();
}
@Override
public String read(int colum, int row) throws Exception {
FileInputStream fileInputStream = new FileInputStream(path);
Workbook workbook = Workbook.getWorkbook(fileInputStream);
Sheet sheet = workbook.getSheet(0);
Cell cell = sheet.getCell(colum, row);
String content = cell.getContents();
workbook.close();
fileInputStream.close();
return content;
}
}
如果用apache的poi讀取,為com.greysh.excel.poi的ExcelUtils類
package com.greysh.excel.poi;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import com.greysh.excel.service.ExcelService;
public class ExcelUtils implements ExcelService {
private String path = "D:\\ExcelPOI.xls";
public static void main(String[] args) throws Exception {
ExcelUtils excelUtils = new ExcelUtils();
String content = "Hello Worlds";
excelUtils.write(2, 3, content);
String newContent = excelUtils.read(2, 3);
System.out.println(newContent);
}
@Override
public void write(int colum, int row, String content) throws Exception {
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("Excel");
Row rows = sheet.createRow(row);
Cell cell = rows.createCell(colum);
cell.setCellValue("Hello World");
FileOutputStream fileOut = new FileOutputStream(path);
workbook.write(fileOut);
fileOut.close();
}
@Override
public String read(int colum, int row) throws Exception {
Workbook workbook = new HSSFWorkbook(new POIFSFileSystem(
new FileInputStream(path)));
Sheet sheet = workbook.getSheetAt(0);
Row rows = sheet.getRow(row);
Cell cell = rows.getCell(colum);
String content = cell.getStringCellValue();
return content;
}
}
此段代碼修改自Apache的官方demo,對高級樣式進行了精簡,如欲了解高級功能,例如單元格樣式,可以訪問http://poi.apache.org/spreadsheet/quick-guide.html
對比以上兩種方式,我們可以總結出調用流程如下
1打開工作文件Workbook,在此之前先用java的io流創建或者讀取文件
2.打開工作表Sheet
3.讀行,然后讀列。不過讀者請注意,行和列是從零開始的
4.取得數據進行操作
本代碼運行后會在控制臺打印出Hello World,同時D盤會生成Excel文件
如果想了解如何設置單元格的顯示樣式或者讓單元格的內容的數據格式不只是String,可以訪問JXL和POI的官方網站。