views:

223

answers:

1

Hello everyone,

I am using the following code in classic ASP to generate Excel file, the code is simple and it works. I run the code under IIS 7.0 on Windows Vista x86. Two issues,

  1. there is a weird warning box, here is the screen snapshot

http://i27.tinypic.com/2n81udw.jpg

  1. All cells' background is white, no border of each cell shown, here is the screen snapshot,

http://i25.tinypic.com/vy5t2d.jpg

My code,

<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
<%

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>\n"
    + "<head>\n"
    + "<meta http-equiv=\"Content-Type\" "
    + "content=\"text/html; charset=UTF-8\">\n"
    + "<style type=\"text/css\">\n"
    + "html, body, table {\n"
    + "    margin: 0;\n"
    + "    padding: 0;\n"
    + "    font-size: 11pt;\n"
    + "}\n"
    + "table, th, td { \n"
    + "    border: 0.1pt solid #D0D7E5;\n"
    + "    border-collapse: collapse;\n"
    + "    border-spacing: 0;\n"
    + "}\n"
    + "</style>\n"
    + "</head>\n"
    + "<body>\n"
    + "<table>\n"
    + "";
}

function getXlsEnd(){
    return ""
    + "</table>\n"
    + "</body>\n"
    + "</html>"
    + "";
}

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());
    var columnCount = 2;
    for (var i=0, il=10; i<il; i+=columnCount) {
        Response.Write("<tr>");
        for (var j=0; j<columnCount; j++) {
            Response.Write("<td>");
            Response.Write(xlsEscape("Data"));
            Response.Write("</td>");
        }
        Response.Write("</tr>\n");
        // prevent Response Buffering Limit Exceeded
        if (i % 1000 === 0) {
            Response.Flush();
        }
    }
    Response.Write(getXlsEnd());
}

function main(){
    var filetype = Request.QueryString("filetype")();
    var filename = "filename";

    Response.Clear();
    writeXls(filename);
    Response.End();
}

main();

%>

Any solutions?

thanks in advance, George

+2  A: 

A. There is a weird warning box...

You are serving up HTML with the file extension set at xls. This is a security violation introduced in the last year or so. Does changing the extension to htm avoid the security prompt?

B. Because your HTML Table only contains two columns and five rows. There are no other "cells", so there are no borders. In order to avoid this, I am pretty sure you need to serve up CVS, Workbook XML, or "other". (In my opinion, I like that there are no borders, but most people do not.)

C. ...code under IIS 7.0 on Windows Vista x86...

This is an Excel and security issue, and not related to IIS or Windows.

D. Sorry, I don't have a specific answer for you because I been using Syncfusion 3.0 the last few years. It serves up the correct Excel formatted workbooks and there is no issues.

AMissico
For A, I am not hosting in html, I am hosting in asp. How to dynamically generate code in htm? For B, CSV if fine, but if the cell contains commma sign, it will conflicting with delimeter comma sign, for example, if in cell (A, 1), I want to input "hello, world" and in cell (B, 1), I want to input "Hello, Excel", then there will be 4 cells. Any ideas to solve issue when cell content containing comma?
George2
A) You are hosting in ASP, but you are serving an HTML table via the ASP code. You are forcing Excel to open the HTML table as a workbook, which is okay.
AMissico
B) Excel uses a little different CSV, but don't worry about that. Just "quote" your strings that contain a comma, such as "Anthony, Jr.".
AMissico