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的官方网站。