如何使用c#添加超链接到excel形状?

时间:2017-02-13 07:41:10

标签: c# excel-2010

我正在尝试为形状添加超链接,但在执行时会出错。

  

HRESULT的异常:0x800A03EC

我创建了一个Shape,然后我为超链接分配了一个地址但没有成功。请在C#中运行。代码在没有链接的情况下工作。请参阅以下代码:

InteropExcel.Range r = (InteropExcel.Range)iWS.Cells[rownum, currCol];
InteropExcel.Shape sh = (InteropExcel.Shape)iWS.Shapes.AddShape(MsoAutoShapeType.msoShape4pointStar, Convert.ToSingle(r.Left), Convert.ToSingle(r.Top), 20, 20);

sh.Hyperlink.Address = "c:\\test.txt";

1 个答案:

答案 0 :(得分:0)

以下代码显示了如何添加指向excel形状的链接:

InteropExcel.Range rshx = (InteropExcel.Range)iWS.Cells[rownum, currCol];

InteropExcel.Shape shxr = (InteropExcel.Shape)iWS.Shapes
    .AddShape(MsoAutoShapeType.msoShapeDiamond, 
        Convert.ToSingle(rshx.Left), Convert.ToSingle(rshx.Top), 20, 20);

Release(rshx);

shxr.ShapeStyle = MsoShapeStyleIndex.msoShapeStylePreset31;

iWS.Hyperlinks.Add(shxr, linkurl);
Release(shxr);