Java Excel API是一个成熟的、开源的Java API,主页地址:http://jexcelapi.sourceforge.net/,通过它开发人员可以动态地读取、写入或者修改Excel文件。利用这些APIJava开发人员,可以非常轻松地完成Excel读取、写入和修改,并且可以把改动写入到任何输出流中(如磁盘、HTTP、socket和数据库等等)。由于Java Excel API是完全由Java编写的,所以它可以运行在任何可以运行Java虚拟机的操作系统上。它主要的特性包括:
1、读取操作支持的Excel版本包括95、97、2000、XP和2003
2、公式的读取和编写(支持97及以后版本)
3、支持字体、数字和日期的格式化
4、图形复制
5、支持单元格的阴影、边框和颜色的设置
6、国际化
7、支持图片的插入和复制
……
上面的列举的只是一部分功能,更多的介绍可以参考其主页上的说明。
下面通过一个示例介绍读、写和合并单元格的基本操作:
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExcelHandler {
private OutputStream reportOS;
//表头的字体格式,字体、大小和样式
private final static WritableFont HEADER_FONT_STYLE = new WritableFont(
WritableFont.TIMES, 12, WritableFont.BOLD);
//内容的字体格式
private final static WritableCellFormat BODY_FONT_STYLE = new WritableCellFormat(
new WritableFont(WritableFont.TIMES,
WritableFont.DEFAULT_POINT_SIZE));
public ExcelHandler(String filePath) {
try {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
this.reportOS = new FileOutputStream(filePath);
} catch (Exception e) {
}
}
public void getExcelReport() {
try {
//创建WorkBook
WritableWorkbook workBook = Workbook.createWorkbook(this.reportOS);
//创建Sheet(工作表)
WritableSheet sheet = workBook.createSheet("report", 0);
// 写表头
writeReportHeader(sheet);
// 写内容
writeReportBody(sheet);
//写入内容
workBook.write();
//关闭
workBook.close();
reportOS.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void writeReportHeader(WritableSheet sheet) {
try {
//创建表头的单元格格式
WritableCellFormat headerFormat = new WritableCellFormat(
HEADER_FONT_STYLE);
//水平居中对齐
headerFormat.setAlignment(Alignment.CENTRE);
//竖直方向居中对齐
headerFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
//建立标签,参数依次为:列索引、行索引、内容、格式
Label seqLabel = new Label(0, 0, "序号", headerFormat);
//增加单元格
sheet.addCell(seqLabel);
//合并单元格,参数依次为:列索引、行索引、列索引+需要合并的列的个数、行索引+需要合并的行的个数
sheet.mergeCells(0, 0, 0, 1);
//设置单元格宽度,以字符为单位
sheet.setColumnView(0, "序号".length() + 10);
Label basicInfoLabel = new Label(1, 0, "基本信息", headerFormat);
sheet.addCell(basicInfoLabel);
sheet.mergeCells(1, 0, 3, 0);
Label nameLabel = new Label(1, 1, "姓名", headerFormat);
sheet.addCell(nameLabel);
sheet.setColumnView(0, "姓名".length() + 10);
Label ageLabel = new Label(2, 1, "年龄", headerFormat);
sheet.addCell(ageLabel);
sheet.setColumnView(0, "年龄".length() + 10);
Label heightLabel = new Label(3, 1, "身高", headerFormat);
sheet.addCell(heightLabel);
sheet.setColumnView(0, "身高".length() + 10);
} catch (Exception e) {
e.printStackTrace();
}
}
private void writeReportBody(WritableSheet sheet) {
try {
//单元格内容位数字
Number seq = new Number(0, 2, Double
.parseDouble("0"));
sheet.addCell(seq);
Label label = new Label(1, 2, "张三", BODY_FONT_STYLE);
sheet.addCell(label);
Number age = new Number(2, 2, Double
.parseDouble("18"));
sheet.addCell(age);
Number height = new Number(3, 2, Double
.parseDouble("183"));
sheet.addCell(height);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ExcelHandler handler = new ExcelHandler("c:/test.xls");
handler.getExcelReport();
}
}
import java.io.FileOutputStream;
import java.io.OutputStream;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExcelHandler {
private OutputStream reportOS;
//表头的字体格式,字体、大小和样式
private final static WritableFont HEADER_FONT_STYLE = new WritableFont(
WritableFont.TIMES, 12, WritableFont.BOLD);
//内容的字体格式
private final static WritableCellFormat BODY_FONT_STYLE = new WritableCellFormat(
new WritableFont(WritableFont.TIMES,
WritableFont.DEFAULT_POINT_SIZE));
public ExcelHandler(String filePath) {
try {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
this.reportOS = new FileOutputStream(filePath);
} catch (Exception e) {
}
}
public void getExcelReport() {
try {
//创建WorkBook
WritableWorkbook workBook = Workbook.createWorkbook(this.reportOS);
//创建Sheet(工作表)
WritableSheet sheet = workBook.createSheet("report", 0);
// 写表头
writeReportHeader(sheet);
// 写内容
writeReportBody(sheet);
//写入内容
workBook.write();
//关闭
workBook.close();
reportOS.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void writeReportHeader(WritableSheet sheet) {
try {
//创建表头的单元格格式
WritableCellFormat headerFormat = new WritableCellFormat(
HEADER_FONT_STYLE);
//水平居中对齐
headerFormat.setAlignment(Alignment.CENTRE);
//竖直方向居中对齐
headerFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
//建立标签,参数依次为:列索引、行索引、内容、格式
Label seqLabel = new Label(0, 0, "序号", headerFormat);
//增加单元格
sheet.addCell(seqLabel);
//合并单元格,参数依次为:列索引、行索引、列索引+需要合并的列的个数、行索引+需要合并的行的个数
sheet.mergeCells(0, 0, 0, 1);
//设置单元格宽度,以字符为单位
sheet.setColumnView(0, "序号".length() + 10);
Label basicInfoLabel = new Label(1, 0, "基本信息", headerFormat);
sheet.addCell(basicInfoLabel);
sheet.mergeCells(1, 0, 3, 0);
Label nameLabel = new Label(1, 1, "姓名", headerFormat);
sheet.addCell(nameLabel);
sheet.setColumnView(0, "姓名".length() + 10);
Label ageLabel = new Label(2, 1, "年龄", headerFormat);
sheet.addCell(ageLabel);
sheet.setColumnView(0, "年龄".length() + 10);
Label heightLabel = new Label(3, 1, "身高", headerFormat);
sheet.addCell(heightLabel);
sheet.setColumnView(0, "身高".length() + 10);
} catch (Exception e) {
e.printStackTrace();
}
}
private void writeReportBody(WritableSheet sheet) {
try {
//单元格内容位数字
Number seq = new Number(0, 2, Double
.parseDouble("0"));
sheet.addCell(seq);
Label label = new Label(1, 2, "张三", BODY_FONT_STYLE);
sheet.addCell(label);
Number age = new Number(2, 2, Double
.parseDouble("18"));
sheet.addCell(age);
Number height = new Number(3, 2, Double
.parseDouble("183"));
sheet.addCell(height);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ExcelHandler handler = new ExcelHandler("c:/test.xls");
handler.getExcelReport();
}
}