<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>无标题文档</title> <% dim conn Set conn = Server.CreateObject("ADODB.Connection") conn.open "Provider=SQLOLEDB.1;Persist Security Info=true;User ID=sa;Password=sa;Initial Catalog=pubs;Data Source=(local);Connect Timeout=15" dim rs Set rs = Server.CreateObject("ADODB.RecordSet") rs.open "select * from authors",conn %> </head><body> <input type="button" name="out_word1" onclick="javascript:AutomateExcel() " value="导出到excel" class="notPrint"> <SCRIPT LANGUAGE="JavaScript"> <!-- function AutomateExcel() { // Start Excel and get Application object. var oXL = new ActiveXObject("Excel.Application"); // Get a new workbook. var oWB = oXL.Workbooks.Add(); var oSheet = oWB.ActiveSheet;
//Add table headers going cell by cell. <%
for i = 0 to 2 for j = 0 to 9 %> oSheet.Cells(<%=i+1%>,<%=j+1%>).value = "1"; //document.write(<%=rs.fields(j).value%>); <% next next %> oXL.Visible = true; oXL.UserControl = true; oXL.quit; } //--> </SCRIPT> </body> <% rs.close Set rs = Nothing conn.close Set conn = Nothing %> </html>
各位,现在我重述一下我问题,有如下两段代码: 1. function AutomateExcel() { // Start Excel and get Application object. var oXL = new ActiveXObject("Excel.Application"); // Get a new workbook. var oWB = oXL.Workbooks.Add(); var oSheet = oWB.ActiveSheet;
//Add table headers going cell by cell. for (i=0;i<<%=rs.RecordCount%>;i++) { <% for j = 0 to 8 %> oSheet.Cells(i+1,<%=j+1%>).Value = <%=j%>; <% next rs.movenext %> //alert(i); } oXL.Visible = true; oXL.UserControl = true; oXL.quit; }2. function AutomateExcel() { // Start Excel and get Application object. var oXL = new ActiveXObject("Excel.Application"); // Get a new workbook. var oWB = oXL.Workbooks.Add(); var oSheet = oWB.ActiveSheet;
//Add table headers going cell by cell. for (i=0;i<<%=rs.RecordCount%>;i++) { <% for j = 0 to 8 %> oSheet.Cells(i+1,<%=j+1%>).value = <%=rs(j)%>; <% next rs.movenext %> //alert(i); } oXL.Visible = true; oXL.UserControl = true; oXL.quit; }以上两段代码的区别在于,写入Excel文件的变量类型不同。执行第一段代码可正确导出Excel,而执行第二段代码时却会出现“缺少对象”的错误提示,不明白这是什么,请问有人可以帮忙吗? 谢谢了,这个问题很急的!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
<%
dim conn
Set conn = Server.CreateObject("ADODB.Connection")
conn.open "Provider=SQLOLEDB.1;Persist Security Info=true;User ID=sa;Password=sa;Initial Catalog=pubs;Data Source=(local);Connect Timeout=15"
dim rs
Set rs = Server.CreateObject("ADODB.RecordSet")
rs.open "select * from authors",conn
%>
</head><body>
<input type="button" name="out_word1" onclick="javascript:AutomateExcel() " value="导出到excel" class="notPrint">
<SCRIPT LANGUAGE="JavaScript">
<!--
function AutomateExcel()
{
// Start Excel and get Application object.
var oXL = new ActiveXObject("Excel.Application");
// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
//Add table headers going cell by cell.
<%
for i = 0 to 2
for j = 0 to 9
%>
oSheet.Cells(<%=i+1%>,<%=j+1%>).value = "1";
//document.write(<%=rs.fields(j).value%>);
<%
next
next
%>
oXL.Visible = true;
oXL.UserControl = true;
oXL.quit;
}
//-->
</SCRIPT>
</body>
<%
rs.close
Set rs = Nothing
conn.close
Set conn = Nothing
%>
</html>
1.
function AutomateExcel()
{
// Start Excel and get Application object.
var oXL = new ActiveXObject("Excel.Application");
// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
//Add table headers going cell by cell.
for (i=0;i<<%=rs.RecordCount%>;i++)
{
<%
for j = 0 to 8
%>
oSheet.Cells(i+1,<%=j+1%>).Value = <%=j%>;
<%
next
rs.movenext
%>
//alert(i);
}
oXL.Visible = true;
oXL.UserControl = true;
oXL.quit;
}2.
function AutomateExcel()
{
// Start Excel and get Application object.
var oXL = new ActiveXObject("Excel.Application");
// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
//Add table headers going cell by cell.
for (i=0;i<<%=rs.RecordCount%>;i++)
{
<%
for j = 0 to 8
%>
oSheet.Cells(i+1,<%=j+1%>).value = <%=rs(j)%>;
<%
next
rs.movenext
%>
//alert(i);
}
oXL.Visible = true;
oXL.UserControl = true;
oXL.quit;
}以上两段代码的区别在于,写入Excel文件的变量类型不同。执行第一段代码可正确导出Excel,而执行第二段代码时却会出现“缺少对象”的错误提示,不明白这是什么,请问有人可以帮忙吗?
谢谢了,这个问题很急的!
你的错误在oSheet.Cells(i+1,<%=j+1%>).value = <%=rs(j)%>;一行
因为当你是oSheet.Cells(i+1,<%=j+1%>).value = <%=j%>;的时候输出的<%=j%>是数值型
当然不会有问题
但是当你是<%=rs(j)%>的时候,从数据库中读取的可能是字符串,如果字符串没有加上引号会被认为是变量名,比如:oSheet.Cells(i+1,<%=j+1%>).value = test;
而你的test并没有在前面的程序里面定义,所以提示缺少对象!
改成oSheet.Cells(i+1,<%=j+1%>).value = "<%=rs(j)%>";试试看!