给你一段代码自己改一改吧,我来不及改了:)
希望对你有用!<jsp:useBean class="demo_project.Get_Data" id="user" scope="session"></jsp:useBean>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>显示数据</title>
<script language="javascript">
var excelObj;
var $Table;
function printTable(talbleId){
var rows,cols;
var i,j;
$Table=document.all(talbleId);
rows=$Table.rows.length;
if(rows>0){
try{
cols=$Table.rows(0).children.length;
excelObj = new ActiveXObject("Excel.Application");
excelObj.Visible = true;
excelObj.WorkBooks.Add;
for(i=1;i<=rows;i++)
for(j=1;j<=cols;j++){
showInExcel(i,j,$Table.rows(i-1).children(j-1).innerText);
}
}catch(e){alert("生成Excel失败!");}
}
else{
alert("no data");}
}
function showInExcel(row,col,value){
excelObj.Cells(row, col).Value=value;
}
</script>
</head>
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import= "java.lang.*"%>
<%@ page import= "java.util.*"%>
<%
//String type
java.lang.String str;
str=user.getProjectData_T();
//Array type
java.lang.String[][] str1;
java.lang.String temp="";
str1=user.getProjectData_A();
int i;
for(i=0;i<4;i++)
{
temp=temp+"<td>"+str1[0][i]+"</td>";
}//Vector type
Vector v=new Vector();
java.lang.String temp1="";
v=user.getProjectData_V();
temp1=v.firstElement().toString();
%>
<body bgcolor="#eaeaea">
<center>
<table id="myTable" border="1" cellspacing="0" cellpadding="1">
<tr>
<th nowrap>项目名称</th>
<th nowrap>项目编号</th>
<th nowrap>项目经理</th>
<th nowrap>项目类别</th>
</tr>
<%=str%>
<tr>
<%=temp%>
</tr>
</table>
<%=temp1%>
<table border="0">
<tr>
<td>
<input type="button" value="生成EXCEL表" onclick="printTable('myTable')">
</td>
<td>直接从页面的表格里取数据生成Excel表
</td>
</tr>
<tr>
<td>
<input type="button" value="生成EXCEL表" onclick="printTable('myTable')">
</td>
<td>直接从数据库中取数据生成Excel表
</td>
</tr>
</table>
</center>
</body>
</html>
希望对你有用!<jsp:useBean class="demo_project.Get_Data" id="user" scope="session"></jsp:useBean>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>显示数据</title>
<script language="javascript">
var excelObj;
var $Table;
function printTable(talbleId){
var rows,cols;
var i,j;
$Table=document.all(talbleId);
rows=$Table.rows.length;
if(rows>0){
try{
cols=$Table.rows(0).children.length;
excelObj = new ActiveXObject("Excel.Application");
excelObj.Visible = true;
excelObj.WorkBooks.Add;
for(i=1;i<=rows;i++)
for(j=1;j<=cols;j++){
showInExcel(i,j,$Table.rows(i-1).children(j-1).innerText);
}
}catch(e){alert("生成Excel失败!");}
}
else{
alert("no data");}
}
function showInExcel(row,col,value){
excelObj.Cells(row, col).Value=value;
}
</script>
</head>
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import= "java.lang.*"%>
<%@ page import= "java.util.*"%>
<%
//String type
java.lang.String str;
str=user.getProjectData_T();
//Array type
java.lang.String[][] str1;
java.lang.String temp="";
str1=user.getProjectData_A();
int i;
for(i=0;i<4;i++)
{
temp=temp+"<td>"+str1[0][i]+"</td>";
}//Vector type
Vector v=new Vector();
java.lang.String temp1="";
v=user.getProjectData_V();
temp1=v.firstElement().toString();
%>
<body bgcolor="#eaeaea">
<center>
<table id="myTable" border="1" cellspacing="0" cellpadding="1">
<tr>
<th nowrap>项目名称</th>
<th nowrap>项目编号</th>
<th nowrap>项目经理</th>
<th nowrap>项目类别</th>
</tr>
<%=str%>
<tr>
<%=temp%>
</tr>
</table>
<%=temp1%>
<table border="0">
<tr>
<td>
<input type="button" value="生成EXCEL表" onclick="printTable('myTable')">
</td>
<td>直接从页面的表格里取数据生成Excel表
</td>
</tr>
<tr>
<td>
<input type="button" value="生成EXCEL表" onclick="printTable('myTable')">
</td>
<td>直接从数据库中取数据生成Excel表
</td>
</tr>
</table>
</center>
</body>
</html>
<%@ page import="java.text.*,java.io.*,java.util.*" %>
<jsp:useBean id="SQLBean" class="pcb.myQuery" scope="page" /><%
String tableid = request.getParameter("tableid");
int rcount = 0;
String sql = "SELECT COUNT(*) FROM table_map WHERE table_id='" + tableid + "'";
SQLBean.execQuery(sql);
if (SQLBean.mySet.next()) rcount = SQLBean.mySet.getInt(1);
sql = "SELECT fname FROM table_map WHERE table_id='" + tableid + "' ORDER BY id";
SQLBean.execQuery(sql);
%>
<html><head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
</head><body>
<style type="text/css">
{
.showTD {color: #003366; }
.hiddenTD {display:none; }
.onFocue {color: #CC66FF;}
.offFocue {color:#003366;}
}
#floater {
LEFT: 445px; POSITION: absolute; TOP: 15px; VISIBILITY: visible; WIDTH: 125px; Z-INDEX: 10}
</STYLE><object classid="clsid:0002E510-0000-0000-C000-000000000046" id="Spreadsheet1" width="777" height="500">
<param name="HTMLURL" value>
<param name="HTMLData" value="<html xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40"><head>
<style type="text/css">
<!--tr
{mso-height-source:auto;}
td
{white-space:nowrap;}
.wc07FF4F4
{white-space:nowrap;
font-family:ËÎÌå;
mso-number-format:General;
font-size:auto;
font-weight:auto;
font-style:auto;
text-decoration:auto;
mso-background-source:auto;
mso-pattern:auto;
mso-color-source:auto;
text-align:general;
vertical-align:bottom;
border-top:none;
border-left:none;
border-right:none;
border-bottom:none;
mso-protection:locked;}
-->
</style>
</head><body>
<!--[if gte mso 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:OWCVersion>9.0.0.3821</x:OWCVersion>
<x:Label Style='border-top:solid .5pt silver;border-left:solid .5pt silver;
border-right:solid .5pt silver;border-bottom:solid .5pt silver'>
<x:Caption>Microsoft Office Spreadsheet</x:Caption>
</x:Label>
<x:Name>Sheet1</x:Name>
<x:WorksheetOptions>
<x:Selected/>
<x:Height>13229</x:Height>
<x:Width>20558</x:Width>
<x:TopRowVisible>5</x:TopRowVisible>
<x:LeftColumnVisible>0</x:LeftColumnVisible>
<x:ProtectContents>False</x:ProtectContents>
<x:DefaultRowHeight>210</x:DefaultRowHeight>
<x:StandardWidth>2389</x:StandardWidth>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
<x:MaxHeight>80%</x:MaxHeight>
<x:MaxWidth>80%</x:MaxWidth>
</x:ExcelWorkbook>
</xml><![endif]--><table class=wc07FF4F4 x:str>
<col class=wc07FF4F4 width="56">
<tr height="14">
<td class=wc07FF4F4></td>
</tr>
</table></body></html>
">
<param name="DataType" value="HTMLDATA">
<param name="AutoFit" value="0">
<param name="DisplayColHeaders" value="-1">
<param name="DisplayGridlines" value="-1">
<param name="DisplayHorizontalScrollBar" value="-1">
<param name="DisplayRowHeaders" value="-1">
<param name="DisplayTitleBar" value="-1">
<param name="DisplayToolbar" value="-1">
<param name="DisplayVerticalScrollBar" value="-1">
<param name="EnableAutoCalculate" value="-1">
<param name="EnableEvents" value="-1">
<param name="MoveAfterReturn" value="-1">
<param name="MoveAfterReturnDirection" value="0">
<param name="RightToLeft" value="0">
<param name="ViewableRange" value="1:65536">
</object>
<div class="hiddenTD" id="tablers">
<table width=95% align=center border=1 cellspacing=0 cellpadding=0>
<tr><b>
<%
while (SQLBean.mySet.next()){
out.println("<td> " + SQLBean.mySet.getString(1) + "</td>");
}
%>
</b>
</tr><%
sql = "SELECT * FROM " + tableid + " ORDER BY id";
SQLBean.execQuery(sql);
while (SQLBean.mySet.next()){
out.println("<tr>");
for(int i=0;i<rcount;i++){
if(SQLBean.mySet.getString(i+2)!=null){
out.println("<td> " + String.valueOf(SQLBean.mySet.getString(i+2)) + "</td>");
}
}
out.println("</tr>");
}
SQLBean.ShutOff();
%></table>
</div>
<script language="javascript">
Spreadsheet1.HTMLData = tablers.innerHTML;
</script>
</body>
</html>
不过运行你代码中的javascript时总是显示“生成Excel失败!”。
经我测试,发现执行到“excelObj = new ActiveXObject("Excel.Application");”这条语句时发生异常导致显示“生成Excel失败!”。
希望blackcourser兄能检查一下,并能给出解决方法,小弟在这儿先谢谢你了!
运行你的代码,可以在浏览器中显示Excel界面,界面中的内容正是网页中的表格数据。但是怎么把浏览器中显示的Excel格式的表格数据转存到真正的Excel文件中去呢?在右键菜单中没有相关的命令,在浏览器中显示的Excel界面上有一个“导出到Excel”工具按钮,但按了也没用。用浏览器菜单中“文件/保存”命令也不行。
希望xzhoujun兄予以指点,小弟在这儿也先谢谢你了!