目的:
1.通过方法designer.Workbook.Worksheets[0].Hyperlinks.Add("A1", 1, 1, url);给导出到excel里面的数据加上超链接
2.使用mvc进行测试
3.给A1,A2两个单元格加上超链接
4.给超链接加上超链接样式
5.添加超链接有2个方法
public int Add(string cellName, int rowNumber, int columnNumber, string address);
public int Add(int firstRow, int firstColumn, int rowNumber, int columnNumber, string address);
具体实现核心方法:
designer.Workbook.Worksheets[0].Cells[0, 0].PutValue("超链接1");
designer.Workbook.Worksheets[0].Cells[1, 0].PutValue("超链接2");
designer.Workbook.Worksheets[0].Hyperlinks.Add("A1", 1, 1, url);
designer.Workbook.Worksheets[0].Cells[0, 0].SetStyle(LinkStyle(designer.Workbook));
designer.Workbook.Worksheets[0].Hyperlinks.Add(1, 0, 1, 1, url);
designer.Workbook.Worksheets[0].Cells[1, 0].SetStyle(LinkStyle(designer.Workbook));
效果图
入口方法
public ActionResult chaolianjie()
{
string url = "http://www.baidu.com/";
WorkbookDesigner designer = new WorkbookDesigner();
string path = Server.MapPath("/Templete/DDD.xls");
designer.Workbook.Open(path);
//添加超链接
designer.Workbook.Worksheets[0].Cells[0, 0].PutValue("超链接1");
designer.Workbook.Worksheets[0].Cells[1, 0].PutValue("超链接2");
designer.Workbook.Worksheets[0].Hyperlinks.Add("A1", 1, 1, url);
designer.Workbook.Worksheets[0].Cells[0, 0].SetStyle(LinkStyle(designer.Workbook)); designer.Workbook.Worksheets[0].Hyperlinks.Add(1, 0, 1, 1, url);
designer.Workbook.Worksheets[0].Cells[1, 0].SetStyle(LinkStyle(designer.Workbook));
designer.Process(); //将流文件写到客户端流的形式写到客户端,名称是_report.xls
designer.Save("_report.xls", SaveType.OpenInExcel, FileFormatType.Excel2003, System.Web.HttpContext.Current.Response);
Response.Flush();
Response.Close();
designer = null;
// Response.End();
return View("getexcel");
}