[RESOLVED]To remove empty cells while exporting html to excel in asp.net

I have an .aspx code which contains one table ..Now I want to export all the values into the Excel File and I don’t want to export empty cell (<td></td>) in excel file..

My .aspx code is

<table>
    
    <tr><td>Hello</td><td>World</td></tr>
    <tr><td>   </td><td>   </td></tr>
    <tr><td>   </td><td>   </td></tr>
    <tr><td>Hello</td><td>World</td></tr>
    <tr><td>   </td><td>   </td></tr>
    <tr><td>   </td><td>   </td></tr>
    
</table>

Hi,

If your table is in string form, try trimming white spaces or replace with empty string.

if that is not what u are after, explain what issue u r facing with empty cells.

hi …

I can export html table into excel file…. that works fine…but i got empty cells between data.. so how can remove empty cells with data… i attached the link for,

use this link..

http://jsfiddle.net/cCzqn/294/

if you Response object to convert your page to excel … in excel you will get whatever there is on page.

if you need to customize excel output you have to use Microsoft.Interop.Office.Excel library.

http://dotnetmentors.com/c-sharp/how-to-export-sql-data-to-excel-using-microsoft-office-interop.aspx

Hi,

I updated ur code, which removes empty cells in between data.

http://jsfiddle.net/cCzqn/295/

Hi Jagadiz,

Thanks for your post.

Jagadiz61

I can export html table into excel file…. that works fine…but i got empty cells between data.. so how can remove empty cells with data

As for your problem, you could remove blank rows from table via jQuery as below.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="../../Scripts/jquery-1.8.2.js"></script>
    <script>
        $(function () {
            $('#tb1 tr').each(function () {
                if (!$.trim($(this).text()))
                    $(this).remove();
            });
        })
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table id="tb1">
                <tr>
                    <td>Hello</td>
                    <td>World</td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td>Hello</td>
                    <td>World</td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

Hope it will be helpful to you.

Best Regards,

Fei Han

Jagadiz61

I can export html table into excel file…. that works fine…but i got empty cells between data.. so how can remove empty cells with data… i attached the link for,

Check the sample application I’ve created for you, using your jsFiddle

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript"
        src="http://code.jquery.com/jquery-latest.min.js" charset="utf-8">
    </script>
    <script type="text/javascript">
        $(function () {
            $("[id$=myButtonControlID]").click(function (e) {
                window.open('data:application/vnd.ms-excel,' + encodeURIComponent($($('div[id$=divTableDataHolder]').html().replace(/(<!--.*(?!-->))|s+/g, '')).find('td:empty').remove().end().find('tr:empty').remove().end()[0].outerHTML));
                e.preventDefault();
            });
        })
    </script>
</head>
<body>
        <button id="myButtonControlID">Export Table data into Excel</button>
        <div id="divTableDataHolder">
            <table>

                <tr>
                    <td>Hello</td>
                    <td>World</td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td>Hello</td>
                    <td>World</td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                </tr>

            </table>
        </div>
</body>
</html>

Leave a Reply