在管理信息系统(Management Information System,简称MIS)中经常会遇到将数据库内容或者是,特定的数据库内容导出为excel表的功能,毕竟excel是办公软件中最常用的表格处理软件,便于分析和增删查改,

 
下面是一个导出excel实例,可以在很多地方重用,导出的是通讯录的excel文件。附件为使用本代码所需jar包。
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
/**
     * @Function Name: export()
     * @Description:
     * @return
     * @Description:
     * @throws Exception
     * @Date Created: Apr 28, 2009 2:32:36 PM
     * @Author: gaochao
     * @Last Modified: , Date Modified:
     */
 
public String export() throws Exception {
       // 根据页面内容生成excel
       contactList = (List) getSession().getAttribute("contactlist");
       int size = contactList.size();// 得到list.size()
       OutputStream os = null;
       boolean b1 = true;
       try {
           getResponse().reset(); // 清空输出流
           os = getResponse().getOutputStream(); // 取得输出流
           String fileName = "公共通讯录.xls";// 导出文件名
           getResponse().setHeader(
                  "Content-disposition",
                  "attachment; filename="
                         new String(fileName.getBytes(), "ISO-8859-1")); // 设定输出文件头
           getResponse().setContentType("application/msexcel"); // 定义输出类型
       catch (IOException ex) {// 捕捉异常
           b1 = false;
           System.out.println("流操作错误:" + ex.getMessage());
       }
 
       // 生成一个 Exc 的工作空间
       // 创建新的Excel 工作簿
       WritableWorkbook workbook = Workbook.createWorkbook(os);
       // 生成 Exc SheetSheet名为通讯录导出
       WritableSheet sheet = workbook.createSheet("通讯录导出", 0);
       // 这里是对Cell里面的字体进行格式化
       WritableFont wfc = new WritableFont(WritableFont.ARIAL, 10,
              WritableFont.NO_BOLDfalse, UnderlineStyle.NO_UNDERLINE,
              jxl.format.Colour.BLACK);
       WritableCellFormat format = new WritableCellFormat(wfc);
       WritableCellFormat wcfFC = new jxl.write.WritableCellFormat();
       // 行居中
       wcfFC.setAlignment(Alignment.CENTRE);
       // 列居中
       wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);
       List<Label> labellist = null;
       Label labelCk = null;
 
       labelCk = new Label(0, 0, "序号", wcfFC);
       sheet.addCell(labelCk);
       labelCk = new Label(1, 0, "姓名", wcfFC);
       sheet.addCell(labelCk);
       labelCk = new Label(2, 0, "单位名称", wcfFC);
       sheet.addCell(labelCk);
       labelCk = new Label(3, 0, "职务", wcfFC);
       sheet.addCell(labelCk);
       labelCk = new Label(4, 0, "单位电话", wcfFC);
       sheet.addCell(labelCk);
       labelCk = new Label(5, 0, "手机", wcfFC);
       sheet.addCell(labelCk);
       labelCk = new Label(6, 0, "QQ", wcfFC);
       sheet.addCell(labelCk);
       labelCk = new Label(7, 0, "电子邮件", wcfFC);
       sheet.addCell(labelCk);
       labelCk = new Label(8, 0, "msn", wcfFC);
       sheet.addCell(labelCk);
       // 循环将输出内容加到sheet
       for (int i = 1; i < size + 1; i++) {
           Contact c = (Contact) contactList.get(i - 1);
           String cId = String.valueOf(c.getId());
           labelCk = new Label(0, i, cId, wcfFC);
           sheet.addCell(labelCk);
           String cName = c.getName();
           labelCk = new Label(1, i, cName, wcfFC);
           sheet.addCell(labelCk);
           String cWork = c.getWork();
           labelCk = new Label(2, i, cWork, wcfFC);
           sheet.addCell(labelCk);
           String cPost = c.getPost();
           labelCk = new Label(3, i, cPost, wcfFC);
           sheet.addCell(labelCk);
           String cTel = c.getWorkTel();
           labelCk = new Label(4, i, cTel, wcfFC);
           sheet.addCell(labelCk);
           String cMobile = c.getMobile();
           labelCk = new Label(5, i, cMobile, wcfFC);
           sheet.addCell(labelCk);
           String cQq = c.getQq();
           labelCk = new Label(6, i, cQq, wcfFC);
           sheet.addCell(labelCk);
           String cEmail = c.getEmail();
           labelCk = new Label(7, i, cEmail, wcfFC);
           sheet.addCell(labelCk);
           String cMsn = c.getMsn();
           labelCk = new Label(8, i, cMsn, wcfFC);
           sheet.addCell(labelCk);
       }
       workbook.write();
       workbook.close();
       return "refresh";
    }
这个链接是个别人写的在jsp页面上取得要导出的内容,通过<% %>嵌入java代码实现功能,可以参考着看一下,本人不建议在jsp页面中嵌入java代码的形式,除非万不得已;如果jsp页面过于臃肿,不仅可维护性差,连可读性也极差。以前看过好几年的高手编的jsp页面,长且繁杂,看代码时要来来回回把滚动条拖来拖去,不过还是佩服高手哈~