2011-07-29 41 views
1

我必須編寫一個程序,定期讀取網頁並將該頁面上的表中的某些數據複製到Excel電子表格中。我不知道從哪裏開始,或者哪種編程語言適合這個項目。我知道一點C++和Matlab編程。任何人都可以提供意見,指出我在正確的方向或建議開源項目做類似的事情嗎?在Excel中複製html表格

我可以使用wget(linux)或fget1(matlab)下載網頁,但我不知道如何將本網頁源文件的某些數據保存到Excel中。

+0

你每次需要創建一個新的Excel文檔,還是修改現有的文檔? –

+0

[用PHP&jQuery將HTML導出爲EXCEL](http://stackoverflow.com/questions/4411503/export-html-to-excel-with-php-jquery) –

回答

0

我會假設你有學習C#的空間。由於您必須從網頁中提取表格,因此需要特殊的庫/框架來處理諸如Watin等網頁瀏覽。獲得表後,這是保存到Excel電子表格中的問題。爲了方便起見,您可以編寫CSV格式(逗號分隔的文本),並且Excel可以打開該文件。希望它有幫助

0

我使用下面的代碼vb.net解析多個html表格從保存的網頁到數據表(表必須具有相同的結構)(使用Html-Agility-Pack)並將其保存到Xml文件:

Imports System.Net 

    Public Sub ParseHtmlTable(byval HtmlFilePath as String) 

    Dim webStream As Stream 
    Dim webResponse = "" 
    Dim req As FileWebRequest 
    Dim res As FileWebResponse 

    req = WebRequest.Create("file:///" & HtmlFilePath) 

    req.Method = "GET" ' Method of sending HTTP Request(GET/POST) 

    res = req.GetResponse ' Send Request 

    webStream = res.GetResponseStream() ' Get Response 

    Dim webStreamReader As New StreamReader(webStream) 

    Dim htmldoc As New HtmlAgilityPack.HtmlDocument 
    htmldoc.LoadHtml(webStreamReader.ReadToEnd()) 

    Dim nodes As HtmlAgilityPack.HtmlNodeCollection = htmldoc.DocumentNode.SelectNodes("//table/tr") 

    Dim dtTable As New DataTable("Table1") 

    Dim Headers As List(Of String) = nodes(0).Elements("th").Select(Function(x) x.InnerText.Trim).ToList 

    For Each Hr In Headers 

     dtTable.Columns.Add(Hr) 

    Next 

    For Each node As HtmlAgilityPack.HtmlNode In nodes 

     Dim Row = node.Elements("td").Select(Function(x) x.InnerText.Trim).ToArray 

     dtTable.Rows.Add(Row) 

    Next 

    dtTable.WriteXml("G:\1.xml", XmlWriteMode.WriteSchema) 

    End Sub 

是導入文件到Excel

後閱讀本Article導入XML到Excel

希望它可以幫助