博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp and javascript: sql server export data to csv and to xls
阅读量:6823 次
发布时间:2019-06-26

本文共 3114 字,大约阅读时间需要 10 分钟。

<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%><%//塗聚文//20131021function getData(connectionString, sql){    var result = null;    var adStateOpen = 1;    var connection = new ActiveXObject("ADODB.CONNECTION");    try{        connection.Open(connectionString);    } catch(e1){        return null;    }    if (connection.State !== adStateOpen) {        return null;    }    try{        var recordset = connection.Execute(sql);    } catch(e2){        return null;    }    if (!recordset.EOF) {        result = recordset.GetRows().toArray();        recordset.Close();    }    recordset = null;    connection.Close();    connection = null;    return result;}function writeCsvHttpHeaders(filename){    Response.ContentType = "text/csv";    Response.Charset = "utf-8";    Response.AddHeader("Content-Disposition",             "attachment; filename="+filename+".csv");}function writeXlsHttpHeaders(filename){    Response.ContentType = "application/vnd.ms-excel";    Response.Charset = "utf-8";    Response.AddHeader("Content-Disposition",             "attachment; filename="+filename+".xls");}function getXlsStart(){    return ""    + "\n"    + "\n"    + "
\n" + "\n" + "\n" + "\n" + "
\n" + "";}function getXlsEnd(){ return "" + "
\n" + "\n" + "" + "";}function csvEscape(val){ if (typeof val === "number") { return val.toString(10).replace(".", ","); } else if (typeof val === "string") { if (val.indexOf("\"") !== -1) { return "\""+val.replace(/"/g, "\"\"")+"\""; } else if (val.indexOf(";") !== -1) { return "\""+val+"\""; } else { return val; } } else if (val === null) { return "#NULL#"; } else if (val === undefined) { return "#UNDEFINED#"; } else { return "#ERROR#"; }}function writeCsv(filename, data, columnCount){ writeCsvHttpHeaders(filename); // utf-8 BOM (very important for special characters) Response.Write("\uFEFF"); for (var i=0, il=data.length; i
"); for (var j=0; j
"); Response.Write(xlsEscape(data[i+j])); Response.Write(""); } Response.Write("\n"); // prevent Response Buffering Limit Exceeded if (i % 1000 === 0) { Response.Flush(); } } Response.Write(getXlsEnd());}function main(){ var filetype ="Excel" ' Request.QueryString("filetype")(); var connectionString = "Provider=SQLOLEDB.1;" + "Data Source=188.68.218.86;" + "User ID=sa;" + "Password=2011intranet;" + "Initial Catalog=intranet"; var sql = "" + "SELECT * \n" '+ ", B_Name \n" '+ ", B_EnglishName \n" + "FROM branch \n" + ";"; var filename = "filename"; var columnCount = 3; var data = getData(connectionString, sql); if (data !== null) { Response.Clear(); if (filetype == "csv") { writeCsv(filename, data, columnCount); } else { writeXls(filename, data, columnCount); } } else { Response.Write("Error, no data found"); } Response.End();}main();%>

转载地址:http://qsozl.baihongyu.com/

你可能感兴趣的文章
据说是iOS开发一年总结的笔记,有空看看
查看>>
修改 ubuntu 默认启动项
查看>>
Java递归删除目录中的子目录和文件的方法
查看>>
Android startActivity 隐式调用, 启动其他Activity过程
查看>>
webSocket 入门demo
查看>>
输入框样式定义学习笔记
查看>>
eclipse 快捷键
查看>>
ioc 注解的流程
查看>>
nexus私服无法下载jar包
查看>>
HTML 速查列表
查看>>
javascript 设计模式之单体(Singleton)模式
查看>>
如何禁止手机浏览器上下滚动?
查看>>
SpringBoot学习之路:13.SpringBoot使用Lombok简化JavaBean代码
查看>>
常考面试题及汇总答案
查看>>
easyUI-DataGrid加载数据和分页
查看>>
js Blob对象实现文件下载功能
查看>>
IT自学资源
查看>>
jQuery零基础入门——(七)事件
查看>>
Android 之窗口小部件详解--App Widget
查看>>
Java并发知识点总结
查看>>