在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();
}
}
}