您的位置:首页 > 其它

poi 导出word 表格文件

2017-02-15 18:56 597 查看
poi导出word文件,其实很简单,下面我为大家写了一个样例

//创建一个doc对象
XWPFDocument xdoc = new XWPFDocument();
//创建段落对象
XWPFParagraph p = xdoc.createParagraph();
//通过段落对象,创建字体对象。该对象设置字体大小,内容,颜色,是否加粗等。
XWPFRun pRun = p.createRun();
//设计标题
pRun.setText("位置信息");
pRun.setFontSize(11);
pRun.setBold(true);
//水平居中
p.setAlignment(ParagraphAlignment.LEFT);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
saveDocument(xdoc, "f:/sys_" + System.currentTimeMillis()+ ".doc");

// 创建表格10行1列
XWPFTable table = xdoc.createTable(10, 1);
//获取第一行对象(标题行)
XWPFTableRow row = table.getRow(0);
//获取改行某一列的对象,通过该对象设置每个表格的内容等信息
XWPFTableCell cell = row.getCell(0);
//通过cell获取对象,通过段落对象获取内容对象
if(cell.getParagraphs().size()>0){
p=cell.getParagraphs().get(0);
}else{
p=cell.addParagraph();
}
XWPFRun pRun=p.createRun();
pRun.setText(“图片”);
//垂直居中
cell.setVerticalAlignment(XWPFVertAlign.CENTER);
//水平居中
p.setAlignment(ParagraphAlignment.LEFT);

//后台数据
Map<String, List<DeviceOnlineData>> result=getLocationData("161d3703f1764f789197c627c9576782");
//开始写入数据行
for(int i=0;i<table.getRows().size();i++){
//第一行是标题行,跳过
if(i==0){
continue;
}

cell=table.getRow(i).getCell(0);
//设置列宽度
setCellWidth(cell,"2500");
//给文字添加超链接,\n 是表格内换行
appendExternalHyperlink("ht
4000
tps://www.baidu.com","百度"+"\n", cell);
}
}
//导出到F盘
saveDocument(xdoc, "f:/sys_" + System.currentTimeMillis()+ ".doc");
/**
* 设置超链接
*/
public void appendExternalHyperlink(String url, String text,XWPFTableCell cell) {
XWPFParagraph p=getCellFirstParagraph(cell);
String id = p.getDocument().getPackagePart().addExternalRelationship(url,XWPFRelation.HYPERLINK.getRelation()).getId();
CTHyperlink link = p.getCTP().addNewHyperlink();
link.setId(id);

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);

ctr.setTArray(new CTText[]{ctText});
link.setRArray(new CTR[]{ctr});

//垂直居中
cell.setVerticalAlignment(XWPFVertAlign.CENTER);
//水平居中
p.setAlignment(ParagraphAlignment.CENTER);

//cell.setText( "<a href='http://www.baidu.com'>"+text+"</a>");
/*
// Add the link as External relationship
String id = xdoc.getPackagePart().addExternalRelationship(url,XWPFRelation.HYPERLINK.getRelation()).getId();
// Append the link and bind it to the relationship
CTHyperlink cLink = xdoc.getLastParagraph().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);
ctr.setTArray(new CTText[] { ctText });
// Insert the linked text into the link
cLink.setRArray(new CTR[] { ctr });

setCellContext(cell,ctText);
*/
//设置字体
/*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"));
//设置段落居中
paragraph.setAlignment(ParagraphAlignment.CENTER);
paragraph.setVerticalAlignment(TextAlignment.CENTER);*/
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: