| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2963 人关注过本帖
标题:ASP生成excel问题请教,谢谢!
只看楼主 加入收藏
cuipingtiao
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-8-8
收藏
 问题点数:0 回复次数:19 
ASP生成excel问题请教,谢谢!

在ASP中生成excel的时候,碰到了一些问题:代码如下:
<%
'--生成excel文件
dim rs,sql,filename,fs,myfile,x
Set fs = server.CreateObject("scripting.filesystemobject")
filename = Server.MapPath("11.xls")
if fs.FileExists(filename) then
fs.DeleteFile(filename)
end if
set myfile = fs.CreateTextFile(filename,true)
'--生成excel文件

On Error Resume Next

strAddr = Server.MapPath(".")

set objExcelApp = CreateObject("Excel.Application")
objExcelApp.DisplayAlerts = false
objExcelApp.Application.Visible = true
objExcelApp.WorkBooks.Open(strAddr & "\11.xls")
set objExcelBook = objExcelApp.ActiveWorkBook
set objExcelSheets = objExcelBook.Worksheets
set objExcelSheet = objExcelBook.Sheets(1)

objExcelSheet.Range("B2:k2").Value = Array("Week1", "Week2", "Week3", "Week4", "Week5", "Week6", "Week7", "Week8", "Week9", "Week10")
objExcelSheet.Range("B3:k3").Value = Array("67", "87", "5", "9", "7", "45", "45", "54", "54", "10")
objExcelSheet.Range("B4:k4").Value = Array("10", "10", "8", "27", "33", "37", "50", "54", "10", "10")
objExcelSheet.Range("B5:k5").Value = Array("23", "3", "86", "64", "60", "18", "5", "1", "36", "80")
objExcelSheet.Cells(3,1).Value="Internet Explorer"
objExcelSheet.Cells(4,1).Value="Netscape"
objExcelSheet.Cells(5,1).Value="Other"

objExcelApp.Quit
set objExcelApp = Nothing

%>


这些代码能生成一个空的excel文件,但是往文件中写数据的时候就失败,找了很多原因,找不到,请各位大仙帮忙看看,谢谢了.

搜索更多相关主题的帖子: excel ASP 
2007-08-09 08:08
zhy589
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2007-7-2
收藏
得分:0 
我现在也在写这个程序可是调试出错啊你下载我的原程序看看吧!也在这一页
2007-08-09 10:50
cuipingtiao
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-8-8
收藏
得分:0 

在哪里的,找不到啊?

2007-08-09 11:10
zsf2006
Rank: 1
来 自:博客园
等 级:新手上路
威 望:1
帖 子:315
专家分:0
注 册:2006-6-3
收藏
得分:0 

我给个给你,你参考一下:
<%
'==========下面是asp生成excel表格
dim filename,fs,myfile,x
dim j,shijian
Server.ScriptTimeOut=9999
Set fs = server.CreateObject("scripting.filesystemobject")
'--假设你想让生成的EXCEL文件做如下的存放
filename =Server.MapPath("tmpexcel.xls")
'--如果原来的EXCEL文件存在的话删除它
if fs.FileExists(filename) then
fs.DeleteFile(filename)
end if
'--创建EXCEL文件
set myfile = fs.CreateTextFile(filename,true)

Set rs = Server.CreateObject("ADODB.Recordset")
'--从数据库中把你想放到EXCEL中的数据查出来

sql="select name,sex,age,email,company,per,address,code,phone,defen from per"
rs.Open sql,conn,1,1
'response.end
if rs.EOF and rs.BOF then

else
dim strLine,responsestr
strLine="姓名" & chr(9)&"性别" & chr(9)&"年龄" & chr(9)&"电子邮件" & chr(9)&"公司" & chr(9)&"个人主页" & chr(9)&"地址" & chr(9)&"邮编" & chr(9)&"电话"& chr(9)&"得分"

'For each x in rs.fields
'strLine= strLine & x.name & chr(9)
'Next

'--将表的列名先写入EXCEL
myfile.writeline strLine

Do while Not rs.EOF
strLine=""
for each x in rs.Fields
strLine= strLine & x.value & chr(9)
next
'--将表的数据写入EXCEL
myfile.writeline strLine
rs.MoveNext
loop
end if

rs.Close
set rs = nothing
conn.close
set conn = nothing
set myfile = nothing
Set fs=Nothing
'=============结束
%>


光临我的博客:http://
2007-08-09 11:40
cuipingtiao
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-8-8
收藏
得分:0 

以上的代码是可以生成excel文件,但是好像不能处理,把单员格给合并,而采用excel.appliction的话是可以把但愿格给合并的.

2007-08-09 13:51
zhy589
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2007-7-2
收藏
得分:0 
回复:(cuipingtiao)在哪里的,找不到啊?
在第2页了,标题是
[求助]用ASP编写一程序可以导入EXCEL中的数据到Access数据库中并且在页面中显示记录调试出错了
2007-08-09 14:21
zhy589
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2007-7-2
收藏
得分:0 
回复:(zsf2006)我给个给你,你参考一下:

谢谢你啊我也参考了你的代码可是运行出现了错误,错误代码如下:

错误类型:
Microsoft VBScript 编译器错误 (0x800A03EA)
语法错误
/1xs/daochu.asp, line 33, column 66
strLine="姓名" & chr(9)&"号码" & chr(9)&"机型" & chr(9)&"维修时间" & chr(9)&


告诉我怎么回事啊?

[此贴子已经被作者于2007-8-10 14:23:28编辑过]

2007-08-10 14:21
cuipingtiao
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-8-8
收藏
得分:0 

这句代码应该是没有问题的,我在我自己的机子上可以运行.

2007-08-14 08:22
sidivefen
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-8-14
收藏
得分:0 

我给你一个一段代码,你试试看.但置一下浏览器.

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<title>WEB页面导出为EXCEL文档的方法</title>

</head>

<body>

<table id="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0">

<tr>

<td colspan="5" align="center">WEB页面导出为EXCEL文档的方法</td>

</tr>

<tr>

<td>列标题1</td>

<td>列标题2</td>

<td>列标题3</td>

<td>列标题4</td>

<td>列标题5</td>

</tr>

<tr>

<td>aaa</td>

<td>bbb</td>

<td>ccc</td>

<td>ddd</td>

<td>eee</td>

</tr>

<tr>

<td>AAA</td>

<td>BBB</td>

<td>CCC</td>

<td>DDD</td>

<td>EEE</td>

</tr>

<tr>

<td>FFF</td>

<td>GGG</td>

<td>HHH</td>

<td>III</td>

<td>JJJ</td>

</tr>
</table>
<input type="button" onclick="javascript:method1('tableExcel');" value="第一种方法导入到EXCEL">

<input type="button" onclick="javascript:method2('tableExcel');" value="第二种方法导入到EXCEL">

<input type="button" onclick="javascript:getXlsFromTbl('tableExcel',null);" value="第三种方法导入到EXCEL">

<SCRIPT LANGUAGE="javascript">

function method1(tableid) {//整个表格拷贝到EXCEL中

var curTbl = document.getElementById(tableid);

var oXL = new ActiveXObject("Excel.Application");

//创建AX对象excel

var oWB = oXL.Workbooks.Add();

//获取workbook对象

var oSheet = oWB.ActiveSheet;

//激活当前sheet

var sel = document.body.createTextRange();

sel.moveToElementText(curTbl);

//把表格中的内容移到TextRange中

sel.select();

//全选TextRange中内容

sel.execCommand("Copy");

//复制TextRange中内容

oSheet.Paste();

//粘贴到活动的EXCEL中

oXL.Visible = true;

//设置excel可见属性

}

function method2(tableid) //读取表格中每个单元到EXCEL中

{

var curTbl = document.getElementById(tableid);

var oXL = new ActiveXObject("Excel.Application");

//创建AX对象excel

var oWB = oXL.Workbooks.Add();

//获取workbook对象

var oSheet = oWB.ActiveSheet;

//激活当前sheet

var Lenr = curTbl.rows.length;

//取得表格行数

for (i = 0; i < Lenr; i++)

{

var Lenc = curTbl.rows(i).cells.length;

//取得每行的列数

for (j = 0; j < Lenc; j++)

{

oSheet.Cells(i + 1, j + 1).value = curTbl.rows(i).cells(j).innerText;

//赋值

}

}

oXL.Visible = true;

//设置excel可见属性

}

function getXlsFromTbl(inTblId, inWindow) {

try {

var allStr = "";

var curStr = "";

//alert("getXlsFromTbl");

if (inTblId != null && inTblId != "" && inTblId != "null") {

curStr = getTblData(inTblId, inWindow);

}

if (curStr != null) {

allStr += curStr;

}

else {

alert("你要导出的表不存在!");

return;

}

var fileName = getExcelFileName();

doFileExport(fileName, allStr);

}

catch(e) {

alert("导出发生异常:" + e.name + "->" + e.description + "!");

}

}

function getTblData(inTbl, inWindow) {

var rows = 0;

//alert("getTblData is " + inWindow);

var tblDocument = document;

if (!!inWindow && inWindow != "") {

if (!document.all(inWindow)) {

return null;

}

else {

tblDocument = eval(inWindow).document;

}

}

var curTbl = tblDocument.getElementById(inTbl);

var outStr = "";

if (curTbl != null) {

for (var j = 0; j < curTbl.rows.length; j++) {

//alert("j is " + j);

for (var i = 0; i < curTbl.rows[j].cells.length; i++) {

//alert("i is " + i);

if (i == 0 && rows > 0) {

outStr += " \t";

rows -= 1;

}

outStr += curTbl.rows[j].cells[i].innerText + "\t";

if (curTbl.rows[j].cells[i].colSpan > 1) {

for (var k = 0; k < curTbl.rows[j].cells[i].colSpan - 1; k++) {

outStr += " \t";

}

}

if (i == 0) {

if (rows == 0 && curTbl.rows[j].cells[i].rowSpan > 1) {

rows = curTbl.rows[j].cells[i].rowSpan - 1;

}

}

}

outStr += "\r\n";

}

}

else {

outStr = null;

alert(inTbl + "不存在!");

}

return outStr;

}

function getExcelFileName() {

var d = new Date();

var curYear = d.getYear();

var curMonth = "" + (d.getMonth() + 1);

var curDate = "" + d.getDate();

var curHour = "" + d.getHours();

var curMinute = "" + d.getMinutes();

var curSecond = "" + d.getSeconds();

if (curMonth.length == 1) {

curMonth = "0" + curMonth;

}

if (curDate.length == 1) {

curDate = "0" + curDate;

}

if (curHour.length == 1) {

curHour = "0" + curHour;

}

if (curMinute.length == 1) {

curMinute = "0" + curMinute;

}

if (curSecond.length == 1) {

curSecond = "0" + curSecond;

}

var fileName = "leo_zhang" + "_" + curYear + curMonth + curDate + "_"

+ curHour + curMinute + curSecond + ".csv";

//alert(fileName);

return fileName;

}

function doFileExport(inName, inStr) {

var xlsWin = null;

if (!!document.all("glbHideFrm")) {

xlsWin = glbHideFrm;

}

else {

var width = 6;

var height = 4;

var openPara = "left=" + (window.screen.width / 2 - width / 2)

+ ",top=" + (window.screen.height / 2 - height / 2)

+ ",scrollbars=no,width=" + width + ",height=" + height;

xlsWin = window.open("", "_blank", openPara);

}

xlsWin.document.write(inStr);

xlsWin.document.close();

xlsWin.document.execCommand('Saveas', true, inName);

xlsWin.close();

}

</SCRIPT>

</body>

</html>

2007-08-14 10:08
sidivefen
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-8-14
收藏
得分:0 
回复:(cuipingtiao)ASP生成excel问题请教,谢谢!

我给你一个一段代码,你试试看.但置一下浏览器.

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<title>WEB页面导出为EXCEL文档的方法</title>

</head>

<body>

<table id="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0">

<tr>

<td colspan="5" align="center">WEB页面导出为EXCEL文档的方法</td>

</tr>

<tr>

<td>列标题1</td>

<td>列标题2</td>

<td>列标题3</td>

<td>列标题4</td>

<td>列标题5</td>

</tr>

<tr>

<td>aaa</td>

<td>bbb</td>

<td>ccc</td>

<td>ddd</td>

<td>eee</td>

</tr>

<tr>

<td>AAA</td>

<td>BBB</td>

<td>CCC</td>

<td>DDD</td>

<td>EEE</td>

</tr>

<tr>

<td>FFF</td>

<td>GGG</td>

<td>HHH</td>

<td>III</td>

<td>JJJ</td>

</tr>
</table>
<input type="button" onclick="javascript:method1('tableExcel');" value="第一种方法导入到EXCEL">

<input type="button" onclick="javascript:method2('tableExcel');" value="第二种方法导入到EXCEL">

<input type="button" onclick="javascript:getXlsFromTbl('tableExcel',null);" value="第三种方法导入到EXCEL">

<SCRIPT LANGUAGE="javascript">

function method1(tableid) {//整个表格拷贝到EXCEL中

var curTbl = document.getElementById(tableid);

var oXL = new ActiveXObject("Excel.Application");

//创建AX对象excel

var oWB = oXL.Workbooks.Add();

//获取workbook对象

var oSheet = oWB.ActiveSheet;

//激活当前sheet

var sel = document.body.createTextRange();

sel.moveToElementText(curTbl);

//把表格中的内容移到TextRange中

sel.select();

//全选TextRange中内容

sel.execCommand("Copy");

//复制TextRange中内容

oSheet.Paste();

//粘贴到活动的EXCEL中

oXL.Visible = true;

//设置excel可见属性

}

function method2(tableid) //读取表格中每个单元到EXCEL中

{

var curTbl = document.getElementById(tableid);

var oXL = new ActiveXObject("Excel.Application");

//创建AX对象excel

var oWB = oXL.Workbooks.Add();

//获取workbook对象

var oSheet = oWB.ActiveSheet;

//激活当前sheet

var Lenr = curTbl.rows.length;

//取得表格行数

for (i = 0; i < Lenr; i++)

{

var Lenc = curTbl.rows(i).cells.length;

//取得每行的列数

for (j = 0; j < Lenc; j++)

{

oSheet.Cells(i + 1, j + 1).value = curTbl.rows(i).cells(j).innerText;

//赋值

}

}

oXL.Visible = true;

//设置excel可见属性

}

function getXlsFromTbl(inTblId, inWindow) {

try {

var allStr = "";

var curStr = "";

//alert("getXlsFromTbl");

if (inTblId != null && inTblId != "" && inTblId != "null") {

curStr = getTblData(inTblId, inWindow);

}

if (curStr != null) {

allStr += curStr;

}

else {

alert("你要导出的表不存在!");

return;

}

var fileName = getExcelFileName();

doFileExport(fileName, allStr);

}

catch(e) {

alert("导出发生异常:" + e.name + "->" + e.description + "!");

}

}

function getTblData(inTbl, inWindow) {

var rows = 0;

//alert("getTblData is " + inWindow);

var tblDocument = document;

if (!!inWindow && inWindow != "") {

if (!document.all(inWindow)) {

return null;

}

else {

tblDocument = eval(inWindow).document;

}

}

var curTbl = tblDocument.getElementById(inTbl);

var outStr = "";

if (curTbl != null) {

for (var j = 0; j < curTbl.rows.length; j++) {

//alert("j is " + j);

for (var i = 0; i < curTbl.rows[j].cells.length; i++) {

//alert("i is " + i);

if (i == 0 && rows > 0) {

outStr += " \t";

rows -= 1;

}

outStr += curTbl.rows[j].cells[i].innerText + "\t";

if (curTbl.rows[j].cells[i].colSpan > 1) {

for (var k = 0; k < curTbl.rows[j].cells[i].colSpan - 1; k++) {

outStr += " \t";

}

}

if (i == 0) {

if (rows == 0 && curTbl.rows[j].cells[i].rowSpan > 1) {

rows = curTbl.rows[j].cells[i].rowSpan - 1;

}

}

}

outStr += "\r\n";

}

}

else {

outStr = null;

alert(inTbl + "不存在!");

}

return outStr;

}

function getExcelFileName() {

var d = new Date();

var curYear = d.getYear();

var curMonth = "" + (d.getMonth() + 1);

var curDate = "" + d.getDate();

var curHour = "" + d.getHours();

var curMinute = "" + d.getMinutes();

var curSecond = "" + d.getSeconds();

if (curMonth.length == 1) {

curMonth = "0" + curMonth;

}

if (curDate.length == 1) {

curDate = "0" + curDate;

}

if (curHour.length == 1) {

curHour = "0" + curHour;

}

if (curMinute.length == 1) {

curMinute = "0" + curMinute;

}

if (curSecond.length == 1) {

curSecond = "0" + curSecond;

}

var fileName = "leo_zhang" + "_" + curYear + curMonth + curDate + "_"

+ curHour + curMinute + curSecond + ".csv";

//alert(fileName);

return fileName;

}

function doFileExport(inName, inStr) {

var xlsWin = null;

if (!!document.all("glbHideFrm")) {

xlsWin = glbHideFrm;

}

else {

var width = 6;

var height = 4;

var openPara = "left=" + (window.screen.width / 2 - width / 2)

+ ",top=" + (window.screen.height / 2 - height / 2)

+ ",scrollbars=no,width=" + width + ",height=" + height;

xlsWin = window.open("", "_blank", openPara);

}

xlsWin.document.write(inStr);

xlsWin.document.close();

xlsWin.document.execCommand('Saveas', true, inName);

xlsWin.close();

}

</SCRIPT>

</body>

</html>

2007-08-14 10:09
快速回复:ASP生成excel问题请教,谢谢!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.055531 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved