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";//測試文件名(輸出文件)
try


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


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

}

}


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