北京北大青鳥校區(qū)學(xué)術(shù)部:如何用Java操作Excel文件--Java Excel API

北京北大青鳥校區(qū)學(xué)術(shù)部提供:Java Excel API是一個成熟的、開源的Java API,主頁地址:http://jexcelapi.sourceforge.net,通過它,我們可以動態(tài)地讀取、寫入或者修改Excel文件。利用這些APIJava,我們開發(fā)人員可以非常輕松地完成Excel讀取、寫入和修改,并且可以把改動寫入到任何輸出流中(如磁盤、HTTP、socket和數(shù)據(jù)庫等等)。北京北大青鳥校區(qū)專家講解,由于Java Excel API是完全由Java編寫的,所以它可以運(yùn)行在任何可以運(yùn)行Java虛擬機(jī)的操作系統(tǒng)上。它主要的特性包括:

1、讀取操作支持的Excel版本包括95、97、2000、XP和2003
2、公式的讀取和編寫(支持97及以后版本)
3、支持字體、數(shù)字和日期的格式化
4、圖形復(fù)制
5、支持單元格的陰影、邊框和顏色的設(shè)置
6、國際化
7、支持圖片的插入和復(fù)制
……
上面的列舉的只是一部分功能,在接下來的課程中,北京北大青鳥校區(qū)的老師會進(jìn)行詳細(xì)的講解。

北京北大青鳥校區(qū)講師下面將通過一個示例介紹讀、寫和合并單元格的基本操作:
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);
//內(nèi)容的字體格式
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 {
//創(chuàng)建WorkBook
WritableWorkbook workBook = Workbook.createWorkbook(this.reportOS);
//創(chuàng)建Sheet(工作表)
WritableSheet sheet = workBook.createSheet("report", 0);
// 寫表頭
writeReportHeader(sheet);
// 寫內(nèi)容
writeReportBody(sheet);

//寫入內(nèi)容
workBook.write();
//關(guān)閉
workBook.close();
reportOS.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void writeReportHeader(WritableSheet sheet) {
try {
//創(chuàng)建表頭的單元格格式
WritableCellFormat headerFormat = new WritableCellFormat(
HEADER_FONT_STYLE);
//水平居中對齊
headerFormat.setAlignment(Alignment.CENTRE);
//豎直方向居中對齊
headerFormat.setVerticalAlignment(VerticalAlignment.CENTRE);

//建立標(biāo)簽,參數(shù)依次為:列索引、行索引、內(nèi)容、格式
Label seqLabel = new Label(0, 0, "序號", headerFormat);
//增加單元格
sheet.addCell(seqLabel);
//合并單元格,參數(shù)依次為:列索引、行索引、列索引+需要合并的列的個數(shù)、行索引+需要合并的行的個數(shù)
sheet.mergeCells(0, 0, 0, 1);
//設(shè)置單元格寬度,以字符為單位
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 {
//單元格內(nèi)容位數(shù)字
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();
}
}
以上內(nèi)容由北京北大青鳥校區(qū)學(xué)術(shù)部提供。

北大青鳥網(wǎng)上報名
北大青鳥招生簡章