Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29319897
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Java

2010-05-09 15:10:29

1、编写生成的类

package bj.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import jxl.*;
import jxl.read.biff.BiffException;
import jxl.write.WritableFont;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class ExcelUtils {
    /**
     * 功能:生成员工信息录入的EXCEL表格
     * 包括:所属单位 身份证号 邮箱 danwei cards emails
     * */

    public static void yuanExcels(OutputStream os) {
        jxl.write.WritableWorkbook wwb = null;
        try {
            jxl.write.Label labelC = null;
            wwb = Workbook.createWorkbook(os);
            jxl.write.WritableSheet ws = wwb.createSheet("员工信息", 0);
            labelC = new jxl.write.Label(0, 0, "员工所属单位");
            ws.addCell(labelC);
            labelC = new jxl.write.Label(1, 0, "员工身份证号");
            ws.addCell(labelC);
            labelC = new jxl.write.Label(2, 0, "员工电子邮箱");
            ws.addCell(labelC);
            wwb.write();
        } catch (Exception e) {
            System.out.println(e.toString());
        } finally {
            try {
                wwb.close();
            } catch (WriteException e) {
                // TODO Auto-generated catch block

                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block

                e.printStackTrace();
            }
        }
    }
}


第二步:编写servlets

else if (flag.equalsIgnoreCase("getexcels")) {
            //客户端下载到本地后的文件的名称

            String showFileName = "员工信息EXCEL.xls";
            //文件名称的编码

            showFileName = new String(showFileName.getBytes("ISO-8859-1"),"utf-8");
            
            //设置页面弹出下载窗口

            response.addHeader("Content-disposition","attachment; filename=\"" + showFileName + "\"");
            response.setContentType("application/unknown;charset=utf-8");
            try {
                OutputStream outstream = response.getOutputStream();
                ExcelUtils.yuanExcels(outstream);
            } catch (Exception e) {
                System.out.println(e.toString());
            }
        }

 这个方法写在doGet里面!

OK
阅读(995) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~