NPOI 2 AddHyperlinkInXlsx

using System;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.Util;
using System.IO;

namespace XSSF
{
	public class AddHyperlinkInXlsx
	{
		public static void DoAction()
		{
			IWorkbook workbook = new XSSFWorkbook ();

			//设置默认格式,蓝色,带下划线
			ICellStyle hlinkStyle = workbook.CreateCellStyle ();
			IFont hlinkFont = workbook.CreateFont ();
			hlinkFont.Underline = (byte)FontUnderlineType.SINGLE;
			hlinkFont.Color = HSSFColor.BLUE.index;
			hlinkStyle.SetFont (hlinkFont);

			ICell cell;
			ISheet sheet = workbook.CreateSheet ("Hyperlinks");

			//URL
			cell = sheet.CreateRow (0).CreateCell (0);
			cell.SetCellValue ("超链接");
			XSSFHyperlink link = new XSSFHyperlink (HyperlinkType.URL);
			link.Address = ("http://www.cmono.net");
			cell.Hyperlink = link;
			cell.CellStyle = (hlinkStyle);

			//Link to a file in the current directory
			cell = sheet.CreateRow (1).CreateCell (0);
			cell.SetCellValue ("连接当前目录中文件");
			link = new XSSFHyperlink (HyperlinkType.FILE);
			link.Address = "link.xlsx";
			cell.Hyperlink = link;
			cell.CellStyle = hlinkStyle;

			//e-mail link
			cell = sheet.CreateRow (2).CreateCell (0);
			cell.SetCellValue ("电子邮件");
			link = new XSSFHyperlink (HyperlinkType.EMAIL);
			//Note:如果地址包含空格,请确保已经进行了URL编码
			link.Address = "mailto:changweihua@outlook.com";
			cell.Hyperlink = link;
			cell.CellStyle = hlinkStyle;

			//link to a place in this workbook
			
			//Create a target sheet and cell
			ISheet sheet2 = workbook.CreateSheet("Target ISheet");
			sheet2.CreateRow (0).CreateCell (0);
			sheet2.CreateRow(1).CreateCell(0).SetCellValue("Target ICell");
			
			cell = sheet.CreateRow(3).CreateCell(0);
			cell.SetCellValue("Worksheet Link");
			link = new XSSFHyperlink(HyperlinkType.DOCUMENT);
			//地址格式:表名!单元格坐标,X用字母表示,Y用数字表示
			link.Address = ("'Target ISheet'!A2");
			cell.Hyperlink = (link);
			cell.CellStyle = (hlinkStyle);

			FileStream sw = File.Create("test.xlsx");
			workbook.Write(sw);
			sw.Close();

		}
	}
}

知识共享许可协议
《NPOI 2 AddHyperlinkInXlsx》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     159.38ms