2011-10-03 192 views
30

我想弄清楚如何使用EPPlus代替包含鏈接文本的單元格在單元格內編寫超鏈接。我需要它被識別爲鏈接並且可點擊。如何使用EPPlus在單元格內創建鏈接

任何幫助表示讚賞。

+0

你不能使用** = HYPERLINK()**公式嗎? –

+0

你試過** worksheet.Cells [i,j] .Hyperlink ** = new Uri(localfilepath)? –

+0

@TimWilliams由於數據是從數據集加載的,因此無法工作。 – IEnumerator

回答

18

下面的代碼工作的罰款和我在一起。

string FileRootPath = "http://www.google.com"; 
_Worksheet.Cells[intCellNumber, 1].Formula = "HYPERLINK(\"" + FileRootPath + "\",\"" + DisplayText + "\")"; 

我希望這會對你有所幫助。

快樂編碼!

+0

爲什麼'_Worksheet.Cells [intCellNumber,1] .Formula = String.Format(「HYPERLINK(\」{0} \「,\」{1} \「)」,FileRootPath,DisplayText)'? – MrOodles

+1

爲什麼不是'String.Format(@「HYPERLINK(」「{0}」「,」「{1}」「)」,FileRootPath,DisplayText)';) – Pakman

+2

對我來說,它似乎不是一個通用的解決方案,因爲HYPERLINK函數可能不適用於非英文版的MS Excel。從這個角度來看,韓的回答更好。 –

1

我不知道EPPlus,但在VBA(我猜C#將使用相同的原理),你可以使用下面的代碼:

Sub Test() 

    ' place value into cell 
    ActiveSheet.[A1] = 13 

    ' create link and set its range property 
    ActiveSheet.Hyperlinks.Add ActiveSheet.[A1], "http://www.google.at" 

    ' use cell in a calculation 
    ActiveSheet.[A2].Formula = "=A1+2" 

End Sub 

超鏈接都具有一定範圍的財產,因此,儘管對象的單元格的值可以通過重寫來改變,鏈接將保持不變。編輯由長鼠標單擊單元格

希望這有助於 - 好運拾音

+0

謝謝@MikeD。我正在使用EPPlus做這個服務器端。Emmanuel的回答正確地解決了我的問題。 – IEnumerator

73

這是其他的方式來做到:

var cell = sheet.Cells["A1"]; 
cell.Hyperlink = new Uri("http://www.google.com"); 
cell.Value = "Click me!"; 

我已經測試。它工作正常。

+2

Ahhhhhhhhhhhh比'\「」,「\」\「??」'+1感謝+1感謝! – ppumkin

+6

感謝您的支持!我必須在超鏈接後設置值,否則我會在單元格中看到地址,而不是數值。 – rpeshkov

+0

我有一個異常,如無法找到System._ComObject的超鏈接的定義。我添加了EPPlus參考,並且沒有編譯錯誤。即使我使用.NET 4.0版本。任何人都可以告訴我這個運行時異常的原因嗎? –

10

有幾種方法去做:

1)要使用URI,然後設置一個人類可讀的名稱

var cell = sheet.Cells["A1"]; 
cell.Hyperlink = new Uri("http://www.google.com"); 
cell.Value = "Click me!"; 

2)使用ExcelHyperlink和使用對象初始化設置人​​類可讀的名稱

var cell = sheet.Cells["A1"]; 
cell.Hyperlink = new ExcelHyperlink("http://www.google.com") { Display = "Click me! }; 

3)要使用超鏈接=()式

var cell = sheet.Cells["A1"]; 
cell.Formula = string.Format("HYPERLINK({0},{1})", "http://www.google.com", "Click me!"); 
cell.Calculate(); 
+0

我可以在哪裏參考ExcelHyperlink? – Muflix

+1

@Muflix添加對OfficeOpenXml的引用 – Cardin

+0

@Muflix新類應該是ExcelHyperLink而不是ExcelHyperlink。如果您添加對OfficeOpenXml的引用並修復類名稱上的套管,它將起作用。 – Baxter

1

基於提供的答案和文檔,我能夠創建一個擴展方法,同時處理適當的超鏈接格式。它創建了一個名爲風格,如果需要的話,並用於所有後續超鏈接樣式:

public static void WriteHyperlink(this ExcelRange cell, string text, string url, bool excelHyperlink = false, bool underline = true) 
{ 
    if (string.IsNullOrWhiteSpace(text)) 
     return; 

    // trying to reuse hyperlink style if defined 
    var workBook = cell.Worksheet.Workbook; 
    string actualStyleName = underline ? HyperLinkStyleName : HyperLinkNoUnderlineStyleName; 

    var hyperlinkStyle = workBook.Styles.NamedStyles.FirstOrDefault(s => s.Name == actualStyleName); 
    if (hyperlinkStyle == null) 
    { 
     var namedStyle = workBook.Styles.CreateNamedStyle(actualStyleName); 
     namedStyle.Style.Font.UnderLine = underline; 
     namedStyle.Style.Font.Color.SetColor(Color.Blue); 
    } 

    if (excelHyperlink) 
     cell.Hyperlink = new ExcelHyperLink(url) { Display = text }; 
    else 
    { 
     cell.Hyperlink = new Uri(url); 
     cell.Value = text; 
     cell.StyleName = actualStyleName; 
    } 
} 

沒有造型的超級鏈接看起來就像普通的文本,如果沒有明確的造型採用cell.Hyperlink = new Uri(url);(雖然光標正確表明該文本實際上是一個超鏈接文本)。

相關問題