| 導購 | 订阅 | 在线投稿
分享
 
 
 

asp將數據導入excel後,中文出現亂碼??怎麽解決

來源:互聯網  2008-08-07 07:35:05  評論

問: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數據庫

問: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數據庫
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
王朝網路微信公眾號
微信掃碼關註本站公眾號 wangchaonetcn
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有