1. 程式人生 > >jxl & poi

jxl & poi

步驟 val output 特點 text cell for flush wid

一、jxl 的用法:

  缺點:僅限 office2003

  特點:代碼中,沒有用到 FileInputStream

jxl
步驟

a. 創建輸出流
OutputStream os = response.getOutputStream();
response.setHeader("Content-Disposition",
"attachment; filename="+new String(fileName.getBytes("gb2312"), "iso-8859-1"));

b. 創建 WritableWorkbook (也可選擇加載模板)
WritableWorkbook wwb = Workbook.createWorkbook(os);
/** 加載模板


Workbook wb = Workbook.getWorkbook(inputFile);
WritableWorkbook wwb = Workbook.createWorkbook(os, wb)
*/

c. 創建 WritableSheet (或者選取指定已有sheet)
WritableSheet sheet = wwb.createSheet(fileName, 0);
/** 指定 sheet
WritableSheet sheet = wwb.getSheet("Sheet1");
*/

d. 創建 WritableFont
WritableFont wf = new WritableFont(WritableFont.createFont("Arial Unicode MS"), 9);

e. 創建 WritableCellFormat 用於 excel 單元格的格式
WritableCellFormat wcf = new WritableCellFormat(wf);

f. 創建 Label(列 行 從 0 開始)
Label label = new Label(column_num, row_num, value, wcf)

g. 添加 cell
sheet.addCell(label)

h. 輸出,並關閉各文件,各流
wwb.write(); // 寫入數據
wwb.close(); // 關閉文件
os.flush();
os.close(); // 關閉數據流

jxl & poi