在C#中添加在Excel [2007]超链接 - 在Excel中它的自我 [英] Adding hyperlinks in Excel[2007] in C# - Within Excel it self
本文介绍了在C#中添加在Excel [2007]超链接 - 在Excel中它的自我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人能告诉我如何,我们可以在.NET(C#)
Can anybody tell me how we can add a hyperlink in Excel (2007 or later) from a cell in one sheet to a cell in another sheet using Office Interop in .NET (c#)
例如:从Sheet1中单元格A1到Sheet2单元格B10的超链接
For example: A hyperlink from Sheet1 Cell A1 to Sheet2 Cell B10
推荐答案
您想要什么在这里使用的是 Hyperlinks.Add < 。/ A>法
What you want to use here is the Hyperlinks.Add method.
您可以用代码看起来像这样称呼它:
You can call it with code that looks something like this:
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
Excel.Range rangeToHoldHyperlink = worksheet.get_Range("A1", Type.Missing);
string hyperlinkTargetAddress = "Sheet2!A1";
worksheet.Hyperlinks.Add(
rangeToHoldHyperlink,
string.Empty,
hyperlinkTargetAddress,
"Screen Tip Text",
"Hyperlink Title");
下面是一个全自动化的例子,你可以测试:
Here is a full automation example that you can test:
void AutomateExcel()
{
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Workbook workbook = excelApp.Workbooks.Add(Type.Missing);
workbook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
workbook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
Excel.Range rangeToHoldHyperlink = worksheet.get_Range("A1", Type.Missing);
string hyperlinkTargetAddress = "Sheet2!A1";
worksheet.Hyperlinks.Add(
rangeToHoldHyperlink,
string.Empty,
hyperlinkTargetAddress,
"Screen Tip Text",
"Hyperlink Title");
MessageBox.Show("Ready to clean up?");
// Cleanup:
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
Marshal.FinalReleaseComObject(range);
Marshal.FinalReleaseComObject(worksheet);
workbook.Close(false, Type.Missing, Type.Missing);
Marshal.FinalReleaseComObject(workbook);
excelApp.Quit();
Marshal.FinalReleaseComObject(excelApp);
}
希望这有助于!
Hope this helps!
迈克
这篇关于在C#中添加在Excel [2007]超链接 - 在Excel中它的自我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文