首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

jsp页面table导出替excel

2011-12-21 
jsp页面table导出为exceljsp页面如下:........htmlheadtitlebean:message keyzlhs_tggygz.jsp.ti

jsp页面table导出为excel
jsp页面如下:
........
<html>
<head>
<title><bean:message key="zlhs_tggygz.jsp.title"/></title>
</head>
<div align="right"><a href="yfcMain.do?u_code=<bean:write name="UserBean" property="u_code"/>" title=<bean:message key="zl_xmxxck.jsp.return"/>>
<img src="pic/icon_home.gif" border=0 ></a></div>
<body>
<a href="zlhs_tggygz_add.jsp"><bean:message key="common.xjxx"/></a> 
  <hr class=hrOne>  
  <hbjzl:zlhs_searchtag tableName="ZlHsTggygz" action="zlhs_tggygz.jsp" />
<hbjzl:pageTag setting_linage="<%=MaxPage %>" id="aaa" name="tggygzlist"
url="zlhs_tggygz.jsp" arrange="" manner="select">  
<table id="PrintA" class=tableCommonStyle width=100% cellpadding=3 cellspacing=1 border=0>
  <tr>
  <td width="5%" scope="col" class=forumColorOne align="center"><bean:message key="zlhs_tgssccgz.jsp.xh"/></td>  
  <td width="6%" scope="col" class=forumColorOne align="center"><bean:message key="zlhs_wsclc.jsp.ssqx"/></td>
  <td width="12%" scope="col" class=forumColorOne align="center"><bean:message key="common.qymc"/></td>
  <td width="12%" scope="col" class=forumColorOne align="center"><bean:message key="common.gcmc"/></td>
  <td width="15%" scope="col" class=forumColorOne align="center"><bean:message key="zlhs_tggygz.jsp.gylb"/></td>
  <td width="11%" scope="col" class=forumColorOne align="center"><bean:message key="zlhs_tggygz.jsp.gylr"/></td>
  <td width="6%" scope="col" class=forumColorOne align="center"><bean:message key="zlhs_tggygz.jsp.gycl"/></td>
  <td width="7%" scope="col" class=forumColorOne align="center"><bean:message key="zlhs_tggygz.jsp.ygypfl"/></td>
  <td width="8%" scope="col" class=forumColorOne align="center"><bean:message key="zlhs_tggygz.jsp.gzhgypfl"/></td>
  <td width="6%" scope="col" class=forumColorOne align="center"><bean:message key="zlhs_tgssccgz.jsp.jpl"/></td>
  <td width="5%" scope="col" class=forumColorOne align="center"><bean:message key="common.ht"/></td>  
  <td width="8%" scope="col" class=forumColorOne align="center"><bean:message key="zlhs.jsp.operate"/></td>  
  </tr>
  <logic:empty name="tggygzlist">
<% request.getSession().removeAttribute("tggygzlist"); %>
</logic:empty>
<logic:present name="tggygzlist" scope="session">
<logic:iterate id="ppp" name="aaa">
  <tr>
  <td class=forumColorTwo align="center"><bean:write name="ppp" property="qydm" /></td>  
  <td class=forumColorTwo align="center"><bean:write name="ppp" property="ssbm" /></td>
  <td class=forumColorTwo align="center"><bean:write name="ppp" property="qymc" /></td>


  <td class=forumColorTwo align="center"><bean:write name="ppp" property="gcmc" /></td>
  <td class=forumColorTwo align="center"><bean:write name="ppp" property="gylb" /></td>
  <td class=forumColorTwo align="center"><bean:write name="ppp" property="gynr" /></td>
  <td class=forumColorTwo align="right"><bean:write name="ppp" property="gycl" /></td>
  <td class=forumColorTwo align="right"><bean:write name="ppp" property="ygypfl" /></td>
  <td class=forumColorTwo align="right"><bean:write name="ppp" property="gzhpfl" /></td>
  <td class=forumColorTwo align="right"><bean:write name="ppp" property="jpl" /></td>
  <td class=forumColorTwo align="center"><bean:write name="ppp" property="sfzhtz" /></td>
  <td class=forumColorTwo align="center"><a href="tggygz_u.do?gcbh=<bean:write name="ppp" property="gcbh" />"><bean:message key="zlhs.jsp.update"/></a>|<a href="tggygz_del.do?gcbh=<bean:write name="ppp" property="gcbh" />" onclick="return confirm('<bean:message key="common.del"/>')"><bean:message key="zlhs.jsp.delete"/></a></td>
  </tr>  
</logic:iterate>
</logic:present>
<%
try{
tggygzlist=(List)request.getSession().getAttribute("tggygzlist");
for(i=0;i<tggygzlist.size();i++)
{
hj1+=((ZlHsTggygz)tggygzlist.get(i)).getJpl();
}
}catch(Exception e){
hj1=0;
}
String hj2=String.valueOf(hj1);
if(hj2.indexOf(".")>0){

if(hj2.substring(String.valueOf(hj1).indexOf("."),hj2.length()).length()>3){
hj2=hj2.substring(0,String.valueOf(hj1).indexOf(".")+3);
}
hj1=Double.valueOf(hj2);
}
%>
<tr>
<td class=forumColorTwo align="center"><bean:message key="common.hj"/></td>
<td class=forumColorTwo align="center">-</td>  
<td class=forumColorTwo align="center">-</td> 
<td class=forumColorTwo align="center">-</td> 
<td class=forumColorTwo align="center">-</td> 
<td class=forumColorTwo align="center">-</td> 
<td class=forumColorTwo align="center">-</td> 
<td class=forumColorTwo align="center">-</td> 
<td class=forumColorTwo align="center">-</td> 
<td class=forumColorTwo align="right"><%=hj1 %></td> 
<td class=forumColorTwo align="center">-</td> 
<td class=forumColorTwo align="center">-</td> 
</tr>
</table>
</hbjzl:pageTag>
<br>
<input type="button" value="导出为Excel"></body>
</html>
现在想将此页面上的table导出为excel。但是表格的最后一列操作选择不要导出。该如何进行?
上网查了半天,有人说用jakarta Poi ,有人说用jxl。还有人说用开源displaytag。
但是我对这些都不了解,还是不知道该怎么做。请大家多多赐教!
本来想多给点分,不过,系统提示“您无权发这么多可用分”。呜呜呜呜




[解决办法]
建议直接上网找下 可以通过js来控制导出的
[解决办法]


删除body前的字符,在开始处加上
<%
response.setContentType("application/msexcel"); 
response.setHeader("Content-disposition",
"attachment; filename=excelname.xls");
%>
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=GBK">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 11">
</head>
[解决办法]
楼上的思路可以使用,简单的说,把html另存为 .xls 就可以用excel直接打开的!

如果要更多的控制,需要用jxl / poi 写程序输出!
[解决办法]
有这样的js组件,我用过!
[解决办法]
jxl.jar
[解决办法]
ssh架构 jasperreport 很成熟了啊
[解决办法]
你找找 薛笛(csdn专家) jasperReport资料很丰富了 , 导出excel是为了打印不?

。。。。。。。有点激动。。。
[解决办法]

button onclick事件调用下面js 方法
然后你要导出的table定义个id=viewtable.这样就只导出这个table的内容。操作按钮在table外,不会导出来的。

<script language=javascript> 
function printToExcel() {
window.clipboardData.setData( "Text ",document.all( 'viewtable ').outerHTML); 
try 

var ExApp = new ActiveXObject( "Excel.Application ") 
var ExWBk = ExApp.workbooks.add() 
var ExWSh = ExWBk.worksheets(1) 
ExApp.DisplayAlerts = false 
ExApp.visible = true 
}
catch(e) 

alert( "您的电脑没有安装Microsoft Excel软件! ") 
return false 
}
ExWBk.worksheets(1).Paste;

</script>
[解决办法]
如果你要导出的内容列表跟目前的不一样,你就重新写一个jsp,按下导出按钮后,把查询结果传递到那个jsp,在那个jsp前面加上这段<%
response.reset();
response.setContentType("application/vnd.ms-excel;charset=GBK");
response.setHeader("Content-Disposition","attachment; filename=queryresult.xls");
%>
然后在那个jsp列表里你去掉操作列。
[解决办法]


[解决办法]
我看了上面的好多留言 好多人 已经告诉你怎么导EXCEL 了
用Button 只是增加个 onClick 事件, 你所说的要最后一列可显示 
那就要用JS 去控制了, 当你导EXCEL 之前 在最后一列可以点列头 用JS控制 让它显示与否了

说的够清楚了 .
[解决办法]
我刚好也做了个导出为EXCLE,用到了POI,很方便的,通过一个按钮,把你的table里的内容发到servlet,在打包成EXCLE让用户下载,油箱是zwj6612@yahoo.com.cn,现在刚好没有这个工程,你要发你看看
[解决办法]
//导出excel
function outputExcel(tableObject, offColInt) {
try {
var excelObjectApplication = new ActiveXObject("Excel.Application");
}
catch (e) {
alert("\u6d4f\u89c8\u5668\u5f53\u524d\u8bbe\u7f6e\u4e0d\u5141\u8bb8\u6b64\u64cd\u4f5c\uff01");
}
excelObjectApplication.visible = true;
excelObjectApplication.DisplayAlerts = false;
var xlBook = excelObjectApplication.Workbooks.Add;
var excelObjectActiveSheet = xlBook.Worksheets(1);
var rowLength = tableObject.rows.length;
var colLength = tableObject.rows[0].cells.length;


colLength -= parseInt(offColInt);
for (i = 0; i < rowLength - 1; i++) {
for (j = 0; j < colLength; j++) {
excelObjectActiveSheet.Cells(i + 1, j + 1).value = "'" + tableObject.rows[i].cells[j].innerText;
}
}
}

<input type="button" name="output" value="导出本页"
onClick="outputExcel(document.getElementById('caseListTable'), 0)"
class="button" style="cursor:pointer">


<table align="center" border="1" cellspacing="0" cellpadding="0"
style="word-break:break-all" id="caseListTable"bordercolordark="#CCCCCC" bordercolorlight="#FFFFFF" width="900"
background="./././newimages/bg_7.jpg">

这是我用的JS导出EXCEL的JS代码,要删除第一行和左右一行,最后一行没试过,想想应该没问题,只要把上面的循环里面的值改一改就可以了,以上是有用代码,先把JS文件写入,写一个button事件,再把你要导出的table命名
当然这要设置浏览器安全问题
把对没有标记为安全的ActiveX 控件进行初始化和脚本改为提示
一般用这个可能要求客户改浏览器权限,这是弊端,FIREFOX不能用,所以,如果想导出可能经过后台导比较好,如果数据量大的话,经后台比较慢吧,有个JXL包可以导入,导出用流应该就可以了.
[解决办法]
4楼的思路可以使用

在当前页 导出EXCEL 处设为超链接,跳到下一页,下一页的开头如下:
<% 
response.setContentType("application/msexcel");
response.setHeader("Content-disposition","attachment;
filename=excelname.xls"); 
%> 

然后再在这一页用HTML代码画<table></table>就可以了,导出的excel布局和<table>中的布局一样。

[解决办法]
在当前页 导出EXCEL 处设为超链接,跳到下一页,下一页的开头如下: 
<%
response.setContentType("application/msexcel");
response.setHeader("Content-disposition","attachment; 
filename=excelname.xls");
%>
up 4L

热点排行