前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >导出word,Aspose word, java

导出word,Aspose word, java

原创
作者头像
学以致用丶
修改2022-06-27 11:14:07
2.6K0
修改2022-06-27 11:14:07
举报
文章被收录于专栏:学习记录的专栏

Maven引用

代码语言:html
复制
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-words</artifactId>
    <version>15.8.0</version>
</dependency>

数据填充,模板固定位置

代码语言:javascript
复制
 //固定位置数据
  String[] name = new String[]{
        "DateTime",
        "Name",
        "Sex",
         ...
  };
  String[] value = new String[]{
           "2021 年 05 月 13 号",
          "姓名",
          "性别",
          ...
   };
   //填充至模板
   merge.execute(name, value);

移动到书签位置

代码语言:javascript
复制
builder.moveToBookmark("picture");

指定位置插入图片

代码语言:javascript
复制
//添加图片
String imagePath="C:\\Users\\Administrator\\Desktop\\timg.jpg";
FileInputStream fis = new FileInputStream(imagePath);
byte[] image = new byte[fis.available()];
fis.read(image);
fis.close();
//移动到书签位置,在指定位置添加图片
builder.moveToBookmark("picture");
//设置格式  WrapType.INLINE 参考:https://www.cnblogs.com/zhmlxx/p/14547931.html
builder.insertImage(image, RelativeHorizontalPosition.DEFAULT, 1, RelativeVerticalPosition.MARGIN, 1, 100, 120, WrapType.INLINE);

添加目录(在某一级目录下添加目录)

代码语言:javascript
复制
//移动书签位置添加目录
builder.moveToBookmark("xxxxx");
//创建二级目录(当前是在一级目录下添加的二级目录)
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);

代码

代码语言:javascript
复制
    @Override
    public void dataFillTest(HttpServletRequest request, ) throws UnsupportedEncodingException {
        OutputStream outputStream = null;
        //模板路径
        String templatePath="C:\\Users\\Administrator\\Desktop\\xxxxx.docx";
        //导出路径(指定路径)
        String DownloadPath="C:\\Users\\Administrator\\Desktop\\";
        Document doc = null;
        try {
            doc = new Document(templatePath);
            MailMerge merge = doc.getMailMerge();
            
            DocumentBuilder builder = new DocumentBuilder(doc);
            //固定位置数据
            String[] name = new String[]{
                    "DateTime",
                    "Name",
                    "Sex",
                    ...
            };
             String[] value = new String[]{
                    "2021 年 05 月 13 号",
                    "姓名",
                    "性别",
                    ...
            };
            
            //填充至模板
            merge.execute(name, value);
            
            //添加图片
            String imagePath="C:\\Users\\Administrator\\Desktop\\timg.jpg";
            FileInputStream fis = new FileInputStream(imagePath);
            byte[] image = new byte[fis.available()];
            fis.read(image);
            fis.close();
            //移动到书签位置,在指定位置添加图片
            builder.moveToBookmark("picture");
            //设置格式  WrapType.INLINE 参考:https://www.cnblogs.com/zhmlxx/p/14547931.html
            builder.insertImage(image, RelativeHorizontalPosition.DEFAULT, 1, RelativeVerticalPosition.MARGIN, 1, 100, 120, WrapType.INLINE);
            
            //当前的段落
            Paragraph curParagraph = builder.getCurrentParagraph();
            builder.getParagraphFormat().clearFormatting();
            //创建二级目录
            builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);
            builder.writeln("第1个标题");
            //插入书签
            builder.startBookmark( "MyBookmark" );
            //添加当前目录下,添加正文
            builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.BODY_TEXT);
            //插入表格
            builder.startTable();
            builder.insertCell();
            builder.write("Row 1, Cell 1 Content.");

            builder.insertCell();
            builder.write("Row 1, Cell 2 Content.");
            builder.endRow();

            builder.insertCell();
            builder.write("Row 2, Cell 1 Content");

            builder.insertCell();
            builder.write("Row 2, Cell 2 Content.");
            builder.endRow();

            builder.endTable();

            builder.write(" ");
            
            //插入表格2
            builder.startTable();
            builder.insertCell();
            builder.write("table2 Row 1, Cell 1 Content.");

            // Build the second cell
            builder.insertCell();
            builder.write("table2 Row 1, Cell 2 Content.");
            // Call the following method to end the row and start a new row.
            builder.endRow();

            // Build the first cell of the second row.
            builder.insertCell();
            builder.write("table2 Row 2, Cell 1 Content");

            // Build the second cell.
            builder.insertCell();
            builder.write("table2 Row 2, Cell 2 Content.");
            builder.endRow();

            // Signal that we have finished building the table.
            builder.endTable();


            builder.endBookmark( "MyBookmark" );

            //替换模板内 的内容
            doc.getRange().replace("content", "ccccccccasdasdasdasd", true, false);

            //2 填充数据源---动态表格
            doc.getMailMerge().executeWithRegions(new MapMailMergeDataSource(getMapList(), "table"));

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            //更新目录
            doc.updateFields();
            doc.save(bos, SaveFormat.DOCX);
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());

            // 输出位置
            outputStream = new FileOutputStream(new File(DownloadPath+"text.docx"));

            int bytesRead = 0;
            while ((bytesRead = bis.read()) != -1) {
                outputStream.write(bytesRead);
            }
            outputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


 private List<Map<String, Object>> getMapList() {
        List<Map<String, Object>> dataList = new ArrayList<Map<String,Object>>();

        for (int i = 0; i < 5; i++) {
            Map<String, Object> record = new HashMap<String, Object>();
            //这里的key要与模板中的<<xxxxx>>对应
            record.put("Num", i+"1");
            record.put("name", "夏丹"+(i+1));
            record.put("Address", "中国 北京市 东城区");
            record.put("City", "北京");
            dataList.add(record);
        }
        return dataList;
    }

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档