After asp imports data into excel, Chinese characters appear garbled? ? how to solve
Author:Eve Cole
Update Time:2009-06-25 17:44:35
Question: After asp imports data into excel, does the Chinese appear garbled? ? how to solve
<!--#include file="function/connect.asp" -->
<!--#include file="function/function.asp" -->
<%
Response.Charset = "UTF-8"
'The file included earlier is and to link to the database, you need to modify this file to link to a different library
sql1="select * from apply " 'Here is the query statement to output EXCEL, such as "SESECT * FORM CAI WHERE gender='female'"
filename="excel4.xls" 'The file name of the EXCEL file to be output, you only need to change the above two sentences, do not change anything else.
call toexcel(FILENAME,sql1)
set conn=nothing
function ReadText(FileName) 'This is a function for reading files
'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) 'This is a function for writing files
set fs= createobject("scripting.filesystemobject")
set ts=fs.createtextfile(server.MapPath(FileName),true)
ts.writeline(data)
ts.close
setts=nothing
set fs=nothing
end sub
sub toexcel(filename,sql) 'This is an EXCEL file generated based on SQL statements and FILENAME
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>Generating EXCEL file</title>
</head>
<BODY>
Generating EXCEL file....
</BODY>
</HTML>
answer:
EXEC master..xp_cmdshell 'bcp "SELECT * FROM library name.dbo.apply where gender=female" queryout "excel4.xls" -c -q -S"server name" -U"username" -P"password"'
I used this method to export to excel without garbled characters. You can try it, sql database