How to Export to excel keeping text colour same ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 127mohit
    New Member
    • Nov 2009
    • 1

    How to Export to excel keeping text colour same ?

    Hi all,

    I have a simple code that exports an HTML table on an ASP page to excel.
    The code works fine, however if the original text in the HTML table had a particular colour, it is not exported in the same way.The resulting excel file has all text black only.
    Pls. suggest how to solve this problem.

    Any help would be greatly appreciated .


    Here's the code:



    Code:
    <html>
    
    <head>
    <script type="text/javascript">
    
    function CreateExcelSheet()
    {
    
    
    var x=myTable.rows
    
    var xls = new ActiveXObject("Excel.Application")
    xls.visible = true
    xls.Workbooks.Add
    for (i = 0; i < x.length; i++)
    {
    var y = x[i].cells
    
    for (j = 0; j < y.length; j++)
    {
    xls.Cells( i+1, j+1).Value = y[j].innerText
    }
    }
    
    }
    </script>
    
    
    </head>
    
    <body marginheight="0" marginwidth="0">
    <form>
    <input type="button" onclick="CreateExcelSheet()" value="Export to Excel !">
    </form>
    <table id="myTable" border="1">
     <tr>
            <td>Test</td>
    <td><font color="red">Test</td>
    <td>Test</td>
    <td>Test</td>
    <td>Test</td>
        </tr>
    
     <tr>
            <td>Test</td>
    <td>Test</td>
    <td>Test</td>
    <td>Test</td>
    <td>Test</td>
        </tr> 
    <tr>
            <td>Test</td>
    <td>Test</td>
    <td>Test</td>
    <td>Test</td>
    <td>Test</td>
        </tr> 
    <tr>
            <td>Test</td>
    <td>Test</td>
    <td>Test</td>
    <td>Test</td>
    <td>Test</td>
        </tr>
    </table>
    
    </body>
    </html>
    Last edited by Dormilich; Nov 20 '09, 05:47 PM. Reason: Please use [code] tags when posting code
  • rnd me
    Recognized Expert Contributor
    • Jun 2007
    • 427

    #2
    a couple other tricks to try:

    in head:

    Code:
     
    	<style type="text/css">
    			.green {color:green;}
    		</style>

    in table:
    Code:
    <tr>
            <td>Test</td>
    <td>Test</td>
    <td class="green">Test</td>
    <td style="color:blue">Test</td>
    <td>Test</td>
        </tr>

    Comment

    Working...