By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,389 Members | 2,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,389 IT Pros & Developers. It's quick & easy.

copy the selective html content & paste it in Excel 2007 programmatically (

P: 2
I've a form where an webrowser contains a html file. I want to copy a selective portion of the html content and paste that content in an excel 2007 file where it will be pasted as a normal text (not in HTML code). I want to have the excel part should be as it is done manually (i.e. it should be in proper format as it is in the HTML file).

Platform/OS/Version : GUI/Windows XP/2005
Language :Vb.Net

Expand|Select|Wrap|Line Numbers
  1. Private Sub frmParse_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         WebBrowser1.Navigate(New Uri(""))
  3. End Sub
  5. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  6.         Dim sr As StreamReader = New StreamReader(WebBrowser1.DocumentStream)
  7.         Dim xlAppOut As Excel.Application
  8.         Dim xlBookOut As Excel.Workbook
  9.         Dim xlSheet1 As Excel.Worksheet
  10.         xlAppOut = CreateObject("Excel.Application")
  11.         xlBookOut = xlAppOut.Workbooks.Add
  12.         xlSheet1 = xlBookOut.Worksheets.Item(1)
  13.         xlAppOut.Visible = True
  14.         Clipboard.Clear()
  15.         Dim strContent As String = GetSelection(WebBrowser1)
  16.         Clipboard.SetText(strContent)
  17.         xlSheet1.Paste()
  18. End Sub
  20. Private Function GetSelection(ByVal webBrowser As WebBrowser) As String
  21.         GetSelection = ""
  22.         Dim doc As IHTMLDocument2 = CType(webBrowser.Document.DomDocument, IHTMLDocument2)
  23.         Dim sel As IHTMLSelectionObject = doc.selection
  24.         Dim range As IHTMLTxtRange = CType(sel.createRange, IHTMLTxtRange)
  25.         GetSelection = range.htmlText
  26. End Function
May 6 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.