Step 1
- Start the Excel application.
- 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.
Figure 1: Excel Spreadsheet
- Save the Workbook with file extension “xlsx”.
- Open Notepad.
- Add the code shown below.
- <! DOCTYPE html>
- <html>
- <head>
- <style>
- table, td, th {
- border: 1px solid green;
- }
- th {
- background-color: green;
- color: white;
- }
- fieldset {
- border: #00cc00 2px solid;
- padding: 10px;
- Color: green;
- }
- </style>
- <script language="vbscript" type="text/vbscript">
-
-
- Sub Export()
- Dim mytable
- Dim mytable1
- Dim tab
- Dim n
- Dim j
- Set objExcel = CreateObject("Excel.Application")
- Set objWorkbook = objExcel.Workbooks.Open("C:\Users\Karthikeyan.K\Desktop\Book1.xlsx")
- objExcel.Application.Visible =True
- objWorkbook.Windows(1).Visible =True
- set XlSheet =objWorkbook.Sheets(1)
- XlSheet.Activate
- Set tab=document.getElementsByTagName("table")(0)
- mytable = document.getElementsByTagName("table")(0).rows.length
- mytable1= document.getElementsByTagName("table")(0).rows(0).cells.length
- For n = 0 to (mytable-1)
- For j = 0 To (mytable1-1)
- XlSheet.Cells (n + 1, j + 1).Value = tab.Rows(n).Cells(j).innertext
- Next
-
- Next
- MsgBox "Data Exported Successfully",vbInformation
- objWorkbook. Save
- objWorkbook. Close
- Set objWorkbook = Nothing
- Set objExcel = Nothing
- End Sub
- </script>
- </head>
- <body>
- <form>
- <fieldset>
- <Center>
- <img src="C:\Users\Karthikeyan.K\Desktop\excel_file-128.png" alt="face" height="128" width="128">
- <br>
- <br>
- <table>
- <tr>
- <th>Firstname</th>
-
- <th>Lastname</th>
- </tr>
- <tr>
- <td>Karthik</td>
- <td>K</td>
- </tr>
- <tr>
- <td>Mithu</td>
- <td>M</td>
- </tr>
- <tr>
- <td>Vinith</td>
- <td>R</td>
- </tr>
- <tr>
- <td>Menon</td>
- <td>A</td>
- </tr>
- </table>
- <br>
-
- <input type="button" onclick="Export ()" value="Export" /> <br>
- </Center>
- </fieldset>
- </form>
- </body>
- </html>
- Save the file with the extension “.htm”.
Step 2
Follow the procedure to turn on or turn off VBScript in your Internet Explorer:
- Choose Internet Options from the Tools menu.
Figure 2
- Select security tab from the dialog box.
- Click the Custom Level button.
Figure 3
- Scroll down until you find the Scripting option.
Figure 4
- Select the Enable radio button under Active scripting.
- Finally click OK and get out.
Output
- Open the HTML document.
Figure 5: User data table
- Press the Export button.
Figure 6: Export user data
Figure 7: Data Exported Successfully
- Open the Workbook to check sheet1's data.
Figure 8: User data sheet
Conclusion
Thanks for reading! I hope you liked this article. Please provide your valuable suggestions.