在servlet中调用

try
{
File fileWrite = new File("D:/Write.xls");
fileWrite.createNewFile();
OutputStream os = new FileOutputStream(fileWrite);
ExcelExport.excelExport(os);
response.sendRedirect("shijian.do?method=selSy3");
}
catch(Exception e)
{
e.printStackTrace();
}

将打印的主要代码封装成一个工具类

package cn.com.shxt.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import cn.com.shxt.model.Administrator;
import cn.com.shxt.model.User;
import cn.com.shxt.service.AdministratorService;
import cn.com.shxt.service.ShiJianService;
import cn.com.shxt.service.UserService;
import cn.com.shxt.util.FileUploadTool;
import cn.com.shxt.util.Page;

public class ExcelExport extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = -5824318581633781502L;
public static void excelExport(OutputStream os){
List<Map<String, String>> list = new ShiJianService().selSy4();
try{

WritableWorkbook wwb = Workbook.createWorkbook(os);

WritableSheet ws = wwb.createSheet("第一页",0);

Label label = new Label(0,0,"课程名称");
ws.addCell(label);
label = new Label(1,0,"教师姓名");
ws.addCell(label);
label = new Label(2,0,"学生姓名");
ws.addCell(label);
label = new Label(3,0,"实验名称");
ws.addCell(label);
label = new Label(4,0,"实验目的");
ws.addCell(label);
label = new Label(5,0,"实验要求");
ws.addCell(label);
label = new Label(6,0,"实验内容");
ws.addCell(label);
label = new Label(7,0,"实验心得");
ws.addCell(label);
label = new Label(8,0,"成绩");
ws.addCell(label);
label = new Label(9,0,"实验结果");
ws.addCell(label);
for(int i=0;i<list.size();i++){
label=new Label(0,i+1,list.get(i).get("cname"));
ws.addCell(label);
label=new Label(1,i+1,list.get(i).get("aname"));
ws.addCell(label);
label=new Label(2,i+1,list.get(i).get("s_name"));
ws.addCell(label);
label=new Label(3,i+1,list.get(i).get("sname"));
ws.addCell(label);
label=new Label(4,i+1,list.get(i).get("mudi"));
ws.addCell(label);
label=new Label(5,i+1,list.get(i).get("yaoqiu"));
ws.addCell(label);
label=new Label(6,i+1,list.get(i).get("content"));
ws.addCell(label);
label=new Label(7,i+1,list.get(i).get("xinde"));
ws.addCell(label);
label=new Label(8,i+1,list.get(i).get("cj"));
ws.addCell(label);
label=new Label(9,i+1,list.get(i).get("photo"));
ws.addCell(label);
}

wwb.write();
wwb.close();

}catch(Exception e){
System.out.println("生成信息表(Excel格式)时出错:");
e.printStackTrace();
}
}

public String getFileName(){
Date date=new Date();
Long dateL=date.getTime();
String fileName=dateL.toString()+".xls";
return fileName;
}
public void doGet(HttpServletRequest request,HttpServletResponse response){
System.out.print("hahahah");
String fileName=this.getFileName();
File fileWrite=new File("D:/"+fileName);
try {
fileWrite.createNewFile();
OutputStream os=new FileOutputStream(fileWrite);
this.excelExport(os);
response.sendRedirect("/ibatisTest/pages/sucess.jsp");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
response.sendRedirect("/ibatisTest/pages/error.jsp");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public static void main(String[] args) {
try
{

File fileWrite = new File("D:/taetWrite.xls");
fileWrite.createNewFile();
OutputStream os = new FileOutputStream(fileWrite);
ExcelExport.excelExport(os);
}
catch(Exception e)
{
e.printStackTrace();
}

}

}

将页面打印成excel的更多相关文章

  1. 关于jsp页面转换成excel格式下载遇到问题及解决

    jsp页面转成excel格式的实现思路: 1.使用poi包:poi-bin-3.9-20121203 下载连接地址:http://www.apache.org/dyn/closer.cgi/poi/r ...

  2. 利用Microsoft.Office.Interop.Excel 将web页面转成PDF

    网上有很多将Web页面转成PDF的方法,还有许多收费的第三方插件.其实利用Office 自带的将EXCEL发布成PDF的功能就可以实现,如果你的需求没有多复杂,可以采用笔者的方法. 首先将web页面h ...

  3. Microsoft.Office.Interop.Excel的用法以及利用Microsoft.Office.Interop.Excel将web页面转成PDF

    1.常见用法           using Microsoft.Office.Interop.Excel; 1)新建一个Excel ApplicationClass ExcelApp = New A ...

  4. 【04】基础:将采集结果转成Excel

    前面的教程说了,数据抓取完毕之后,数据以xml格式的形式存储在本地文件夹中. 下面简单地说一下如何将保存在本地的XML文件转成Excel格式. 第一步:压缩文件 进入本地DataScraperWork ...

  5. MySQL要导出成excel的方法

    MySQL 要导出成 excel 文件很简单,执行类似这样的命令:   select * from 某个表 into outfile  'd:/文件名.xls';   上述命令你在服务器上执行,就导在 ...

  6. 第十二篇 C# 将HTML 直接转成Excel

    前些天写项目的时候,客户要求用HTML表格把信息展示出来,后面还要用展示的内容要导出Excel.本来想想在后台操作的话估计是要做死了,但是经过细想,Excel能够发布成HTML,一定也可以由HTML转 ...

  7. 如何将WORD表格转换成EXCEL表格

    WORD和EXCEL都可以制作表格,但WORD表格与EXCEL表格之间有着很明显的差距,所以在办公中经常会需要将WORD转换成EXCEL,今天小编就教大家一招将WORD表格转换成EXCEL表格. 操作 ...

  8. 怎样将PDF文件转换成Excel表格

    PDF文件怎样转换成Excel表格呢?因为很多的数据信息现在都是通过PDF文件进行传输的,所以很多时候,信息的接受者都需要将这些PDF文件所传输的数据信息转换成Excel表格来进行整理,但是我们应该怎 ...

  9. 利用itext将html页面转成pdf(不模糊)

    1.maven项目进入依赖 <dependency> <groupId>org.xhtmlrenderer</groupId> <artifactId> ...

随机推荐

  1. compare

    icompareble 默认的比较器 list.sort() compareto icompare Keyvaluepair<k,p>Where k:new()

  2. opensuse 13.1 安装配置从0开始

    主要目的为自己留作备份,仅作参考! 1. 输入法 托盘->输入法->配置 去掉除英语和Sunpinyin之外的输入法,配置Sunpinyin,使用双拼方案,重启fcitx. 另外需要禁用笔 ...

  3. ASP.NET输出JSON格式数据

    找到一个方法,虽然返回的不是json,但是数据格式是可以的 ArrayList eventList = new ArrayList(); ; i < ;i++ ) { Hashtable ht ...

  4. SpringMVC之入门

    Spring MVC简介 Spring MVC:Spring MVC也叫Spring Web MVC,属于表现层框架,是Spring中的一份子. Spring MVC执行流程图 第一个SpringMV ...

  5. 一个简单的app自动登录Python脚本案例

  6. JavaScript构造函数学习笔记

    1 理解Javascript constructor实现原理 在 JavaScript 中,每个函数都有名为“prototype”的属性,用于引用原型对象.此原型对象又有名为“constructor” ...

  7. android操作线程各种方法解析

    (一)刚开始学习android的时候我是这么写的 new Thread( new Runnable() { public void run() { myView.invalidate(); } }). ...

  8. xamarin学习之页面布局

    在android应中,需要注意3个文件,他们分别是:Main.axml,String.xml和Activity.cs. 1.布局文件Main.axml ,该文件保存在Resouses/layout的目 ...

  9. JSWindow对象

    Window 对象 Window 对象表示浏览器中打开的窗口. 如果文档包含框架(frame 或 iframe 标签),浏览器会为 HTML 文档创建一个 window 对象,并为每个框架创建一个额外 ...

  10. iOS 动画

    图层树.寄宿图以及图层几何学(一)图层的树状结构 技术交流新QQ群:414971585 巨妖有图层,洋葱也有图层,你有吗?我们都有图层 -- 史莱克 Core Animation其实是一个令人误解的命 ...