最近在做一個招聘系統, 里面一個導出excel的功能, 要求導出簡歷,第一個Sheet頁是列表頁,列表頁的第一列是一個超鏈接, 可以跳到簡歷的詳情頁。如下圖:

在這里我主要講HSSFHyperlink函數, 因為這個用的比較少, 如果想了解導出的一些功能,可以看:C# NPOI 導入與導出Excel文檔 兼容xlsx, xls
HSSFHyperlink函數的用法如下:
HSSFHyperlink link = new HSSFHyperlink(HyperlinkType.Document); link.Address = "#" + sheet頁的名字 + "!A1";//設置超鏈接點擊跳轉的地址
Demo下載
下載地址:Excel-text.rar
using NPOI.HSSF.UserModel; using NPOI.HSSF.Util; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Demo { class Program { static void Main(string[] args) { //建立空白工作簿 HSSFWorkbook book = new HSSFWorkbook(); //在工作簿中:建立空白工作表 ISheet sheet = book.CreateSheet("簡歷信息列表"); #region Excel樣式 IFont font = book.CreateFont(); font.Boldweight = short.MaxValue; font.FontHeightInPoints = 11; font.FontName = "宋體"; IFont font2 = book.CreateFont(); font2.FontName = "宋體"; IFont fontLink = book.CreateFont(); fontLink.Color = HSSFColor.Blue.Index; fontLink.FontName = "宋體"; //設置鏈接的樣式:水平垂直對齊居中 ICellStyle cellLinkStyle = book.CreateCellStyle(); cellLinkStyle.Alignment = HorizontalAlignment.Center; cellLinkStyle.VerticalAlignment = VerticalAlignment.Center; //cellLinkStyle.BorderBottom = CellBorderType.THIN; //cellLinkStyle.BorderLeft = CellBorderType.THIN; //cellLinkStyle.BorderRight = CellBorderType.THIN; //cellLinkStyle.BorderTop = CellBorderType.THIN; cellLinkStyle.BottomBorderColor = HSSFColor.Black.Index; cellLinkStyle.LeftBorderColor = HSSFColor.Black.Index; cellLinkStyle.RightBorderColor = HSSFColor.Black.Index; cellLinkStyle.TopBorderColor = HSSFColor.Black.Index; cellLinkStyle.SetFont(fontLink); //設置表頭的樣式:水平垂直對齊居中,加粗 ICellStyle titleCellStyle = book.CreateCellStyle(); titleCellStyle.Alignment = HorizontalAlignment.Center; titleCellStyle.VerticalAlignment = VerticalAlignment.Center; titleCellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index; //圖案顏色 //titleCellStyle.FillPattern = FillPatternType.SPARSE_DOTS; //圖案樣式 titleCellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index; //背景顏色 #endregion //創建表頭 IRow row = sheet.CreateRow(0); row.HeightInPoints = 25; row.CreateCell(0).SetCellValue("查看簡歷"); row.CreateCell(1).SetCellValue("應聘職位"); row.CreateCell(2).SetCellValue("狀態"); row.CreateCell(3).SetCellValue("姓名"); row.CreateCell(4).SetCellValue("年齡"); row.CreateCell(5).SetCellValue("郵箱"); row.CreateCell(6).SetCellValue("地址"); //獲取測試數據 DataTable dt = new Program().GetData(); for (int i = 0; i < dt.Rows.Count; i++) { IRow row1 = sheet.CreateRow(i + 1); row.HeightInPoints = 18; //設置字體大小 HSSFHyperlink link = new HSSFHyperlink(HyperlinkType.Document); link.Address = "#" + dt.Rows[i]["name"].ToString() + (i + 1) + "!A1";//設置超鏈接點擊跳轉的地址 row1.CreateCell(0).Hyperlink = link; row1.GetCell(0).SetCellValue("查看簡歷詳細"); row1.CreateCell(1).SetCellValue(dt.Rows[i]["postion"].ToString()); row1.CreateCell(2).SetCellValue(dt.Rows[i]["state"].ToString()); row1.CreateCell(3).SetCellValue(dt.Rows[i]["name"].ToString()); row1.CreateCell(4).SetCellValue(dt.Rows[i]["age"].ToString()); row1.CreateCell(5).SetCellValue(dt.Rows[i]["email"].ToString()); row1.CreateCell(6).SetCellValue(dt.Rows[i]["address"].ToString()); //設置超鏈接的顏色 row1.GetCell(0).CellStyle = cellLinkStyle; new Program().CreateDetailPage(book, dt.Rows[i], Convert.ToInt32(i) + 1); } //導出 System.IO.MemoryStream ms = new System.IO.MemoryStream(); book.Write(ms); FileStream file = new FileStream("Demo.xls", FileMode.OpenOrCreate); book.Write(file); file.Flush(); file.Close(); book = null; } public void CreateDetailPage(HSSFWorkbook book, DataRow row, int number) { ISheet sheet = book.CreateSheet(row["name"].ToString() + number); IRow row1 = sheet.CreateRow(0); row1.CreateCell(0).SetCellValue("簡歷詳細信息"); } /// <summary> /// 取數據, 方便測試, 數據寫死 /// </summary> /// <returns></returns> public DataTable GetData() { DataTable dt = new DataTable(); //創建列(第一個參數是列名稱,第二個參數是列的類型) dt.Columns.Add("postion", Type.GetType("System.String"));//職位 dt.Columns.Add("state", Type.GetType("System.String")); //狀態 dt.Columns.Add("name", Type.GetType("System.String")); //姓名 dt.Columns.Add("age", Type.GetType("System.String")); //年齡 dt.Columns.Add("email", Type.GetType("System.String")); //郵箱 dt.Columns.Add("address", Type.GetType("System.String")); //地址 //填充數據 //創建一行數據 for (int i = 0; i < 5; i++) { DataRow dr = dt.NewRow(); dr["postion"] = "店鋪兼職員工(全國范圍)"; dr["state"] = "二面"; dr["name"] = "張三" + i; dr["age"] = 22 + i; dr["email"] = "123456@qq.com"; dr["address"] = "上海市 普陀區 111號"; //數據填充到表格 dt.Rows.Add(dr); } return dt; } } }
摘自白馬驛站:https://www.51baidu.com.cn/Home/Detail/2531
簡介白碼驛站是一個面向開發者的知識共享社區,專注於為開發者打造一個純凈的技術交流社區(源碼下載,免費在線圖片壓縮,jquery插件,插件下載,JS/CSS在線壓縮。)-白碼驛站
