2017-01-24 3 views
1

Ich habe Schwierigkeiten, Hyperlinks zu einer anderen Liste zu meiner generierten Excel-Datei hinzuzufügen. Ich habe versucht, es wie folgt aus:EPPlus Hyperlink zu Zelle in einem anderen Blatt

ws.Cells[1, 1].Formula="HYPERLINK(\"[#]'sheetName'!R4C1\";\"linktext\")" 
ws.Cells[1, 1].Formula="HYPERLINK(\"#'sheetName'!R4C1\";\"linktext\")" 
ws.Cells[1, 1].FormulaR1C1="HYPERLINK(\"[#]'sheetName'!R4C1\";\"linktext\")" 
ws.Cells[1, 1].FormulaR1C1="HYPERLINK(\"#'sheetName'!R4C1\";\"linktext\")" 

Nach dem Öffnen erzeugte Datei in Excel-365 (vollständige Offline-Anwendung) zeichnen sich i gerade Mitteilung erhalten, dass es Fehler in der Datei und alle Zellen, in denen ich diesen Hyperlink Formel verwendet werden, sind leer.

Die Fehlermeldung lautet:

we found a problem with some content in FILENAME do you want us to try to recover as much as we can 

Wie es funktioniert?

Auch wenn ich gleiche Formel in Zelle setzen manuell funktioniert es.

komplette Code:

using OfficeOpenXml; 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ExcelHyperlinkTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      MemoryStream excelStream = new MemoryStream(); 
      ExcelPackage excelFile = new ExcelPackage(excelStream); 

      ExcelWorksheet wsSrc = excelFile.Workbook.Worksheets.Add("src"); 
      ExcelWorksheet wsTgt = excelFile.Workbook.Worksheets.Add("tgt"); 

      wsSrc.Cells[1, 1].Formula = string.Format("HYPERLINK(\"#'{0}'!R{1}C{2}\";\"{3}\")", "tgt", 2, 5, "test"); //FormulaR1C1 

      excelFile.Save(); 
      excelStream.Position = 0; 

      using (FileStream file = new FileStream("c:\\linkTest.xlsx", FileMode.Create, System.IO.FileAccess.Write)) 
      { 
       byte[] bytes = new byte[excelStream.Length]; 
       excelStream.Read(bytes, 0, (int)excelStream.Length); 
       file.Write(bytes, 0, bytes.Length); 
       excelStream.Close(); 
      } 
     } 
    } 
} 

Antwort

3

Dies funktioniert

wsSrc.Cells[1, 6].Value = "test3"; 
      Uri url = new Uri("#'tgt'!B5", UriKind.Relative); 
      wsSrc.Cells[1, 6].Hyperlink = url; 
Verwandte Themen