乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 怎么在<em>jsp</em>页面上点击打印,直接打印出页面上的报表-jsp excel打印,j

怎么在<em>jsp</em>页面上点击打印,直接打印出页面上的报表-jsp excel打印,j

作者:乔山办公网日期:

返回目录:excel表格制作


当点击打印按钮时 调用本地打印机打印指定好的图层信息
简单易懂的 代码 , js脚本 例子
注意: 要例子 不要理论

jsp页面的表格数据可以另存为excel表格的。
JSP从数据库导出数据到Excel下载的实现
关键代码:
<%@ page contentType="application/msexcel" %>
<%
//response.setHeader("Content-disposition","inline; filename=videos.xls");
response.setHeader("Content-disposition","attachment; filename=test.xls");
//以上这行设定传送到前端浏览器时的档名为test.xls
//就是靠7a686964616fe58685e5aeb9333这一行,让前端浏览器以为接收到一个excel档
%>
<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="GBK"%>
<%@ page contentType="application/msexcel" %>
<%
//response.setHeader("Content-disposition","inline; filename=videos.xls");
response.setHeader("Content-disposition","attachment; filename=test.xls");
//以上这行设定传送到前端浏览器时的档名为test.xls
//就是靠这一行,让前端浏览器以为接收到一个excel档
%>
<%@ page import="org.springframework.web.context.WebApplicationContext"%>
<%@ page import="com.test.*"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
UserManager um = (UserManager) ctx.getBean("userManager");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>spring jdbc test</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>

<table border="1" width="100%">
<tr> <td>id</td> <td>name</td>
</tr>
<%

List<User> users2=um.getUserList();
for(int i=0;i<users2.size();i++)
{
int t_id2=users2.get(i).getId();
String t_name2=users2.get(i).getName();
%>
<tr>
<td><%=t_id2 %></td> <td><%=t_name2 %></td>
</tr>
<%
}
%>
</table>
</body>
</html>
java中导出Excel有两个组件可以使用,一个是jxl,一个是POI,我这里用的是POI。导出是可以在服务器上生成文件,然后下载e799bee5baa6e997aee7ad94e59b9ee7ad94362,也可以利用输出流直接在网页
中弹出对话框提示用户保存或下载。生成文件的方式会导致服务器中存在着垃圾文件,实现方式不太优雅,所以这里我采用的是后面直接通过输出流的方式。
1、修改WEB服务器的CONF/web.xml,添加
<mime-mapping>
<extension>xls</extension>
<mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>

如果不添加这个,那么在网页中下载的时候就变成了JSP文件

2、download.jsp文件

<%@ page contentType="application/vnd.ms-excel" language="java" import="java.util.*,com.shangyu.action.WriteExcel" pageEncoding="GBK"%><%
response.setHeader("Content-Disposition","attachment;filename=test123.xls");//指定下载的文件名
response.setContentType("application/vnd.ms-excel");
WriteExcel we=new WriteExcel();
we.getExcel("111.xls",response.getOutputStream());
%>

注意不要有html代码,并且除了<% %> 中间的代码,其它的地方不要有空格。否则在导出文件的时候会在后台出现异常,虽然不影响程序的使用,到时令人看起来
不太舒服

3、WriteExcel.java 生成Excel的JavaBean,复杂的应用请查看API

package com.shangyu.action;
import java.io.*;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
public class WriteExcel
{

public void getExcel(String sheetName,OutputStream output)
{
HSSFWorkbook wb=new HSSFWorkbook();
HSSFSheet sheet1=wb.createSheet("sheet1");
HSSFRow row=sheet1.createRow((short)0);
HSSFCell cell=row.createCell((short)0);
cell.setCellValue(1);

row.createCell((short)1).setCellValue(2);
row.createCell((short)2).setCellValue(3);
row.createCell((short)3).setCellValue("中文字符");

row=sheet1.createRow((short)1);
cell=row.createCell((short)0);
cell.setCellValue(1);

row.createCell((short)1).setCellValue(2);
row.createCell((short)2).setCellValue(3);
row.createCell((short)3).setCellValue("中文字符");

//FileOutputStream fileout=new FileOutputStream("workbook.xls");

try {
output.flush();
wb.write(output);
output.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println( "Output is closed ");
}
}
}

通过以上三步,应该可以直接生成Excel文件下载或保存了,这在一些信息系统中相当有用。

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/shangyu79/archive/2008/07/21/2682541.aspx

报表打印不是调用JS完成的,而是通过Java操作Excel进行报表打印的,常用的一般为POI比较多。功能较为强大,包括数据的导入到处,都可以完成,需要的话扣我给你例子,你对照着改改就可以导出数据,并且已经设置好Excel的样式了

相关阅读

  • -jsp打印excel,jsp直接展现excel

  • 乔山办公网excel表格制作
  • 因为ms word和excel的文档都支持html文本格式,因此可以先用word或excel做好模版,另存为Web页,然后将该html改成jsp,将数据部分动态填入即可,不用很辛苦的调整格式 word页面只要在jsp头设
关键词不能为空

ppt怎么做_excel表格制作_office365_word文档_365办公网