?
Solved

How to Convert HTML table to .csv compatible file via ASP?

Posted on 2009-04-21
6
Medium Priority
?
972 Views
Last Modified: 2012-05-06
I have created a page which loads SQL data into a table and displays it to the user...from here the user can add/remove and sort various columns via a few Javascript functions.  The add/remove and sort is done COMPLETELY on the client-side with no server interaction.

Is it possible to create an ASP function which when called would loop through this table which has been modified by javascript, and create a .csv compatible file which the user could then download?
0
Comment
Question by:drmans
  • 3
  • 3
6 Comments
 
LVL 17

Expert Comment

by:NicksonKoh
ID: 24201519
Hi,

You can do it simply in javascript like code shown below. Give an id like what i did to the table.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
 
    <script type="text/javascript" language="javascript">
        function Extract() {
            var lines = "";
 
            tb = document.getElementById("TableA")
            for (var n = 0; n < TableA.rows.length; n++) {
                line = "";
                for (var m = 0; m < TableA.rows[n].cells.length; m++) {
                    var cell = TableA.rows[n].cells[m]
                    if (line != "") line += ",";
                    line += cell.innerText;
                }
                if (lines != "") lines += "\n";
                lines += line;
            }
            document.getElementById("TextArea1").value = lines;
        }
    </script>
 
</head>
<body>
    &nbsp;<table style="width: 100%;" id="TableA" name="TableA">
        <tr name="A">
            <td>
                Header1
            </td>
            <td>
                Header2
            </td>
            <td>
                Header3
            </td>
        </tr>
        <tr>
            <td>
                1
            </td>
            <td>
                abc
            </td>
            <td>
                sfd
            </td>
        </tr>
        <tr>
            <td>
                2
            </td>
            <td>
                asdf
            </td>
            <td>
                dfdsf
            </td>
        </tr>
    </table>
    <p>
        <input id="Button1" type="button" value="button" onclick="Extract()" /></p>
    <textarea id="TextArea1" cols="20" rows="10"></textarea>
</body>
</html>

Open in new window

0
 

Author Comment

by:drmans
ID: 24215589
Thanks for your response...that all works fine.  The problem I'm having now is how to automatically invoke a 'save as' dialog box to save the content created by your "Extract" function.  Any thoughts??
0
 
LVL 17

Accepted Solution

by:
NicksonKoh earned 2000 total points
ID: 24221144
In this case, I would suggest that extract result write to a hidden text field in a form and submit the result to another asp page.

In that asp page, you would write the result to another text file with .csv extension using FileSystemObject and then Server.Transfer to that csv file. The GenerateCSV.asp would be something like

<%
strText = Request.Form("hfText")

... Use FSO to create csv file with strText...

Server.Transfer(strFilename)
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
 
    <script type="text/javascript" language="javascript">
        function Extract() {
            var lines = "";
 
            tb = document.getElementById("TableA")
            for (var n = 0; n < TableA.rows.length; n++) {
                line = "";
                for (var m = 0; m < TableA.rows[n].cells.length; m++) {
                    var cell = TableA.rows[n].cells[m]
                    if (line != "") line += ",";
                    line += cell.innerText;
                }
                if (lines != "") lines += "\n";
                lines += line;
            }
            //document.getElementById("TextArea1").value = lines;
 
            document.getElementById("hfText").value = lines;
            document.forms[0].submit();
        }
    </script>
 
</head>
<body>
     <table style="width: 100%;" id="TableA" name="TableA">
        <tr name="A">
            <td>
                Header1
            </td>
            <td>
                Header2
            </td>
            <td>
                Header3
            </td>
        </tr>
        <tr>
            <td>
                1
            </td>
            <td>
                abc
            </td>
            <td>
                sfd
            </td>
        </tr>
        <tr>
            <td>
                2
            </td>
            <td>
                asdf
            </td>
            <td>
                dfdsf
            </td>
        </tr>
    </table>
    <p>
        <input id="Button1" type="button" value="button" onclick="Extract()" /></p>
    <textarea id="TextArea1" cols="20" rows="10"></textarea>
    <form action="GenerateCSV.asp" method="post" name="form1" target="_self">
    <input name="hfText" type="hidden" id="hfText">
    </form>
</body>
</html>

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:drmans
ID: 24225343
Great!  This is just what I need.  Thanks so much.

One last question...how would I go about inserting code at the end of this process that would automatically delete files created by the FSO off the server after they have been saved to the users computer?
0
 
LVL 17

Expert Comment

by:NicksonKoh
ID: 24230176
You cannot reliably do that based on "after user has saved".

It is simply easier to a write a dos batch file to delete csv files created and schedule to run this batch at an appropriate timing.
0
 

Author Comment

by:drmans
ID: 24250851
Thanks a bunch...everything's working perfectly now.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question