Step 1

  1. Start the Excel application.

  2. On the Home tab choose the Cells style to change the header background and sheet background style so it looks as in the following diagram.

    Excel Spreadsheet
    Figure 1: Excel Spreadsheet

  3. Save the Workbook with file extension “xlsx”.

  4. Open Notepad.

  5. Add the code shown below.
    1. <! DOCTYPE html>
    2. <html>
    3. <head>
    4. <style>
    5. table, td, th {
    6. border: 1px solid green;
    7. }
    8. th {
    9. background-color: green;
    10. color: white;
    11. }
    12. fieldset {
    13. border: #00cc00 2px solid;
    14. padding: 10px;
    15. Color: green;
    16. }
    17. </style>
    18. <script language="vbscript" type="text/vbscript">
    19. Sub Export()
    20. Dim mytable
    21. Dim mytable1
    22. Dim tab
    23. Dim n
    24. Dim j
    25. Set objExcel = CreateObject("Excel.Application")
    26. Set objWorkbook = objExcel.Workbooks.Open("C:\Users\Karthikeyan.K\Desktop\Book1.xlsx")
    27. objExcel.Application.Visible =True
    28. objWorkbook.Windows(1).Visible =True
    29. set XlSheet =objWorkbook.Sheets(1)
    30. XlSheet.Activate
    31. Set tab=document.getElementsByTagName("table")(0)
    32. mytable = document.getElementsByTagName("table")(0).rows.length
    33. mytable1= document.getElementsByTagName("table")(0).rows(0).cells.length
    34. For n = 0 to (mytable-1)
    35. For j = 0 To (mytable1-1)
    36. XlSheet.Cells (n + 1, j + 1).Value = tab.Rows(n).Cells(j).innertext
    37. Next
    38. Next
    39. MsgBox "Data Exported Successfully",vbInformation
    40. objWorkbook. Save
    41. objWorkbook. Close
    42. Set objWorkbook = Nothing
    43. Set objExcel = Nothing
    44. End Sub
    45. </script>
    46. </head>
    47. <body>
    48. <form>
    49. <fieldset>
    50. <Center>
    51. <img src="C:\Users\Karthikeyan.K\Desktop\excel_file-128.png" alt="face" height="128" width="128">
    52. <br>
    53. <br>
    54. <table>
    55. <tr>
    56. <th>Firstname</th>
    57. <th>Lastname</th>
    58. </tr>
    59. <tr>
    60. <td>Karthik</td>
    61. <td>K</td>
    62. </tr>
    63. <tr>
    64. <td>Mithu</td>
    65. <td>M</td>
    66. </tr>
    67. <tr>
    68. <td>Vinith</td>
    69. <td>R</td>
    70. </tr>
    71. <tr>
    72. <td>Menon</td>
    73. <td>A</td>
    74. </tr>
    75. </table>
    76. <br>
    77. <input type="button" onclick="Export ()" value="Export" /> <br>
    78. </Center>
    79. </fieldset>
    80. </form>
    81. </body>
    82. </html>
  6. Save the file with the extension “.htm”.

Step 2

Follow the procedure to turn on or turn off VBScript in your Internet Explorer:

  1. Choose Internet Options from the Tools menu.

    Internet Options
    Figure 2

  2. Select security tab from the dialog box.

  3. Click the Custom Level button.

    Custom Level button
    Figure 3

  4. Scroll down until you find the Scripting option.

    Scripting option
    Figure 4

  5. Select the Enable radio button under Active scripting.

  6. Finally click OK and get out.

Output

  1. Open the HTML document.

    User data table
    Figure 5: User data table

  2. Press the Export button.

    Export user data
    Figure 6: Export user data

    Data Exported Successfully
    Figure 7: Data Exported Successfully

  3. Open the Workbook to check sheet1's data.

    User data sheet
    Figure 8: User data sheet

Conclusion

Thanks for reading! I hope you liked this article. Please provide your valuable suggestions.