asp将数据导入excel后,中文出现乱码??怎么解决
作者:Eve Cole
更新时间:2009-06-25 17:44:35
问:asp将数据导入excel后,中文出现乱码??怎么解决
<!--#include file="function/connect.asp" -->
<!--#include file="function/function.asp" -->
<%
Response.Charset = "UTF-8"
'前面包含的文件是和来链接到数据库,你要修改这个文件以链接到不同的库
sql1="select * from apply " '这里是要输出EXCEL的查询语句,如 "SESECT * FORM CAI WHERE 性别='女'"
filename="excel4.xls" ' 要输出的EXCEL文件的文件名, 你只要改以上两句就行了,其它的都不要改.
call toexcel(FILENAME,sql1)
set conn=nothing
function ReadText(FileName) '这是一个用于读出文件的函数
'response.write server.MapPath(FileName)
'response.end
set adf=server.CreateObject("Adodb.Stream")
with adf
.Type=2
.LineSeparator=10
.Open
.LoadFromFile (server.MapPath(FileName))
.Charset="GB2312"
.Position=2
ReadText=.ReadText
.Cancel()
.Close()
end with
set ads=nothing
end function
sub SaveText(FileName,Data) '这是一个用于写文件的函数
set fs= createobject("scripting.filesystemobject")
set ts=fs.createtextfile(server.MapPath(FileName),true)
ts.writeline(data)
ts.close
set ts=nothing
set fs=nothing
end sub
sub toexcel(filename,sql) '这是一个根据SQL语句和FILENAME生成EXCEL文件
Set rs=Server.CreateObject("ADODB.RecordSet")
'Response.Write sql
'response.end
rs.Open sql,objconn,1,1
TOEXCELLR="<table width='100%'><tr >"
set myfield=rs.fields
dim fieldname(50)
for i=0 to myfield.count-1
toexcellr=toexcellr&"<td class=xl24>"&MYFIELD(I).NAME&"</td>"
fieldname(i)=myfield(i).name
if myfield(i).type=135 then datename=datename&myfield(i).name&","
next
toexcellr=toexcellr&"</tr>"
do while not rs.eof
toexcellr=toexcellr&"<tr>"
for i=0 to myfield.count-1
if instr(datename,fieldname(i)&",")<>0 then
if not isnull(rs(fieldname(i))) then
TOEXCELLR=TOEXCELLR&"<td class=xl25 ><p align='left'>"&formatdatetime(rs(fieldname(i)),2)&"</p></td>"
else
TOEXCELLR=TOEXCELLR&"<td class=xl25 ><p align='left'> </p></td>"
end if
else
TOEXCELLR=TOEXCELLR&"<td class=xl24 >"&rs(fieldname(i))&"</td>"
end if
next
toexcellr=toexcellr&"</tr>"
rs.movenext
loop
rs.close
toexcellr=toexcellr&"</table>"
tou=readtext("tou.txt")
di=readtext("di.txt")
toexcellr=tou&toexcellr&di
call savetext(filename,toexcellr)
end sub
%>
<html>
<head>
<meta http-equiv="refresh" content="3;URL=<%=filename%>">
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>正在生成EXCEL文件</title>
</head>
<BODY>
正在生成EXCEL文件....
</BODY>
</HTML>
答:
EXEC master..xp_cmdshell 'bcp "SELECT * FROM 库名.dbo.apply where 性别=女" queryout "excel4.xls" -c -q -S"服务器名" -U"用户名" -P"密码"'
我用这种方法导出到excel没有出现乱码,你试下吧,sql数据库