这是一个action,前台只要给个onclick时间,调转到这个action就可以了。
public void commprinttjfx(HttpServletRequest req, HttpServletResponse res) {
WritableWorkbook workbook;
try {
// 创建excel
String fname = "当前时间段用户平均温度";
OutputStream os2 = res.getOutputStream();//取得输出流
res.reset();//清空输出流
//下面是对中文文件名的处理
res.setCharacterEncoding("UTF-8");
fname = java.net.URLEncoder.encode(fname, "UTF-8");
res.setHeader("Content-Disposition", "attachment; filename="+ new String(fname.getBytes("UTF-8"), "GBK") + ".xls");
res.setContentType("application/msexcel");//定义输出类型
OutputStream os = res.getOutputStream();
workbook = Workbook.createWorkbook(os);
WritableSheet sheet = workbook.createSheet("用户平均温度", 0);
//加入表头
Label xuhao = new Label(0, 0, "序号");
sheet.addCell(xuhao);
Label yonghu = new Label(1, 0, "用户");
sheet.addCell(yonghu);
Label IDma = new Label(2, 0, "ID码");
sheet.addCell(IDma);
Label jssj = new Label(3, 0, "接受数据时间");
sheet.addCell(jssj);
Label swwd = new Label(4, 0, "室外温度");
sheet.addCell(swwd);
Label szwd = new Label(5, 0, "设置温度");
sheet.addCell(szwd);
Label snwd = new Label(6, 0, "室内温度");
sheet.addCell(snwd);
Label fmzt = new Label(7, 0, "阀门状态");
sheet.addCell(fmzt);
Label gswd = new Label(8, 0, "供水温度");
sheet.addCell(gswd);
Label hswd = new Label(9, 0, "回水温度");
sheet.addCell(hswd);
Label jzmj = new Label(10, 0, "建筑面积");
sheet.addCell(jzmj);
Label rfh = new Label(11, 0, "热负荷");
sheet.addCell(rfh);
Label rzb = new Label(12, 0, "热指标");
sheet.addCell(rzb);
Label ftrl = new Label(13, 0, "分摊热量");
sheet.addCell(ftrl);
Label ftrf = new Label(14, 0, "分摊热费");
sheet.addCell(ftrf);
Label bjlx = new Label(15, 0, "报警类型");
sheet.addCell(bjlx);
//加入动态值
HttpSession session = req.getSession();
List<ContentVoBean> contenList=(List)session.getAttribute("sessionVoBeantjfx");
for (int i = 1; i < 9; i++) {
ContentVoBean c = contenList.get(i+1);
Label lab0 = new Label(0, i, Integer.toString(i));
sheet.addCell(lab0);
Label lab1 = new Label(1, i, c.getUserNumber());
sheet.addCell(lab1);
Label lab2 = new Label(2, i, c.getWenID());
sheet.addCell(lab2);
Label lab3 = new Label(3, i, c.getRevieTime());
sheet.addCell(lab3);
Label lab4 = new Label(4, i, c.getRoutdoorTemperature());
sheet.addCell(lab4);
Label lab5 = new Label(5, i, c.getSetTemperature());
sheet.addCell(lab5);
Label lab6 = new Label(6, i, c.getRealityTemperature());
sheet.addCell(lab6);
Label lab7 = new Label(7, i, c.getRvalveState());
sheet.addCell(lab7);
Label lab8 = new Label(8, i, c.getRwaterTemperature());
sheet.addCell(lab8);
Label lab9 = new Label(9, i, c.getRbackwaterTemperature());
sheet.addCell(lab9);
Label lab10 = new Label(10, i, c.getJzArea());
sheet.addCell(lab10);
Label lab11 = new Label(11, i, null);
sheet.addCell(lab11);
Label lab12 = new Label(12, i, null);
sheet.addCell(lab12);
Label lab13 = new Label(13, i, c.getFtrl());
sheet.addCell(lab13);
Label lab14 = new Label(14, i, c.getFtrf());
sheet.addCell(lab14);
Label lab15 = new Label(15, i, null);
sheet.addCell(lab15);
}
// 加入图片
WritableImage wi = new WritableImage(0,10,10,20,new File("c://222.png"));
sheet.addImage(wi);
//把创建的内容写入到输出流中,并关闭输出流
workbook.write();
workbook.close();
os.close();
req.getRequestDispatcher("background/content/tjfx.jsp").forward(req, res);
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
}
}