在 C# 中的 Excel[2007] 中添加超链接 - 在 Excel 本身中

发布于 2024-08-03 12:45:01 字数 145 浏览 11 评论 0原文

谁能告诉我如何在 Excel(2007 或更高版本)中添加超链接 使用 .NET (c#) 中的 Office Interop 将一张工作表中的单元格连接到另一张工作表中的单元格

例如:从 Sheet1 单元格 A1 到 Sheet2 单元格 B10 的超链接

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#)

For example: A hyperlink from Sheet1 Cell A1 to Sheet2 Cell B10

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧故 2024-08-10 12:45:01

您要在此处使用的是 Hyperlinks.Add 方法。

您可以使用如下所示的代码来调用它:

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");

这是一个可以测试的完整自动化示例:

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);
}

希望这会有所帮助!

麦克风

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!

Mike

浅忆 2024-08-10 12:45:01

我是这样做的:

        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        xlApp = new Excel.ApplicationClass();
        xlWorkBook = xlApp.Workbooks.Add(System.Reflection.Missing.Value);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
        Excel.Hyperlink link =
            (Excel.Hyperlink)
            xlWorkSheet.Hyperlinks.Add(xlWorkSheet.get_Range("L500", Type.Missing), "#Sheet1!B1", Type.Missing,
                                       "Go top",
                                       "UP");

        xlWorkSheet.Hyperlinks.Add(xlWorkSheet.get_Range("C5", Type.Missing), "www.google.com", Type.Missing, "Click me to go to Google ","Google.com");                                                     
        xlApp.Visible = true;

在指向书中单元格的链接中插入符号#非常重要,如果不插入该符号,则链接将被破坏。

我在一篇俄语文章中描述了这个决定,可以找到一个例子 此处

I do it so:

        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        xlApp = new Excel.ApplicationClass();
        xlWorkBook = xlApp.Workbooks.Add(System.Reflection.Missing.Value);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
        Excel.Hyperlink link =
            (Excel.Hyperlink)
            xlWorkSheet.Hyperlinks.Add(xlWorkSheet.get_Range("L500", Type.Missing), "#Sheet1!B1", Type.Missing,
                                       "Go top",
                                       "UP");

        xlWorkSheet.Hyperlinks.Add(xlWorkSheet.get_Range("C5", Type.Missing), "www.google.com", Type.Missing, "Click me to go to Google ","Google.com");                                                     
        xlApp.Visible = true;

It is important to insert the symbol # in the link that leads to a cell inside the book, if this symbol is not inserted, then the link is broken.

I described this decision in an article in Russian language, an example can be found here

琴流音 2024-08-10 12:45:01

希望下面的一篇能够帮助到你。

xlNewSheet.Hyperlinks.Add(xlWorkRange, string.Empty, "'Detailed Testcase Summary'!A1", "Click Here", "Please click me to go to Detailed Test case Summary Result");    

Hope below one will help you.

xlNewSheet.Hyperlinks.Add(xlWorkRange, string.Empty, "'Detailed Testcase Summary'!A1", "Click Here", "Please click me to go to Detailed Test case Summary Result");    
暗喜 2024-08-10 12:45:01

添加图片链接(已插入工作表中):

Hyperlinks hyperlinks = ws.Hyperlinks;
Hyperlink hyperlink = hyperlinks.Add(picture.ShapeRange.Item(1), "http://stackoverflow.com");

您不是将其直接添加到图片中,而是将其 ShapeRange 中的第一项添加到图片中。 (不管那是什么……)

To add a link to a picture (already inserted into the sheet):

Hyperlinks hyperlinks = ws.Hyperlinks;
Hyperlink hyperlink = hyperlinks.Add(picture.ShapeRange.Item(1), "http://stackoverflow.com");

You are not adding it directly to the picture, but the 1st item in it's ShapeRange. (Whatever that is...)

梦途 2024-08-10 12:45:01

@Mike Rosenblums 的答案翻译成 Delphi 也有效:

uses
  Excel2000;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  AExcelApplication: ExcelApplication;
  AExcelWorkSheet: ExcelWorkSheet;
  ARange: Range;
  hyperlinkTargetAddress:String;
begin

  AExcelApplication := CoExcelApplication.Create;
  AExcelApplication.Visible[0] := true;
  AExcelApplication.Workbooks.Add('',0);
  AExcelApplication.Worksheets.Add(EmptyParam, EmptyParam, EmptyParam, EmptyParam, 0);
  AExcelApplication.Worksheets.Add(EmptyParam, EmptyParam, EmptyParam, EmptyParam, 0);
  AExcelWorkSheet := AExcelApplication.Worksheets.Item[1] as ExcelWorkSheet;
  ARange := AExcelWorkSheet.Range['A1','A1'];

  hyperlinkTargetAddress := 'Sheet2!A1';
  AExcelWorkSheet.Hyperlinks.Add(
    ARange,
    '',
    hyperlinkTargetAddress,
    'Screen Tip Text',
    'Hyperlink Title1'
  );

  ARange := AExcelWorkSheet.Range['A2','A2'];
  AExcelWorkSheet.Hyperlinks.Add(
    ARange,
    '',
    'Sheet1!A1',
    'Screen Tip Text',
    'Hyperlink Title2'
  );

end;

@Mike Rosenblums answer translated to Delphi that also works:

uses
  Excel2000;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  AExcelApplication: ExcelApplication;
  AExcelWorkSheet: ExcelWorkSheet;
  ARange: Range;
  hyperlinkTargetAddress:String;
begin

  AExcelApplication := CoExcelApplication.Create;
  AExcelApplication.Visible[0] := true;
  AExcelApplication.Workbooks.Add('',0);
  AExcelApplication.Worksheets.Add(EmptyParam, EmptyParam, EmptyParam, EmptyParam, 0);
  AExcelApplication.Worksheets.Add(EmptyParam, EmptyParam, EmptyParam, EmptyParam, 0);
  AExcelWorkSheet := AExcelApplication.Worksheets.Item[1] as ExcelWorkSheet;
  ARange := AExcelWorkSheet.Range['A1','A1'];

  hyperlinkTargetAddress := 'Sheet2!A1';
  AExcelWorkSheet.Hyperlinks.Add(
    ARange,
    '',
    hyperlinkTargetAddress,
    'Screen Tip Text',
    'Hyperlink Title1'
  );

  ARange := AExcelWorkSheet.Range['A2','A2'];
  AExcelWorkSheet.Hyperlinks.Add(
    ARange,
    '',
    'Sheet1!A1',
    'Screen Tip Text',
    'Hyperlink Title2'
  );

end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文