采用jxl.jar生成Excel

项目开发注意事项: 1:导入从网上下载的jar包:

mail.jar 和 activation.jar

2:删掉C:\Program Files\MyEclipse\Common\plugins\com.genuitec.eclipse.j2eedt.core_10.0.0.me201110301321\data\libraryset\EE_5 下  javaee.jar中的javax-->activation.jar和mail.jar (两个Jar包)。不删除会报错,原因是是jar包版本不统一

3.导入jxl.jar包

/*

* @(#)DownloadServlet.java Time: 2013-2-28

*

* Copyright 2013 xuedou.com All rights reserved.

*/

package xuedou.skymoni.servlet;

import java.io.IOException;

import java.io.OutputStream;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import common.Logger;

import jxl.Workbook;

import jxl.format.UnderlineStyle;

import jxl.write.Label;

import jxl.write.WritableCellFormat;

import jxl.write.WritableFont;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

import jxl.write.WriteException;

import xuedou.skymoni.bean.Exam;

import xuedou.skymoni.impl.ExamImpl;

import xuedou.skymoni.service.ExamService;

/**

*

类说明

*功能描述:

* 下载Excel

* @author jinmingming jinmingming@xuedou.com

* @version 1.0, 2013-2-28

*/

public class DownloadServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

ExamService examService = new ExamImpl();

OutputStream os=null;

try

{

os = response.getOutputStream();

}

catch (IOException e1)

{

Logger.getLogger(DownloadServlet.class).error("获取response输出流出错");

}

response.reset();

response.setContentType("application/msexcel");

String fileName="市外模拟考信息数据.xls";

response.setHeader("Content-Disposition", "attachment;"+ " filename="+ new String(fileName.getBytes(), "ISO-8859-1"));

WritableWorkbook wwb=null;

WritableSheet ws=null;

try

{

wwb = Workbook.createWorkbook(os);

ws=wwb.createSheet("市外模拟考信息数据",0);

ws.getSettings().setDefaultColumnWidth(15);

//创建表头

WritableFont wfc = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);

WritableCellFormat wcfFC = new WritableCellFormat(wfc);

Label topid = new Label(0,0,"编号",wcfFC);

Label topsname = new Label(1,0,"姓名",wcfFC);

Label topschool = new Label(2,0,"学校",wcfFC);

Label toptel = new Label(3,0,"手机",wcfFC);

Label topissky = new Label(4,0,"是否在蓝天就读",wcfFC);

Label topexamschool = new Label(5,0,"考试校区",wcfFC);

Label topexamtime = new Label(6,0,"考试时段",wcfFC);

Label topexamnum = new Label(7,0,"考试场次",wcfFC);

Label topticknum = new Label(8,0,"准考证号",wcfFC);

ws.addCell(topid);

ws.addCell(topsname);

ws.addCell(topschool);

ws.addCell(toptel);

ws.addCell(topissky);

ws.addCell(topexamschool);

ws.addCell(topexamtime);

ws.addCell(topexamnum);

ws.addCell(topticknum);

Label id = null;

Label sname = null;

Label school = null;

Label tel = null;

Label issky = null;

Label examschool = null;

Label examtime = null;

Label examnum = null;

Label ticknum = null;

List list = examService.allExam(); //得到List结果集

int listsize = list.size();

for (int i = 1; i <= listsize; i++){ //遍历封装

Exam exam = list.get(i-1);

id = new Label(0,i,Integer.toString(i));

sname = new Label(1,i,exam.getSname());

school = new Label(2,i,exam.getSchool());

tel = new Label(3,i,exam.getTel());

issky = new Label(4,i,exam.getIssky());

examschool = new Label(5,i,exam.getExamschool());

examtime = new Label(6,i,exam.getExamtime());

examnum = new Label(7,i,exam.getExamnum());

ticknum = new Label(8,i,exam.getTicknum());

ws.addCell(id);

ws.addCell(sname);

ws.addCell(school);

ws.addCell(tel);

ws.addCell(issky);

ws.addCell(examschool);

ws.addCell(examtime);

ws.addCell(examnum);

ws.addCell(ticknum);

}

}

catch (Exception e)

{

Logger.getLogger(DownloadServlet.class).error("输出Excel失败");

}

finally

{

try

{

wwb.write();

wwb.close();

os.close();

}

catch (WriteException e)

{

Logger.getLogger(DownloadServlet.class).error("关闭WritableWorkbook出错");

}

catch (IOException e)

{

Logger.getLogger(DownloadServlet.class).error("关闭WritableWorkbook出错");

}

}

}

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request, response);

}

}

JSP页面:

点击下载市外模拟考信息数据

java数据生成excel_Java 数据库数据生成Excel相关推荐

  1. java 动态导入excel_java实现导入导出excel数据

    项目需要,要实现一个导入导出excel的功能,于是,任务驱动着我学习到了POI和JXL这2个java操作Excel的插件. 一.POI和JXL介绍 1.POI:是对所有office资源进行读写的一套工 ...

  2. oracle数据库html导出表格,html数据库导出excel表格数据-关于把数据库数据以表格形式导出...

    关于把数据库数据以表格形式导出 库中的数据导出到excel,以oracle为例: 最简单的方法---用工plsql dev 执行File =>newReport Window .在sql标签中写 ...

  3. Java使用存储过程检查数据库数据

    在开发和测试阶段,需要编写单元测试.集成测试自动化测试.编写自动化测试时需要用到假数据去测试特定的功能,我们可以使用sql命令把假数据存入到数据库表中.但是通过这种方式插入数据,它的数据格式.表与表关 ...

  4. oracle 加载数据戽_oracle数据库数据导入导出步骤(入门)

    oracle数据库数据导入导出步骤(入门) 说明: 1.数据库数据导入导出方法有多种,可以通过exp/imp命令导入导出,也可以用第三方工具导出,如:PLSQL 2.如果熟悉命令,建议用exp/imp ...

  5. java将数据写入excel_java将数据写入excel

    需要导入jxl.jar (在网上可以下载,然后放到lib文件夹中) 通过java操作excel表格的工具类库 支持Excel 95-2000的所有版本 生成Excel 2000标准格式 支持字体.数字 ...

  6. java 对excel操作 读取、写入、修改数据;导出数据库数据到excel

    ============前提加入jar包jxl.jar========================= // 从数据库导出数据到excel  public List<Xskh> outP ...

  7. java json导入excel_java 导入json生成excel

    这里我们使用的是maven项目,给出POM文件作为参考: org.apache.poi poi 3.15 org.apache.poi poi-ooxml 3.15 大体的思路都是调用API,新建工作 ...

  8. java 模板生成excel_java通过模板导出excel的一个实例

    写之前,大家请先下好poi的相关jar包,网上遍地都是,不多说 这个是按钮 这个是相关事件 // 导出按钮 $('#exportBtn').click(function(){ txtBeginDate ...

  9. java横向导出excel_java调用jacob生成pdf,word,excel横向

    /** 传进一个office文件的byte[]以及后缀,生成一个pdf文件的byte[]*/ public byte[] jacob_Office2Pdf(byte[] srcFileBytes, S ...

最新文章

  1. appium+python自动化33-解锁九宫格(TouchAction)
  2. Python瓦匠 —— 正则表达式(五)
  3. 运行的Cython的3种方法
  4. 什么是java构造函数_什么是java构造函数
  5. JAVA入门级教学之(内存引用的例子)
  6. 新发布 | Azure镜像市场正式上线
  7. 亚洲首个无人船试验基地建于中国珠海,无人船或将助力南海巡逻
  8. macos 管理员权限 黑苹果_Mac OS X 黑苹果系统安装驱动Kext方法
  9. 获取淘宝服务器时间,用于淘宝、天猫秒杀
  10. 递归算法应用实例------八皇后算法
  11. UML学习_1_模型
  12. LAN9252 out端口识别不到的原因排查
  13. 最全解决方法:未解压的word文档修改保存后找不到
  14. Note++ 常用功能高级用法
  15. 编程开发软件java编程入门到精通,大量教程
  16. 现在的程序员真的都找不到女朋友了??!!!
  17. pdo mysql 安装 zval_macos下编译安装php5.2.17步骤
  18. 总结常用的Javafx功能及用法(上)
  19. 移动固态硬盘,免费送!
  20. 为什么功放做得这么复杂?

热门文章

  1. 创建非矩形的Windows 窗体
  2. Java GUI 开发专题
  3. Web开发编程实用手册
  4. Error: This command has to be run with superuser privileges (under the root user on most systems).
  5. history.back(-1)和history.go(-1)的区别
  6. Python环境 及安装
  7. 简单工厂模式,工厂方法模式,抽象工厂模式,spring的狂想
  8. [数据结构与算法] 单链表的简单demo
  9. 1 用存储过程实现分页,除了上一页,下一页,第一页,和末页外还要有go按钮,以及go到那里的文本框。另外还要在Lable显示“当前x页,一共y页”。注意验证控件的使用和 链接存储过程的内容。...
  10. 10 个最佳的网站分析方法