我无法在excel模板中建立超链接。我想在像这样的http://www.diamondschool/ {personName}
单元格中显示值我已阅读github上的官方文档。根本没有运气
public FileInfo GenerateTemplate(long shoolId)
{
using (var excelPackage = new ExcelPackage(dataTemplate))
{
excelPackage.Workbook.Worksheets.Add("Sheet1");
var ws = excelPackage.Workbook.Worksheets[1];
var dataTable = InitializeDataTable();
var templateData = GetTemplateData(schoolId);
PopulateData(templateData, dataTable);
ws.Cells["A2"].LoadFromDataTable(dataTable, true);
}
///因为我已经从数据表中加载了数据,所以找不到将值显示为超链接的方法。 // http://www.diamondschool/ {personName},因此根据单元格中的值,将角色显示为指向该地址的超链接
//Person name as HyperLink
var namedStyle = ws.Workbook.Styles.CreateNamedStyle("HyperLink");
namedStyle.Style.Font.UnderLine = true;
namedStyle.Style.Font.Color.SetColor(Color.Blue);
ws.Cells["A2"].Value
ws.Cells["A2"].StyleName = "HyperLink";
excelPackage.Save();
}
return dataTemplate;
}
private static DataTable InitializeDataTable()
{
var dataTable = new DataTable();
dataTable.Columns.Add("Person Id", typeof(string));
dataTable.Columns.Add("Person Name", typeof(string));
return dataTable;
}
private static void PopulateData(IEnumerable<DataTemplateRow> data, DataTable dataTable)
{
foreach (var item in data)
{
var dataRow = dataTable.NewRow();
dataRow["Person Id"] = item.Id;
dataRow["Person Name"] = item.Name;
dataTable.Rows.Add(dataRow);
}
}
public IEnumerable<DataTemplateRow> GetTemplateData(long schoolId)
{
var personData = _schoolService.GetData(schoolId);
var result = personData.Data.Select(result => new DataTemplateRow
{
PersonId = result.Id,
PersonName = result.Name
});
return result;
}
public class DataTemplateRow
{
public long Id { get; set; }
public class Name { get; set; }
}
由于我已经从数据表中加载了数据,所以找不到将值显示为超链接的方法。 http://www.diamondschool/ {personName},因此请根据单元格中的值,将角色显示为指向该地址的超链接
答案 0 :(得分:0)
您需要像这样格式化单元格。创建一个Hyperlink
并设置一个Uri
ws.Cells[5, 5].Style.Font.UnderLine = true;
ws.Cells[5, 5].Style.Font.Bold = true;
ws.Cells[5, 5].Style.Font.Color.SetColor(Color.Blue);
ws.Cells[5, 5].Hyperlink = new Uri("https://www.google.nl");