NPOI 1.2.3教程 -27 超级链接Hyperlink
2011-01-23 16:51
477 查看
using System; using System.Text; using System.IO; using NPOI.HSSF.UserModel; using NPOI.HPSF; using NPOI.POIFS.FileSystem; using NPOI.HSSF.Util; using NPOI.SS.UserModel; namespace AddHyperlinkInXls { class Program { static void Main(string[] args) { InitializeWorkbook(); ////cell style for hyperlinks ////by default hyperlinks are blue and underlined CellStyle hlink_style = hssfworkbook.CreateCellStyle(); Font hlink_font = hssfworkbook.CreateFont(); hlink_font.Underline = (byte)FontUnderlineType.SINGLE; hlink_font.Color = HSSFColor.BLUE.index; hlink_style.SetFont(hlink_font); Cell cell; Sheet sheet = hssfworkbook.CreateSheet("Hyperlinks"); //URL cell = sheet.CreateRow(0).CreateCell(0); cell.SetCellValue("URL Link"); HSSFHyperlink link = new HSSFHyperlink(HyperlinkType.URL); link.Address = ("http://poi.apache.org/"); cell.Hyperlink = (link); cell.CellStyle = (hlink_style); //link to a file in the current directory cell = sheet.CreateRow(1).CreateCell(0); cell.SetCellValue("File Link"); link = new HSSFHyperlink(HyperlinkType.FILE); link.Address = ("link1.xls"); cell.Hyperlink = (link); cell.CellStyle = (hlink_style); //e-mail link cell = sheet.CreateRow(2).CreateCell(0); cell.SetCellValue("Email Link"); link = new HSSFHyperlink(HyperlinkType.EMAIL); //note, if subject contains white spaces, make sure they are url-encoded link.Address = ("mailto:poi@apache.org?subject=Hyperlinks"); cell.Hyperlink = (link); cell.CellStyle = (hlink_style); //link to a place in this workbook //Create a target sheet and cell Sheet sheet2 = hssfworkbook.CreateSheet("Target Sheet"); sheet2.CreateRow(0).CreateCell(0).SetCellValue("Target Cell"); cell = sheet.CreateRow(3).CreateCell(0); cell.SetCellValue("Worksheet Link"); link = new HSSFHyperlink(HyperlinkType.DOCUMENT); link.Address = ("'Target Sheet'!A1"); cell.Hyperlink = (link); cell.CellStyle = (hlink_style); WriteToFile(); } static HSSFWorkbook hssfworkbook; static void WriteToFile() { //Write the stream data of workbook to the root directory FileStream file = new FileStream(@"test.xls", FileMode.Create); hssfworkbook.Write(file); file.Close(); } static void InitializeWorkbook() { hssfworkbook = new HSSFWorkbook(); //Create a entry of DocumentSummaryInformation DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation(); dsi.Company = "NPOI Team"; hssfworkbook.DocumentSummaryInformation = dsi; //Create a entry of SummaryInformation SummaryInformation si = PropertySetFactory.CreateSummaryInformation(); si.Subject = "NPOI SDK Example"; hssfworkbook.SummaryInformation = si; } } }
相关文章推荐
- NPOI 1.2.3教程 -18 分组Group
- NPOI 1.2.3教程 -19 读取模板Template
- NPOI 1.2.3教程 -20 填充背景FillBackground
- NPOI 1.2.3教程 -21 下载Download
- NPOI 1.2.3教程 -7 单元格高度和宽度HeightAndWidth
- NPOI 1.2.3教程 -22 画图Drawing
- NPOI 1.2.3教程 -8 打印Print
- NPOI 1.2.3教程 -23 页眉页脚HeaderFooter
- NPOI 1.2.3教程 -9 格式化Format
- NPOI 1.2.3教程 -24 下拉框DropDownList
- NPOI 1.2.3教程 - 1 认识NPOI
- NPOI 1.2.3教程 -10批注Comment
- NPOI 1.2.3教程 -25 条件样式ConditionalFormat
- NPOI 1.2.3教程 - 2 使用NPOI生成xls文件
- NPOI 1.2.3教程 -11 边框样式BorderStyle
- NPOI 1.2.3教程 -26 字体Font
- NPOI 1.2.3教程 - 3 縮放Zoom
- NPOI 1.2.3教程 -12 对齐Alignment
- NPOI 1.2.3教程 - 4 基本公式 BasicFormula
- NPOI 1.2.3教程 -13 活动单元格ActiveCell