`
53873039oycg
  • 浏览: 821160 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

[简单]poi word2007插入超链接

    博客分类:
  • poi
 
阅读更多

        代码参考了http://stackoverflow.com/questions/7007810/how-to-create-a-email-link-in-poi-word-format,代码如下:

       

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.util.Iterator;

import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.TextAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFHyperlink;
import org.apache.poi.xwpf.usermodel.XWPFHyperlinkRun;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRelation;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTColor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHpsMeasure;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline;

public class POI_插入超链接_S3_Test {
	public static void main(String[] args) throws Exception {
		POI_插入超链接_S3_Test t = new POI_插入超链接_S3_Test();
		t.example("f:/saveFile/temp/hylink_result.docx");
	}

	public void example(String savePath) throws Exception {
		XWPFDocument document = new XWPFDocument();
		// Append a link to
		appendExternalHyperlink("mailto:1329186624@qq.com?subject=测试poi超链接",
				" 测试超链接HyperLink", document.createParagraph());
		document.write(new FileOutputStream(savePath));
	}

	/**
	 * Appends an external hyperlink to the paragraph.
	 *@see  详细代码见http://stackoverflow.com/questions/7007810/how-to-create-a-email-link-in-poi-word-format            
	 */
	public static void appendExternalHyperlink(String url, String text,
			XWPFParagraph paragraph) {
		// Add the link as External relationship
		String id = paragraph
				.getDocument()
				.getPackagePart()
				.addExternalRelationship(url,
						XWPFRelation.HYPERLINK.getRelation()).getId();
		// Append the link and bind it to the relationship
		CTHyperlink cLink = paragraph.getCTP().addNewHyperlink();
		cLink.setId(id);
		
		// Create the linked text
		CTText ctText = CTText.Factory.newInstance();
		ctText.setStringValue(text);
		CTR ctr = CTR.Factory.newInstance();
		CTRPr rpr = ctr.addNewRPr();
		
		//设置超链接样式
		CTColor color = CTColor.Factory.newInstance();
		color.setVal("0000FF");
		rpr.setColor(color);
		rpr.addNewU().setVal(STUnderline.SINGLE);
		
		//设置字体
		CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts();
		fonts.setAscii("微软雅黑");
		fonts.setEastAsia("微软雅黑");
		fonts.setHAnsi("微软雅黑");

		//设置字体大小
		CTHpsMeasure sz = rpr.isSetSz() ? rpr.getSz() : rpr.addNewSz();
		sz.setVal(new BigInteger("24"));

		ctr.setTArray(new CTText[] { ctText });
		// Insert the linked text into the link
		cLink.setRArray(new CTR[] { ctr });
		
		//设置段落居中
		paragraph.setAlignment(ParagraphAlignment.CENTER);
		paragraph.setVerticalAlignment(TextAlignment.CENTER);
	}
}

   结果如下

   

 

   全文完

 

  • 大小: 18.8 KB
1
0
分享到:
评论
2 楼 xingxinglaile 2017-05-17  
链接到本文档的某个书签,请问如何操作?谢谢~
1 楼 bm_dtw 2016-07-27  
超链接到本文档位置怎么操作???

相关推荐

Global site tag (gtag.js) - Google Analytics