以下程式會產生一個名為result.xls的EXCEL檔案
在JSP中
<%@ page import="edu.isis.action.lmt.lmt060WriteExcel"%> <% response.reset(); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition","attachment; filename=result.xls"); //filename之後為要產生的檔案名稱 OutputStream os = response.getOutputStream(); lmt060WriteExcel.writeExcel(os); %> |
在java中
package edu.isis.action.lmt; import jxl.*; import jxl.write.*; import java.io.*; import java.util.Date; public class lmt060WriteExcel { public static void writeExcel(OutputStream os,String[][] data,int[] result,String subject) { try{ WritableWorkbook wwb = Workbook.createWorkbook(os); WritableSheet ws = wwb.createSheet("Sheet 1", 0); Label label = new Label(0, 0, "TEST!!"); ws.addCell(label); wwb.write(); wwb.close(); }catch(Exception e){ e.printStackTrace(); } } } |