一、NPOI導出excel數據帶超鏈接_NPOI導出超鏈接
導出超鏈接分兩步
1.設置超鏈接樣式,藍色+下劃線
2.設定單元格Hyperlink 對象
//創建工作簿 HSSFWorkbook wk = new HSSFWorkbook(); //創建名稱爲mySheet的表 ISheet tb = wk.CreateSheet("mySheet"); IRow row = tb.CreateRow(0); ICell cell = row.CreateCell(0); //創建單元格,寫入數據 //富文本 HSSFRichTextString rich = new HSSFRichTextString("中文,測試內容"); IFont font2 = wk.CreateFont(); font2.Color = HSSFColor.OliveGreen.Blue.Index; rich.ApplyFont(0, 2, font2); cell.SetCellValue(rich); //定義超鏈接樣式 ICellStyle hlink_style = wk.CreateCellStyle(); IFont hlink_font = wk.CreateFont(); hlink_font.Underline = FontUnderlineType.Single; hlink_font.Color = HSSFColor.Blue.Index; hlink_style.SetFont(hlink_font); //超鏈接1 IRow row1 = tb.CreateRow(1); ICell cell1 = row1.CreateCell(0); cell1.SetCellValue("濟南小程序開發"); HSSFHyperlink link = new HSSFHyperlink(HyperlinkType.Url); link.Address = ("http://www.jnqianle.cn/"); cell1.Hyperlink = link; cell1.CellStyle = hlink_style; //超鏈接2 IRow row2 = tb.CreateRow(2); ICell cell2 = row2.CreateCell(0); cell2.SetCellValue("濟南網站開發"); HSSFHyperlink link2 = new HSSFHyperlink(HyperlinkType.Url); link2.Address = ("http://site.jnqianle.cn/"); cell2.Hyperlink = link2; cell2.CellStyle= hlink_style; //保存到文件 //打開一個xls文件,如果沒有自行創建 //如果存在則重新創建 using (FileStream fs = File.OpenWrite("linktest.xls")) { wk.Write(fs); Console.WriteLine("導出數據成功!"); }
導出效果:
更多:
XSSFClientAnchor 設置偏移無效 setDx setDy