数据库导出xml、htm、csv、sql

来源:互联网 发布:海文网络计划视频教程 编辑:程序博客网 时间:2024/04/29 17:44


<%
'数据库导出记录代码
'作者海娃,haiwa#blueidea.com,http://www.51windows.Net
'用法:
'ExportDB.asp?sql=select语句&tablename=表名(可选)&filetype=导出格式(xml,htm,csv,sql)&pid=自动编号字段名(仅当导出sql类型时有用)
dim tablename,filetype,fieldPid
sql???= request("sql")
tablename?= request("tablename")
filetype?= lcase(request("filetype"))
fieldPid?= request("pid")
if fieldPid = "" then
?fieldPid = "id"
end if
fieldPid = lcase(fieldPid)
if lcase(left(sql,6))<>"select" then
?Response.write "sql语句必须为select * from [table] where ......."
?Response.end
end if

if tablename = "" then
?tablename = "数据导出结果"
end if

function HTMLEncode(fString)
?if not isnull(fString) then
??fString = Server.HTMLEncode(fString)
??fString = Replace(fString, CHR(10) & CHR(10), "

")
??fString = Replace(fString, CHR(10), "
")
??fString = Replace(fString, CHR(9), "  ")

??HTMLEncode = fString
?end if
end function

function Myreplace(str)
?if not isnull(str) then
??fString = Replace(fString,"""", """""")
??Myreplace = str
?else
??Myreplace = ""
?end if
end function

function Myreplace2(str)
?if not isnull(str) then
??fString = Replace(fString,"'", "''")
??Myreplace2 = str
?else
??Myreplace2 = ""
?end if
end function

dim def_export_sep,def_export_val
def_export_sep = ","
def_export_val = """"

Set rs = Conn.Execute(sql)
'导出XML文件
if filetype="xml" then
?Response.contenttype="text/xml"
?Response.Charset = "gb2312"
?Response.AddHeader "Content-Disposition", "attachment;filename="&tablename&".xml"
?Response.write "" & vbnewline
?Response.write ""
?strLine=""
?dim thefield(50)
?i = 0
?For? each? x? in? rs.fields
??thefield(i)=x.name
??i=i+1
?Next
?While rs.EOF =false
?strLine= vbnewline&chr(9)&""
?k=0
?For? each? x? in? rs.fields
??strLine=? strLine & vbnewline&chr(9)&chr(9)&"<"&thefield(k)&">"
??if instr(x.value,"<")>0 or instr(x.value,">")>0 or instr(x.value,"&")>0 or len(x.value)>255 then
???strLine=? strLine &""
??else
???strLine=? strLine & x.value
??end if
??strLine=? strLine &""
??k=k+1
?Next
?rs.MoveNext
?Response.write strLine &vbnewline& chr(9)&"
"
?Wend
Response.write vbnewline&"
"

'导出sql文件
elseif filetype="sql" then
?Response.contenttype="text/sql"
?Response.AddHeader "Content-Disposition", "attachment;filename="&tablename&".sql"
?strLine=""
?dim sql_insert
?For? each? x? in? rs.fields
??if lcase(x.name)<>fieldPid then?'如果是自动编号
???strLine=? strLine? & def_export_val &? x.name & def_export_val & def_export_sep
??end if
?Next
?strLine = replace(left(strLine,len(strLine)-1),"""","")
?strLine = "insert into ["&tablename&"] (" & strLine & ") values "
?sql_insert = strLine
?'Response.write strLine & vbnewline
?'response.end
?While rs.EOF =false
?strLine= ""
?def_export_val = "'"
?For? each? x? in? rs.fields
??if lcase(x.name)<>fieldPid then
???'2004-8-11更新 Null值时无法导出的bug。
???x_value = x.value
???if isnull(x_value) or len(x_value) = 0 then
????x_value = ""
???else
????x_value = replace(x_value,"'","''")
???end if
???strLine=? strLine & def_export_val & x_value & def_export_val & def_export_sep
??end if
?Next
?rs.MoveNext
?strLine = left(strLine,len(strLine)-1)
?Response.write sql_insert & "("& strLine &")*" & vbnewline
?Wend
elseif filetype="csv" then
?Response.contenttype="text/csv"
?Response.AddHeader "Content-Disposition", "attachment;filename="&tablename&".csv"
?strLine=""
?For? each? x? in? rs.fields
??strLine=? strLine? & def_export_val &? x.name & def_export_val & def_export_sep
?Next
?Response.write strLine & vbnewline
?While rs.EOF =false
??strLine= ""
??For? each? x? in? rs.fields
???strLine=? strLine & def_export_val & Myreplace(x.value) & def_export_val & def_export_sep
??Next
??rs.MoveNext
??Response.write strLine & vbnewline
?Wend
else
?if filetype="htm" then'弹出下载html的对话框
??Response.contenttype="application/ms-download"
??Response.AddHeader "Content-Disposition", "attachment;filename="&tablename&".htm"
?end if
%>





导出数据 - <A href="http://www.51windows.Net</title">www.51windows.Net</title</A>><BR><BR><!--<BR>body,input,select?????? { font-family: Tahoma; font-size: 8pt }<BR>th???????? { font-family: Tahoma; font-size: 8pt;padding:3px;color:#FFFFFF;background-color:#999999;}<BR>td???????? { font-family: Tahoma; font-size: 8pt;padding:3px;background-color:#EFEFEF;}<BR>--><BR><BR></head><BR><BODY style="overflow:auto;" topmargin=2 bgcolor=buttonface></P><P><form method="post" name=myform><BR>SQL:<input type="text" name="sql" value="<% = sql%>"><BR>表名:<input type="text" name="tablename" value="<% = tablename %>" size="8"><BR>导出格式:<select size="1" name="filetype"><BR><option value="">请选择</option><BR><option <%if filetype = "htm" then Response.write "selected "%>value="htm">htm</option><BR><option <%if filetype = "xml" then Response.write "selected "%>value="xml">xml</option><BR><option <%if filetype = "csv" then Response.write "selected "%>value="csv">csv</option><BR><option <%if filetype = "sql" then Response.write "selected "%>value="sql">sql</option><BR></select><BR>自动编号字段名:<input type="text" name="pid" value="<% = fieldPid%>" size="8"><input type="submit" value="确定"><BR></form></P><P><div align="center"><BR><BR>?<BR><%<BR>i=0<BR>?For? each? x? in? rs.fields? <BR>??strLine=? strLine? &chr(9)&chr(9)&"<th align=""center"">"&? x.name? &"</th>"& vbnewline<BR>?Next<BR>?Response.write strLine&chr(9)&""& vbnewline & vbnewline<BR>?While rs.EOF =false<BR>??i=i+1<BR>??Response.write chr(9)&""& vbnewline<BR>??strLine= ""<BR>??For? each? x? in? rs.fields <BR>???strLine=? strLine? &chr(9)&chr(9)&""&? HTMLEncode(x.value) &""& vbnewline<BR>??Next<BR>??rs.MoveNext<BR>??Response.write strLine<BR>??Response.write chr(9)&""& vbnewline & vbnewline<BR>?Wend<BR>Response.write ""& vbnewline<BR>if filetype<>"htm" and filetype<>"xls" and filetype<>"sql" then<BR>Response.write "<p style='line-height:160%;'>"&i&"条记录 导出HTML"<BR>Response.write "|导出EXCEL"<BR>Response.write "|导出XML"<BR>Response.write "|导出SQL"& vbnewline<BR>end if<BR>Response.write "<p>Power by <A HREF=""<A href="http://www.51windows.Net">http://www.51windows.Net</A>"" target=""_blank"">51windows.Net</A>"& vbnewline<BR>Response.write "</div>"& vbnewline<BR>Response.write "</BODY>"& vbnewline<BR>Response.write "</HTML>"& vbnewline</P><P>end if<BR>rs.close<BR>conn.close<BR>Set rs=nothing<BR>Set conn=nothing <BR>%></P> <ul class="list-group"> <li class="list-group-item "><a href="/article/33231">数据库导出xml、htm、csv、sql</a></li> <li class="list-group-item "><a href="/article/37838">数据库导出xml、htm、csv、sql</a></li> <li class="list-group-item "><a href="/article/153121">数据库导出xml、htm、csv、sql代码</a></li> <li class="list-group-item "><a href="/article/2522397">数据库导出xml、htm、csv、sql代码</a></li> <li class="list-group-item "><a href="/article/3479726">SQL导出CSV</a></li> <li class="list-group-item "><a href="/article/5087519">MySql数据库导出csv</a></li> <li class="list-group-item "><a href="/article/7066566">数据库CSV导出</a></li> <li class="list-group-item "><a href="/article/10600399">MySql数据库导出csv</a></li> <li class="list-group-item "><a href="/article/2876790">mysql SQL文导出CSV</a></li> <li class="list-group-item "><a href="/article/6408046">java数据库csv文件导出</a></li> <li class="list-group-item "><a href="/article/9185403">PHP CSV导入导出数据库</a></li> <li class="list-group-item "><a href="/article/10966576">MySql数据库导出csv文件</a></li> <li class="list-group-item "><a href="/article/9144483">JS导出excel、doc、png、pdf 、xml、json、sql、txt、powerpoint、csv</a></li> <li class="list-group-item "><a href="/article/10970291">Table表格导出为Excel、csv、txt、sql、json、xml、Word格式</a></li> <li class="list-group-item "><a href="/article/3836103">导出 Silverlight DataGrid to Excel XML/CSV</a></li> <li class="list-group-item "><a href="/article/1098929">PL/SQL Developer 导入导出csv文件</a></li> <li class="list-group-item "><a href="/article/7994074">PL/SQL developer 导出CSV文件</a></li> <li class="list-group-item "><a href="/article/10180943">指定SQL文本导出CSV文件</a></li> <li class="list-group-item "><a href="/article/33226">当他不再爱你的时候</a></li> <li class="list-group-item "><a href="/article/33227">批量反编译java类文件</a></li> <li class="list-group-item "><a href="/article/33228">[转]在网页中动态的生成一个gif图片</a></li> <li class="list-group-item "><a href="/article/33229">Command 命令集</a></li> <li class="list-group-item "><a href="/article/33230">电脑坏了</a></li> <li class="list-group-item "><a href="/article/33231">数据库导出xml、htm、csv、sql</a></li> <li class="list-group-item "><a href="/article/33232">娶老婆的15条金科玉律</a></li> <li class="list-group-item "><a href="/article/33233">系统测试软件的收集整理</a></li> <li class="list-group-item "><a href="/article/33234">生活哲理小故事系列(转贴)</a></li> <li class="list-group-item "><a href="/article/33235">系统测试工具</a></li> <li class="list-group-item "><a href="/article/33236">Asp.net页面输出到EXCEL (转)</a></li> <li class="list-group-item "><a href="/article/33237">OpenSource 谁来给我介绍介绍经验</a></li> <li class="list-group-item "><a href="/article/33238">在JBuilder中須如何得其JNDI</a></li> <li class="list-group-item "><a href="/article/33239">.net无法新建Web项目</a></li> </ul> </div> </div> <div class="col-md-3 "> <div class="list-group blogblock"> <div class="media list-group-item"> <a href="/blog/1847" class="media-left col-xs-4"><img src="/upload/newsimg/2847.jpg" alt="海文网络计划视频教程" class="img-circle"></a> <a href="/blog/1847" class="media-body"><strong>海文网络计划视频教程</strong></a> </div> <div class="list-group-item clearfix"><span class="col-xs-4">原创</span><span class="col-xs-4">粉丝</span><span class="col-xs-4">点击</span></div> </div> <div class="list-group index-blogs"> <strong class="list-group-item active">热门IT博客</strong> <div class="media list-group-item"> <a href="/blog/38824" class="media-left col-xs-4"><img src="/upload/newsimg/3680.jpg" alt="配置linux服务器" class="img-circle"></a><a href="/blog/38824" class="media-body"><strong>配置linux服务器</strong></a> </div> <div class="media list-group-item"> <a href="/blog/38825" class="media-left col-xs-4"><img src="/upload/newsimg/3681.jpg" alt="淘宝店招html代码" class="img-circle"></a><a href="/blog/38825" class="media-body"><strong>淘宝店招html代码</strong></a> </div> <div class="media list-group-item"> <a href="/blog/38826" class="media-body"><strong>au2017软件破解</strong></a> </div> <div class="media list-group-item"> <a href="/blog/38827" class="media-body"><strong>怎样伪造淘宝授权书</strong></a> </div> <div class="media list-group-item"> <a href="/blog/38828" class="media-body"><strong>淘宝店铺简介格式</strong></a> </div> <div class="media list-group-item"> <a href="/blog/38829" class="media-body"><strong>java二级考试历年真题</strong></a> </div> <div class="media list-group-item"> <a href="/blog/38830" class="media-body"><strong>大数据智能营销系统</strong></a> </div> <div class="media list-group-item"> <a href="/blog/38831" class="media-body"><strong>csdn 博客大牛 大数据</strong></a> </div> <div class="media list-group-item"> <a href="/blog/38832" class="media-body"><strong>iphone软件更新后闪退</strong></a> </div> <div class="media list-group-item"> <a href="/blog/38833" class="media-body"><strong>如何修改手机mac地址</strong></a> </div> <div class="media list-group-item"> <a href="/blog/38834" class="media-body"><strong>seed软件下载</strong></a> </div> <div class="media list-group-item"> <a href="/blog/38835" class="media-body"><strong>南京世纪桥软件</strong></a> </div> <div class="media list-group-item"> <a href="/blog/38836" class="media-body"><strong>滴油雾化器绕丝数据</strong></a> </div> <div class="media list-group-item"> <a href="/blog/38837" class="media-body"><strong>微信群聊天机器人源码</strong></a> </div> <div class="media list-group-item"> <a href="/blog/38838" class="media-body"><strong>工业企业数据 最新年限</strong></a> </div> <div class="media list-group-item"> <a href="/blog/38839" class="media-body"><strong>信用卡扫码套现软件</strong></a> </div> <div class="media list-group-item"> <a href="/blog/38840" class="media-body"><strong>linux的vnc服务</strong></a> </div> <div class="media list-group-item"> <a href="/blog/38841" class="media-body"><strong>手机网络不稳定怎么办</strong></a> </div> <div class="media list-group-item"> <a href="/blog/38842" class="media-body"><strong>软件反跟踪技术</strong></a> </div> <div class="media list-group-item"> <a href="/blog/38843" class="media-body"><strong>mac testosterone</strong></a> </div> </div> <div class="list-group"> <strong class="list-group-item active">热门问题</strong> <a class="list-group-item" href="/focus/show/2438858/1">老师的惩罚</a> <a class="list-group-item" href="/focus/show/2438857/1">人脸识别</a> <a class="list-group-item" href="/focus/show/2438856/1">我在镇武司摸鱼那些年</a> <a class="list-group-item" href="/focus/show/2438855/1">重生之率土为王</a> <a class="list-group-item" href="/focus/show/2438854/1">我在大康的咸鱼生活</a> <a class="list-group-item" href="/focus/show/2438853/1">盘龙之生命进化</a> <a class="list-group-item" href="/focus/show/2438852/1">天生仙种</a> <a class="list-group-item" href="/focus/show/2438851/1">凡人之先天五行</a> <a class="list-group-item" href="/focus/show/2438850/1">春回大明朝</a> <a class="list-group-item" href="/focus/show/2438849/1">姑娘不必设防,我是瞎子</a> <a class="list-group-item" href="/focus/show/68529/1">消</a> <a class="list-group-item" href="/focus/show/68530/1">玄幻之天斗三皇子 风散云消</a> <a class="list-group-item" href="/focus/show/68531/1">双重三连消</a> <a class="list-group-item" href="/focus/show/68532/1">人死之前是三肿三消</a> <a class="list-group-item" href="/focus/show/68533/1">三年不消的淋巴结是恶性的吗</a> <a class="list-group-item" href="/focus/show/68534/1">二消</a> <a class="list-group-item" href="/focus/show/68535/1">老人病危临死前征兆三消三肿</a> <a class="list-group-item" href="/focus/show/68536/1">三清</a> <a class="list-group-item" href="/focus/show/68537/1">三清乡</a> <a class="list-group-item" href="/focus/show/68538/1">三清是谁</a> <a class="list-group-item" href="/focus/show/68539/1">三清都有谁</a> <a class="list-group-item" href="/focus/show/68540/1">什么是三清</a> <a class="list-group-item" href="/focus/show/68541/1">天地三清</a> <a class="list-group-item" href="/focus/show/68542/1">三清是哪三位</a> <a class="list-group-item" href="/focus/show/68543/1">三清有谁</a> <a class="list-group-item" href="/focus/show/68544/1">哪三清</a> <a class="list-group-item" href="/focus/show/68545/1">汽车三清指哪三清</a> <a class="list-group-item" href="/focus/show/68546/1">三清的师傅</a> <a class="list-group-item" href="/focus/show/68547/1">三清是什么意思</a> <a class="list-group-item" href="/focus/show/68548/1">手机三清教程</a> <a class="list-group-item" href="/focus/show/68549/1">三清四帝五老是谁</a> <a class="list-group-item" href="/focus/show/68550/1">手机三清</a> <a class="list-group-item" href="/focus/show/68551/1">三清图片</a> <a class="list-group-item" href="/focus/show/68552/1">三清手机</a> <a class="list-group-item" href="/focus/show/68553/1">一气化三清</a> <a class="list-group-item" href="/focus/show/68554/1">三清是哪三清</a> <a class="list-group-item" href="/focus/show/68555/1">三清祖师</a> <a class="list-group-item" href="/focus/show/68556/1">渡赤水</a> <a class="list-group-item" href="/focus/show/68557/1">三里洋渡风景区旅游</a> <a class="list-group-item" href="/focus/show/68558/1">渡</a> <a class="list-group-item" href="/focus/show/68559/1">三豕渡河</a> <a class="list-group-item" href="/focus/show/68560/1">三里洋渡风景区</a> <a class="list-group-item" href="/focus/show/68561/1">三渡赤水</a> <a class="list-group-item" href="/focus/show/68562/1">三人共渡一日游打一字</a> <a class="list-group-item" href="/focus/show/68563/1">三渡神僧</a> <a class="list-group-item" href="/focus/show/68564/1">宸汐缘中灵汐下凡渡哪三劫</a> <a class="list-group-item" href="/focus/show/68565/1">三湖慈鲷</a> <a class="list-group-item" href="/focus/show/68566/1">画j湖之不良人第三季</a> <a class="list-group-item" href="/focus/show/68567/1">养三湖为什么不建议养杂鲷</a> <a class="list-group-item" href="/focus/show/68568/1">画j湖之不良人第三季免费观看</a> <a class="list-group-item" href="/focus/show/68569/1">纳木错湖必做三件事</a> </div></div> </div> </div> <div id="footer" class="footer hidden-print bg-primary"> <div class="container"> <div class="hide"></div> <p>程序博客网,程序员的互联网技术博客家园。csdn论坛精品 msdn技术资料都在这里</p> </div> </div> <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body> </html>