• asp and javascript: sql server export data to csv and to xls


    <%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
    <%
    //塗聚文
    //20131021
    
    function 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 ""
        + "<html>
    "
        + "<head>
    "
        + "<meta http-equiv="Content-Type" "
        + "content="text/html; charset=UTF-8">
    "
        + "<style type="text/css">
    "
        + "html, body, table {
    "
        + "    margin: 0;
    "
        + "    padding: 0;
    "
        + "    font-size: 11pt;
    "
        + "}
    "
        + "table, th, td { 
    "
        + "    border: 0.1pt solid #D0D7E5;
    "
        + "    border-collapse: collapse;
    "
        + "    border-spacing: 0;
    "
        + "}
    "
        + "</style>
    "
        + "</head>
    "
        + "<body>
    "
        + "<table>
    "
        + "";
    }
    
    function getXlsEnd(){
        return ""
        + "</table>
    "
        + "</body>
    "
        + "</html>"
        + "";
    }
    
    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<il; i+=columnCount) {
            for (var j=0; j<columnCount; j++) {
                Response.Write(csvEscape(data[i+j]));
                if (j !== columnCount-1) {
                    Response.Write(";");
                }
            }
            Response.Write("
    ");
            // prevent Response Buffering Limit Exceeded
            if (i % 1000 === 0) {
                Response.Flush();
            }
        }
    }
    
    function xlsEscape(val){
        if (typeof val === "number") {
            return val.toString(10).replace(".", ",");
        } else if (typeof val === "string") {
            return Server.HTMLEncode(val);
        } else if (val === null)  {
            return "#NULL#";
        } else if (val === undefined)  {
            return "#UNDEFINED#";
        } else {
            return "#ERROR#";
        }
    }
    
    function writeXls(filename, data, columnCount){
        writeXlsHttpHeaders(filename);
        Response.Write(getXlsStart());
        for (var i=0, il=data.length; i<il; i+=columnCount) {
            Response.Write("<tr>");
            for (var j=0; j<columnCount; j++) {
                Response.Write("<td>");
                Response.Write(xlsEscape(data[i+j]));
                Response.Write("</td>");
            }
            Response.Write("</tr>
    ");
            // 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 * 
    "
        '+ ", B_Name 
    "
        '+ ", B_EnglishName 
    "
        + "FROM branch 
    "
        + ";";
        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();
    
    %>
    

  • 相关阅读:
    Dump 文件生成与分析
    打造支持apk下载和html5缓存的 IIS(配合一个超简单的android APP使用)具体解释
    GridView编辑删除操作
    google域名邮箱申请 gmail域名邮箱申请(企业应用套件)指南
    nvl,空时的推断和取值
    如何将图片保存至自定义分组
    Java实现 蓝桥杯VIP 算法训练 集合运算
    Java实现 蓝桥杯VIP 算法训练 瓷砖铺放
    Java实现 蓝桥杯VIP 算法训练 瓷砖铺放
    Java实现 蓝桥杯VIP 算法训练 集合运算
  • 原文地址:https://www.cnblogs.com/geovindu/p/3380947.html
Copyright © 2020-2023  润新知