Excel公式和元数据

有什么办法来提取各种Excel公式的基础元数据。 我在C#中使用Excel公式构build这个框架,并且有兴趣知道每个公式input参数,它们的数据types和返回types。 这将帮助我build立一个基于这个提供的元数据的向导屏幕。

在此先感谢您的帮助。

根据@ ja72提出的build议,从他提到的链接中parsing数据是非常容易的。 我不是很好用C#所以这里是一个vb.net代码,你可以转换为C#

话虽如此,有很多方法可以从C#中查看这个问题

方法1

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

缺点

1)你必须有一个互联网连接

2)这个过程很慢

方法2

创build一个单独的程序导航到URL并使用下面的代码parsing值。 使用下面的代码生成输出到一个文本文件或一个csv文件,并将文件embedded到您的资源中,以便您可以在任何时候使用它

我会build议方式2,但最终的决定是你的。 🙂

 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 

快照

在这里输入图像说明

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

所有功劳归于@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; } } }