poi 导出word 表格文件

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

 // 创建表格101列  
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("https://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);*/
    }       

        }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值