2012-05-01 130 views
2

是否有任何方法来提取各种Excel公式的基础元数据。我在C#中构建这个框架,该框架使用Excel公式,并且有兴趣了解每个公式输入参数,它们的数据类型和返回类型。这将帮助我基于提供的元数据构建向导屏幕。Excel公式和元数据

在此先感谢您的帮助。

+1

对不起,我不认为有可能这样做。您可以尝试解析在线文档或本地帮助文件,但这并不容易。 – ja72

+0

我在工作表函数中找到了'url':http://office.microsoft.com/client/helppreview.aspx?AssetId=HV805551279990&lcid=1033&NS=EXCEL%2EDEV&Version=12&pid=CH080555125&CTT=4 – ja72

+0

+ 1 to both :)好的问题和好的建议。 '但它不容易'@ ja72:这很容易:) –

回答

2

建立在@ ja72建议的内容上,从他提到的链接解析数据是非常容易的。我不是用C#所以这里不太好是vb.net代码,你可以转换成C#

话虽如此,有很多方法,你可以看看从C#

WAY 1

这个问题

在运行时导航到URL并使用下面的代码解析值。

缺点

1)你必须有互联网连接

2)这个过程是缓慢的

WAY 2

创建一个单独的程序来导航到URL并使用下面的代码解析值。 使用下面的代码来生成输出到一个文本文件或CSV文件,并在文件中嵌入您的资源,让你可以使用它,只要你愿意

我建议WAY 2但决定是最终你的。 :)

CODE

Public Class Form1 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     TextBox1.Clear() 
     GetFormulas() 
     MessageBox.Show ("Done!") 
    End Sub 

    Sub GetFormulas() 
     Dim wc As New Net.WebClient 
     '~~> first get links 
     Dim mainPage As String = wc.DownloadString("http://office.microsoft.com/client/helppreview.aspx?AssetId=HV805551279990&lcid=1033&NS=EXCEL.DEV&Version=12&pid=CH080555125") 
     Dim doc As mshtml.IHTMLDocument2 = New mshtml.HTMLDocument 
     doc.write (mainPage) 
     doc.close() 
     Dim table As mshtml.IHTMLElement = DirectCast(doc.getElementById("vstable"), mshtml.IHTMLElement2).getElementsByTagName("table")(0) 
     Dim links As mshtml.IHTMLElementCollection = table.getElementsByTagName("A") 
     For Each link In links 
      Dim childPage As String = wc.DownloadString(link.getAttribute("href")) 
      Dim doc2 As mshtml.IHTMLDocument2 = New mshtml.HTMLDocument 
      doc2.write (childPage) 
      doc2.close() 
      Dim div2 As mshtml.IHTMLElement2 = doc2.getElementById("m_article") 
      For Each elem As mshtml.IHTMLElement In div2.getElementsByTagName("P") 
       If elem.getAttribute("className") = "signature" Then 
        Dim formulaString As String = elem.innerText 
        TextBox1.AppendText (link.innerText & vbTab & formulaString & vbCrLf) 
       End If 
      Next 
     Next 
    End Sub 
End Class 

快照

enter image description here

注意:以上就是关于如何勉强度日ja72给上面的链接的例子。如果你打算去其他任何链接,那么你将不得不相应地更改代码。另请注意,上述链接中提到的公式适用于Excel 2007以上版本。对于Excel 2003,您将不得不进入另一个链接。在上面的例子中,我没有包含一个STOP按钮,所以一旦程序开始运行,它就不能停止,直到它结束。我相信你可以添加一个按钮来终止提取。

+0

+ 1很好完成。提醒我这个线程http://stackoverflow.com/questions/8798260/html-parsing-of-cricinfo-scorecards/8846791#8846791你真的很喜欢报废大声笑 –

+0

@PradeepKumar:大声笑否:)这只是我发现这个问题非常有趣... –

+0

+1感谢您填写我的建议上的差距。解析HTML并不是我的面包和黄油,我很高兴社区迎来了挑战,并提供了我不能做到的答案。 – ja72

1

所有功劳归于@SiddharthRout。这里是Sid发布的代码的C#转换

在使用C#时,你真的不得不混淆大量的强制转换和转换。但那么这就是C#的工作方式:P

using System; 
using System.Windows.Forms; 

Namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     Public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      textBox1.Clear(); 
      GetFormulas(); 
      MessageBox.Show("Done!"); 
     } 

     public void GetFormulas() 
     { 
      mshtml.HTMLDocument doc = NewHtmlDoc("http://office.microsoft.com/client/helppreview.aspx?AssetId=HV805551279990&lcid=1033&NS=EXCEL.DEV&Version=12&pid=CH080555125"); 
      mshtml.IHTMLElement2 table = (mshtml.IHTMLElement2)(mshtml.IHTMLElement2)((mshtml.IHTMLElement2)doc.getElementById("vstable")).getElementsByTagName("table").item(null, 0); 
      mshtml.IHTMLElementCollection links = table.getElementsByTagName("A"); 
      foreach (mshtml.IHTMLElement link in links) 
      { 
       mshtml.HTMLDocument doc2 = NewHtmlDoc(link.getAttribute("href",0).ToString()); 
       mshtml.IHTMLElement div2 = doc2.getElementById("m_article"); 
       foreach (mshtml.IHTMLElement elem in ((mshtml.IHTMLElement2)div2).getElementsByTagName("P")) 
       { 
        if (elem.getAttribute("className",0) != null && elem.getAttribute("className",0).ToString() == "signature") 
        { 
         string formulaString = elem.innerText; 
         textBox1.AppendText(link.innerText + "\t\t" + formulaString + "\n"); 
        } 
       } 
      } 
     } 

     private mshtml.HTMLDocument NewHtmlDoc(string url) 
     { 
      System.Net.WebClient wc = new System.Net.WebClient(); 
      string page = wc.DownloadString(url); 
      mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)(new mshtml.HTMLDocument()); 
      doc.write(page); 
      doc.close(); 
      return (mshtml.HTMLDocument)doc; 
     } 

    } 
} 
+1

+ 1 :)猜猜我需要用C#进行更多的实验,但是第二个想法......呃! :) –

+1

顺便说一下,我添加了一个停止按钮;)你可能想包括它? ;)http://siddharthrout.wordpress.com/2012/05/02/vb-net-to-retrieve-the-names-and-arguments-of-all-excel-formulas/ –

+0

这是伟大的:) –