有没有办法提取各种excel公式的底层元数据。我在C#中使用excel公式构建此框架,并且有兴趣了解每个公式输入参数,它们的数据类型和返回类型。这将帮助我根据提供的元数据构建向导屏幕。
提前感谢您的帮助。
答案 0 :(得分:2)
在@ ja72建议的基础上,很容易从他提到的链接中解析数据。我对C#不太好,所以这里是一个vb.net代码,你可以转换为C#
话虽如此,您可以通过多种方式从C#
中查看此问题方式1
在运行时导航到URL并使用以下代码解析值。
缺点 :
1)您必须有互联网连接
2)这个过程很慢
第2天
创建一个单独的程序以导航到URL并使用以下代码解析值。 使用以下代码生成文本文件或csv文件的输出,并将该文件嵌入您的资源中,以便您可以随时使用它
我建议 WAY 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,您将不得不进入另一个链接。我没有在上面的示例中包含STOP
按钮,所以一旦程序开始运行,它就不能停止直到它完成。我相信你可以再添加一个按钮来终止提取。
答案 1 :(得分: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;
}
}
}