jxltest.java
import java.io.File;
import java.io.FileOutputStream;

import jxl.Workbook;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class jxltest


{
public static void main(String [] args)


{
String templatePath = "c:\\template.xls";//模板文件名
String outFileStr = "c:\\test.xls";//測(cè)試文件名(輸出文件)
try


{
//創(chuàng)建小字體:Arial,大小為8號(hào),非粗體,非斜體
WritableFont wf = new WritableFont(WritableFont.ARIAL, 8,WritableFont.NO_BOLD, false);
//字體顏色為紅色
wf.setColour(jxl.format.Colour.RED);
//創(chuàng)建大字體:Arial,大小為18號(hào),粗體,非斜體
WritableFont Bwf = new WritableFont(WritableFont.ARIAL, 18,WritableFont.NO_BOLD, false);
Bwf.setColour(jxl.format.Colour.RED);
//創(chuàng)建單元格格式:設(shè)置水平對(duì)齊為向右對(duì)齊
jxl.write.WritableCellFormat RwcfF = new jxl.write.WritableCellFormat(wf);
RwcfF.setAlignment(jxl.write.Alignment.RIGHT);
//創(chuàng)建單元格格式:設(shè)置水平對(duì)齊為向左對(duì)齊
jxl.write.WritableCellFormat LwcfF = new jxl.write.WritableCellFormat(wf);
LwcfF.setAlignment(jxl.write.Alignment.LEFT);
//創(chuàng)建單元格格式:設(shè)置水平對(duì)齊為居中對(duì)齊
jxl.write.WritableCellFormat CwcfF = new jxl.write.WritableCellFormat(wf);
CwcfF.setAlignment(jxl.write.Alignment.CENTRE);
jxl.write.WritableCellFormat CBwcfF = new jxl.write.WritableCellFormat(Bwf);
CBwcfF.setAlignment(jxl.write.Alignment.CENTRE);
//設(shè)置垂直對(duì)齊為居中對(duì)齊
CBwcfF.setVerticalAlignment(VerticalAlignment.CENTRE);
//設(shè)置頂部邊框線為實(shí)線(默認(rèn)是黑色--也可以設(shè)置其他顏色)
CBwcfF.setBorder(jxl.format.Border.TOP, BorderLineStyle.MEDIUM);
//設(shè)置右邊框線為實(shí)線
CBwcfF.setBorder(jxl.format.Border.RIGHT, BorderLineStyle.MEDIUM);
//設(shè)置頂部框線為實(shí)線
CBwcfF.setBorder(jxl.format.Border.BOTTOM, BorderLineStyle.MEDIUM);
jxl.write.WritableCellFormat CMwcfF = new jxl.write.WritableCellFormat(wf);
CMwcfF.setAlignment(jxl.write.Alignment.LEFT);
//設(shè)置垂直對(duì)齊為向上對(duì)齊
CMwcfF.setVerticalAlignment(VerticalAlignment.TOP);
CMwcfF.setWrap(true);
File tFile = new File(templatePath);//創(chuàng)建模板文件對(duì)象
File outFile = new File(outFileStr);//創(chuàng)建輸出文件對(duì)象
//創(chuàng)建文件輸出流對(duì)象
FileOutputStream os = new FileOutputStream(outFile);
//模板工作簿對(duì)象
Workbook tBook = Workbook.getWorkbook(tFile);
//輸出工作簿對(duì)象
WritableWorkbook wbook = Workbook.createWorkbook(os, tBook);
//在坐標(biāo)為(0,0)的單元格寫入"測(cè)試"字符串使用8號(hào)紅色小字體,向右對(duì)齊
wsheet.addCell(new Label(0, 0, "test", RwcfF));
//在坐標(biāo)為(1,1)的單元格寫入"test"字符串使用8號(hào)紅色小字體,向左對(duì)齊
wsheet.addCell(new Label(1, 1, "test", LwcfF));
//在坐標(biāo)為(2,2)的單元格寫入"測(cè)試test"字符串使用8號(hào)紅色小字體,居中對(duì)齊
wsheet.addCell(new Label(2,2, "測(cè)試test", CMwcfF));
//在坐標(biāo)為(3,3)的單元格寫入2.00使用18號(hào)紅色大字體,居中對(duì)齊
wsheet.addCell(new Number(23, 14, 2.00, CBwcfF));
//寫入
wbook.write();
wbook.close();
tBook.close();
//關(guān)閉文件輸出流
os.close();
}
catch(Exception e)


{
System.out.println(e.getMessage());
e.printStackTrace();
}

}

}


Let life be beautiful like summer flowers and death like autumn leaves.