poi 超链接 java Excel


package cn.com.jandar.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFHyperlink;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFHyperlink;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class Test {
	public static void main(String[] args) throws IOException {
		XSSFWorkbook workbook = new XSSFWorkbook();
		XSSFSheet sheet = workbook.createSheet("Hyper Links");
		XSSFCell cell;
		CreationHelper createHelper = workbook.getCreationHelper();
		XSSFCellStyle hlinkstyle = workbook.createCellStyle();
		XSSFFont hlinkfont = workbook.createFont();
		hlinkfont.setUnderline(XSSFFont.U_SINGLE);
		hlinkfont.setColor(HSSFColor.BLUE.index);
		hlinkstyle.setFont(hlinkfont);
		//URL Link
		cell = sheet.createRow(1).createCell((short)1);
		cell.setCellValue("URL Link");
		XSSFHyperlink  hyperLink = (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_URL);
		hyperLink.setAddress("http://www.yiibai.com");
		cell.setHyperlink(hyperLink);
		cell.setCellStyle(hlinkstyle);
		//Hyperlink to a file in the current directory
		cell = sheet.createRow(2).createCell(1);
		cell.setCellValue("File Link");
		hyperLink = (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_FILE);
		hyperLink.setAddress("cellstyle.xlsx");
		cell.setHyperlink(hyperLink);
		cell.setCellStyle(hlinkstyle);
		//e-mail link
		cell = sheet.createRow(3).createCell((short) 1);
		cell.setCellValue("Email Link");
		hyperLink = (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_EMAIL);
		hyperLink.setAddress("mailto:[email protected]?"
			      +"subject=Hyperlink");
		cell.setHyperlink(hyperLink);
		cell.setCellStyle(hlinkstyle);
		XSSFSheet sheet2 = workbook.createSheet("明细页面");  //建立新的sheet对象  
		//sheel
		cell = sheet.createRow(2).createCell(1);
		cell.setCellValue("明细");
		hyperLink = (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT);
		hyperLink.setAddress("#明细页面!A10");
		cell.setHyperlink(hyperLink);
		cell.setCellStyle(hlinkstyle);
        for (int i = 0; i < 30; i++) {  
            XSSFRow row2 = sheet2.createRow((short)i);  
            XSSFCell cell2 = row2.createCell((short)0);  
            cell2.setCellValue("测试第"+(i+1)+"行");  
        }  

		FileOutputStream fos = new FileOutputStream(new File("D:/hyperLink.xlsx"));
		workbook.write(fos);
		fos.close();
		System.out.println("hyperlink.xlsx written successfully");

	}
}

引用引用

你可能感兴趣的:(Excel)