Android基于Spire.Doc.Android生成word

最近客户提了新功能,在移动端生成word报告,最后找到了一个成熟的三方spire ,免费版有瑕疵。 同时poi也可以实现,这里只做简单的分享,大家可以自行去查阅poi相关的东西。
 

1.首先:导入jar包https://repo.e-iceblue.cn/repository/maven-public/e-iceblue/spire.doc.android/10.5.0/spire.doc.android-10.5.0.jarhttps://repo.e-iceblue.cn/repository/maven-public/e-iceblue/spire.doc.android/10.5.0/spire.doc.android-10.5.0.jar2.代码实现

public String saveWord(String distline_name) {
    //创建Word文档
    Document document = new Document();
    //添加一个section
    Section section = document.addSection();
    //添加三个段落至section
    Paragraph para1 = section.addParagraph();

    para1.appendText(distline_name + "报告");//标题

    Paragraph para2 = section.addParagraph();
    para2.appendText(" 设备名称:" + "服务器");
    Paragraph para3 = section.addParagraph();
    para3.appendText("问题描述:" + "设备异常无法使用");
    //添加第二个段落
    Paragraph paragraph4 = section.addParagraph();
    //添加图片到段落(我这个是本地图片路径)

    //添加图片的方法
    DocPicture picture = paragraph4.appendPicture("/sdcard/test.png");//替换为自己本地图片的路径 
    //设置图片宽度
    picture.setWidth(200f);
    //设置图片高度
    picture.setHeight(150f);

    //设置第二段和第三段的段首缩进
    para2.getFormat().setFirstLineIndent(25f);
    para3.getFormat().setFirstLineIndent(25f);
    paragraph4.getFormat().setFirstLineIndent(25f);

    //设置第一段和第二段的段后间距
    para2.getFormat().setAfterSpacing(10f);
    para3.getFormat().setAfterSpacing(10f);
    paragraph4.getFormat().setAfterSpacing(10f);
 
    para1.getFormat().setAfterSpacing(15f);
    //将第一段作为标题,设置标题格式
    ParagraphStyle style1 = new ParagraphStyle(document);
    style1.setName("titleStyle");
    style1.getCharacterFormat().setBold(true);
    style1.getCharacterFormat().setTextColor(Color.BLUE);
    style1.getCharacterFormat().setFontName("宋体");
    style1.getCharacterFormat().setFontSize(12f);
    document.getStyles().add(style1);
    para1.applyStyle("titleStyle");

    //设置第一个段落的对齐方式
    para1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

    //保存文档
    String path = Environment.getExternalStorageDirectory().getPath();
    //换成自己路径,输出路径
    File file = new File(path + "/word");
    if (!file.exists()) {
        file.mkdirs();
    }
    String fileName = "";
    fileName = distline_name + "报告"+ ".docx";//文档格式docx

    String fileWordPath = file.getPath() + "/" + fileName;
    document.saveToFile(fileWordPath, FileFormat.Docx);

    return fileWordPath;
}

3.生成效果图

缺点是免费版所以生成的word首行会有个他们的水印。

同时使用POI没有水印问题

Android基于Poi生成Wordicon-default.png?t=MBR7https://blog.csdn.net/Hearbeat/article/details/128660067

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Leonban

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值