1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 安卓生成word文档(使用了Spire.Doc.Android)

安卓生成word文档(使用了Spire.Doc.Android)

时间:2021-05-15 02:18:13

相关推荐

安卓生成word文档(使用了Spire.Doc.Android)

最近产品提了一个新需求,让移动端生成word文件,不是那种表格型的,正常排版的数据,翻来翻去都没有找到合适的,最后发现了一个成熟的三方spire 跟大家分享一下,使用了免费版,有瑕疵,特此记录一下。 后端同事推荐还有一种poi也可以实现,这里只做简单的分享,大家可以自行去查阅poi相关的东西,都不难。

效果:

首先:导入jar包,链接:/s/1tssXG67Etp1HfUTLNCE1Jg

提取码:1024

然后直接上代码:

String namePaths = "";//生成的word文件路径

btn_output_word.setOnClickListener(new View.OnClickListener() {//导出word@Overridepublic void onClick(View v) {//因为导出word中有图片,时间会稍微较长,为了体验加一个加载的loading,你们也可以忽略dialog1 = new LoadProgressDialog(MainActivity.this, "正在导出word...");dialog1.show();//需要文件读写权限if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED) {//数据源final List<ProblemTitleBean> list = new ArrayList<>();list.clear();list.addAll(beanList0);list.addAll(beanList1);list.addAll(beanList2);list.addAll(beanList3);if (list.size() < 1) {return;}//开启一个线程去做生成操作new Thread(new Runnable() {@Overridepublic void run() {// district_name:是我的word文件名开头namePaths = outWordPaths(district_name, list);}}).start();}}});

//关键方法

private String outWordPaths(String distline_name, List<ProblemTitleBean> list) {//创建Word文档Document document = new Document();//添加一个sectionSection section = document.addSection();//添加三个段落至sectionParagraph para1 = section.addParagraph();para1.appendText(distline_name + "台区现场排查异常问题报告");//标题for (int i = 0; i < list.size(); i++) {Paragraph para2 = section.addParagraph();para2.appendText((i + 1) + " 设备名称:" + list.get(i).getProblem_name());Paragraph para3 = section.addParagraph();para3.appendText("问题描述:" + list.get(i).getProblem_text());//添加第二个段落Paragraph paragraph4 = section.addParagraph();//添加图片到段落(我这个是本地图片路径)if (list.get(i).getProblem_pic_path()!=null && list.get(i).getProblem_pic_path()!="") {//添加图片的方法DocPicture picture = paragraph4.appendPicture(list.get(i).getProblem_pic_path());//设置图片宽度picture.setWidth(200f);//设置图片高度picture.setHeight(150f);}//设置其余两个段落的格式,因为没用到给注释了// ParagraphStyle style2 = new ParagraphStyle(document);// style2.setName("paraStyle");// style2.getCharacterFormat().setFontName("宋体");// style2.getCharacterFormat().setFontSize(11f);// document.getStyles().add(style2);// para2.applyStyle("paraStyle");// para3.applyStyle("paraStyle");//设置第二段和第三段的段首缩进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 + "/dfsoft/hippo/word");if (!file.exists()) {file.mkdirs();}String fileName = "";fileName = distline_name + "台区现场排查异常问题报告"+ ".docx";//文档格式docxString fileWordPath = file.getPath() + "/" + fileName;document.saveToFile(fileWordPath, FileFormat.Docx);mHandler.sendEmptyMessage(1);//这里是我开的线程,用来关闭dialog,不用的可以忽略return fileWordPath;}

最后 你就可以在创建的文件夹看到生成的文件;

我们需求是直接分享出去,

if (namePaths != null && !"".equals(namePaths)) {Intent email = tool.email(namePaths);startActivity(Intent.createChooser(email, "请选择邮件发送软件!"));} else {Toast.makeText(MainActivity.this, "发送失败!", Toast.LENGTH_SHORT).show();}

仅供大家参考,ps:因为是免费版所以生成的word首行会有个他们的水印,参考了两种方案,第一种就是自己手动删除(当然这太lowL);第二种我看网上说可以使用别的word操作框架去操作删除参考poi,不过这样会导致项目很大,感觉暂时没有好的处理方式,希望大家有好方法的可以知会我,一起分享进步~

完结,撒花

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。