预报导出--添加超链接
This commit is contained in:
+72
-2
@@ -21,20 +21,26 @@ import org.apache.poi.xwpf.usermodel.XWPFHeader;
|
|||||||
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
||||||
import org.apache.poi.xwpf.usermodel.XWPFPicture;
|
import org.apache.poi.xwpf.usermodel.XWPFPicture;
|
||||||
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
|
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
|
||||||
|
import org.apache.poi.xwpf.usermodel.XWPFRelation;
|
||||||
import org.apache.poi.xwpf.usermodel.XWPFRun;
|
import org.apache.poi.xwpf.usermodel.XWPFRun;
|
||||||
import org.apache.poi.xwpf.usermodel.XWPFStyle;
|
import org.apache.poi.xwpf.usermodel.XWPFStyle;
|
||||||
import org.apache.poi.xwpf.usermodel.XWPFStyles;
|
import org.apache.poi.xwpf.usermodel.XWPFStyles;
|
||||||
import org.apache.poi.xwpf.usermodel.XWPFTable;
|
import org.apache.poi.xwpf.usermodel.XWPFTable;
|
||||||
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
|
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
|
||||||
import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute;
|
import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute;
|
||||||
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTColor;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFldChar;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFldChar;
|
||||||
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
|
||||||
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageSz;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageSz;
|
||||||
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
|
||||||
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle;
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle;
|
||||||
@@ -935,7 +941,6 @@ public class WordUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void setCell(XWPFTable table, int number, String fieldName,String technologyTerritory) {
|
private static void setCell(XWPFTable table, int number, String fieldName,String technologyTerritory) {
|
||||||
|
|
||||||
table.getRow(number).setHeight(500);
|
table.getRow(number).setHeight(500);
|
||||||
CTTblWidth ctTblWidth0 = table.getRow(number).getCell(0).getCTTc().addNewTcPr().addNewTcW();
|
CTTblWidth ctTblWidth0 = table.getRow(number).getCell(0).getCTTc().addNewTcPr().addNewTcW();
|
||||||
ctTblWidth0.setType(STTblWidth.DXA);
|
ctTblWidth0.setType(STTblWidth.DXA);
|
||||||
@@ -948,7 +953,14 @@ public class WordUtil {
|
|||||||
ctTblWidth1.setW(BigInteger.valueOf(6015));
|
ctTblWidth1.setW(BigInteger.valueOf(6015));
|
||||||
//设置单元格边距
|
//设置单元格边距
|
||||||
XWPFTableCell cell1 = table.getRow(number).getCell(1);
|
XWPFTableCell cell1 = table.getRow(number).getCell(1);
|
||||||
cellPadding(cell1,technologyTerritory);
|
if("链接".equals(fieldName) || "Link".equals(fieldName)){
|
||||||
|
//设置超链接
|
||||||
|
if(StringUtils.isNotEmpty(technologyTerritory)){
|
||||||
|
cellLink(cell1,technologyTerritory);
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
cellPadding(cell1,technologyTerritory);
|
||||||
|
}
|
||||||
|
|
||||||
// XWPFParagraph para = table.getRow(number).getCell(1).getParagraphs().get(0);
|
// XWPFParagraph para = table.getRow(number).getCell(1).getParagraphs().get(0);
|
||||||
// XWPFRun run = para.createRun();//对某个段落设置格式
|
// XWPFRun run = para.createRun();//对某个段落设置格式
|
||||||
@@ -1004,5 +1016,63 @@ public class WordUtil {
|
|||||||
run.setFontFamily("Blue Sky Noto Regular");
|
run.setFontFamily("Blue Sky Noto Regular");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置页边距,字体样式,超链接
|
||||||
|
* @param cell
|
||||||
|
* @param text
|
||||||
|
*/
|
||||||
|
public static void cellLink(XWPFTableCell cell,String text){
|
||||||
|
//1. 单元格页边距
|
||||||
|
CTTc ctTc = cell.getCTTc();
|
||||||
|
CTTcPr cttcpr =ctTc.isSetTcPr() ? ctTc.getTcPr() : ctTc.addNewTcPr();
|
||||||
|
CTTcMar ctTcMar = cttcpr.isSetTcMar() ? cttcpr.getTcMar() : cttcpr.addNewTcMar();
|
||||||
|
(ctTcMar.isSetLeft() ? ctTcMar.getLeft() : ctTcMar.addNewLeft()).setW(BigInteger.valueOf(119));//左边距
|
||||||
|
(ctTcMar.isSetTop() ? ctTcMar.getTop() : ctTcMar.addNewTop()).setW(BigInteger.valueOf(63));//上边距
|
||||||
|
// (ctTcMar.isSetRight() ? ctTcMar.getRight() : ctTcMar.addNewRight()).setW(BigInteger.valueOf(100));
|
||||||
|
// (ctTcMar.isSetBottom() ? ctTcMar.getBottom() : ctTcMar.addNewBottom()).setW(BigInteger.valueOf(100));
|
||||||
|
|
||||||
|
//2. 设置文本及文本的字体
|
||||||
|
CTP ctP = (ctTc.sizeOfPArray() == 0) ?
|
||||||
|
ctTc.addNewP() : ctTc.getPArray(0);
|
||||||
|
XWPFParagraph paragraph = cell.getParagraph(ctP);
|
||||||
|
|
||||||
|
|
||||||
|
//3. 单元格内容超链接(只有带http的链接才能跳转,纯文本无法跳转)
|
||||||
|
String id = paragraph
|
||||||
|
.getDocument()
|
||||||
|
.getPackagePart()
|
||||||
|
.addExternalRelationship(text,
|
||||||
|
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);
|
||||||
|
// 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("Blue Sky Noto Regular");
|
||||||
|
fonts.setEastAsia("Blue Sky Noto Regular");
|
||||||
|
fonts.setHAnsi("Blue Sky Noto Regular");
|
||||||
|
|
||||||
|
|
||||||
|
ctr.setTArray(new CTText[] { ctText });
|
||||||
|
// Insert the linked text into the link
|
||||||
|
cLink.setRArray(new CTR[] { ctr });
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user