SQL导出EXCEL 导出内容字段就要出错,其它不会(内容字段包含HTML)
SQL导出EXCEL导出内容字段就要出错,其它不会(内容字段包含HTML)内容字段类型是Content长度16代码如下:<%DIMinfoidinfoid=Trim(Re...
SQL导出EXCEL 导出内容字段就要出错,其它不会(内容字段包含HTML)内容字段类型是Content 长度16
代码如下:
<%
DIM infoid
infoid=Trim(Request("infoid"))
on error resume next'如果有错误继续执行下面的代码
Server.ScriptTimeOut=360000'防止超时
set conn = server.createobject("ADODB.Connection")
conn.open StrConn
set rs=server.createobject("adodb.recordset")
'sql="select * from PE_Commonmodel where ItemID in ("&infoid&")"'根据此SQL语句导出至Excel
sql="select PE_CommonModel.title,PE_U_Article.Author from PE_CommonModel,PE_U_Article WHERE PE_CommonModel.ItemID = PE_U_Article.ID and PE_CommonModel.Status=99 and PE_CommonModel.ItemID in ("&infoid&")"'根据此SQL语句导出至Excel
rs.Open sql,conn,3,3
for Createtablei=0 to rs.Fields.Count-1
Createtable=Createtable&rs.fields(Createtablei).name&" text ,"
next
Createtablesql="Create table Sheet1("&left(Createtable,len(Createtable)-1)&")"
ExcelFile="Excel/Excel.xls"
set fso=Server.CreateObject ("Scripting.FileSystemObject")
fpath=Server.MapPath(ExcelFile)
if fso.FileExists(fpath) then
whichfile=Server.MapPath(ExcelFile)
Set fs = CreateObject("Scripting.FileSystemObject")
Set thisfile = fs.GetFile(whichfile)
thisfile.delete true
dim excelfile,tbname
end if
Dim Driver,DBPath
Set conn = Server.CreateObject("ADODB.Connection")
Driver = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;"
DBPath = "Data Source="&Server.MapPath(ExcelFile)
conn.Open Driver & DBPath
conn.Execute(Createtablesql)
for ii=0 to rs.recordcount-1
for i=0 to rs.Fields.Count-1
Inserttablename=Inserttablename&rs.fields(i).name&","
Inserttable=Inserttable&"'"&Rs(i)&"',"
Next
Insertintosql="Insert into Sheet1("&left(Inserttablename,len(Inserttablename)-1)&")values("&left(Inserttable,len(Inserttable)-1)&")"
'显示错误信息开始
if err.number<>0 then
response.write "<br><br><br><br><br><br><br>"
response.write " <div align='center'><font color='red'>"&err.description&"</font><a href='javascript:history.go(-1)'>退回上一步!</a></div>"
response.write "<br><br><br><br><br><br>"
response.end
end if
'显示错误信息结束
conn.Execute(Insertintosql)
'显示错误信息开始
if err.number<>0 then
response.write "<br><br><br><br><br><br><br>"
response.write " <div align='center'><font color='red'>"&err.description&"</font><a href='javascript:history.go(-1)'>退回上一步!</a></div>"
response.write "<br><br><br><br><br><br>"
response.end
end if
'显示错误信息结束
Insertintosql =""
Inserttable=""
Inserttablename=""
rs.MoveNext
Next
'显示错误信息开始
if err.number<>0 then
response.write "<br><br><br><br><br><br><br>"
response.write " <div align='center'><font color='red'>"&err.description&"</font><a href='javascript:history.go(-1)'>退回上一步!</a></div>"
response.write "<br><br><br><br><br><br>"
response.end
end if
'显示错误信息结束
Response.Redirect (ExcelFile)
%>
</body>
</html>
还有,导出至EXCEL 的列名是默认的表字段名,名字我能不能自己能修改.谢谢! 展开
代码如下:
<%
DIM infoid
infoid=Trim(Request("infoid"))
on error resume next'如果有错误继续执行下面的代码
Server.ScriptTimeOut=360000'防止超时
set conn = server.createobject("ADODB.Connection")
conn.open StrConn
set rs=server.createobject("adodb.recordset")
'sql="select * from PE_Commonmodel where ItemID in ("&infoid&")"'根据此SQL语句导出至Excel
sql="select PE_CommonModel.title,PE_U_Article.Author from PE_CommonModel,PE_U_Article WHERE PE_CommonModel.ItemID = PE_U_Article.ID and PE_CommonModel.Status=99 and PE_CommonModel.ItemID in ("&infoid&")"'根据此SQL语句导出至Excel
rs.Open sql,conn,3,3
for Createtablei=0 to rs.Fields.Count-1
Createtable=Createtable&rs.fields(Createtablei).name&" text ,"
next
Createtablesql="Create table Sheet1("&left(Createtable,len(Createtable)-1)&")"
ExcelFile="Excel/Excel.xls"
set fso=Server.CreateObject ("Scripting.FileSystemObject")
fpath=Server.MapPath(ExcelFile)
if fso.FileExists(fpath) then
whichfile=Server.MapPath(ExcelFile)
Set fs = CreateObject("Scripting.FileSystemObject")
Set thisfile = fs.GetFile(whichfile)
thisfile.delete true
dim excelfile,tbname
end if
Dim Driver,DBPath
Set conn = Server.CreateObject("ADODB.Connection")
Driver = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;"
DBPath = "Data Source="&Server.MapPath(ExcelFile)
conn.Open Driver & DBPath
conn.Execute(Createtablesql)
for ii=0 to rs.recordcount-1
for i=0 to rs.Fields.Count-1
Inserttablename=Inserttablename&rs.fields(i).name&","
Inserttable=Inserttable&"'"&Rs(i)&"',"
Next
Insertintosql="Insert into Sheet1("&left(Inserttablename,len(Inserttablename)-1)&")values("&left(Inserttable,len(Inserttable)-1)&")"
'显示错误信息开始
if err.number<>0 then
response.write "<br><br><br><br><br><br><br>"
response.write " <div align='center'><font color='red'>"&err.description&"</font><a href='javascript:history.go(-1)'>退回上一步!</a></div>"
response.write "<br><br><br><br><br><br>"
response.end
end if
'显示错误信息结束
conn.Execute(Insertintosql)
'显示错误信息开始
if err.number<>0 then
response.write "<br><br><br><br><br><br><br>"
response.write " <div align='center'><font color='red'>"&err.description&"</font><a href='javascript:history.go(-1)'>退回上一步!</a></div>"
response.write "<br><br><br><br><br><br>"
response.end
end if
'显示错误信息结束
Insertintosql =""
Inserttable=""
Inserttablename=""
rs.MoveNext
Next
'显示错误信息开始
if err.number<>0 then
response.write "<br><br><br><br><br><br><br>"
response.write " <div align='center'><font color='red'>"&err.description&"</font><a href='javascript:history.go(-1)'>退回上一步!</a></div>"
response.write "<br><br><br><br><br><br>"
response.end
end if
'显示错误信息结束
Response.Redirect (ExcelFile)
%>
</body>
</html>
还有,导出至EXCEL 的列名是默认的表字段名,名字我能不能自己能修改.谢谢! 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询