java导出word文档组件_Java导出word文档(POI&Spire.Doc)

该博客介绍了如何在Java中使用POI和Spire.Doc库创建Word文档,包括添加目录、设置字体格式、插入图片以及自定义页眉页脚。示例代码展示了如何通过HTTP请求获取图片并将其插入文档,以及如何调整图片大小和设置页码样式。
摘要由CSDN通过智能技术生成

/*** 导出文件* @param condition* @return*/

@PostMapping(value = "/exportInfoToWord")

public void exportInfoToWord(@RequestBody HashMap condition, HttpServletResponse response) throws IOException {

//创建Word文档 Document doc = new Document();

//添加一个目录的section Section section = doc.addSection();

Paragraph para = section.addParagraph();

TextRange tr = para.appendText("目 录");

//设置字体大小和颜色 tr.getCharacterFormat().setTextColor(Color.black);

tr.getCharacterFormat().setFontName("宋体");

tr.getCharacterFormat().setFontSize(20);

para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

//设置段后间距 para.getFormat().setAfterSpacing(10);

//添加段落 para = section.addParagraph();

//通过指定最低的Heading级别1和最高的Heading级别3,创建包含Heading 1、2、3,制表符前导符和右对齐页码的默认样式的Word目录。标题级别范围必须介于1到9之间 para.appendTOC(1, 3);

// 获取http客户端 CloseableHttpClient client = HttpClients.createDefault();

List fileAddress = new ArrayList();

fileAddress.add("http://114.116.236.37:9000/image/file/676faf54-105a-4f36-8efe-3e73729eb69f.jpg");

fileAddress.add("http://114.116.236.37:9000/image/file/962d7ea0-1222-418a-9297-9eee48d6bc85.jpeg");

fileAddress.add("http://114.116.236.37:9000/image/file/a5c6f1c6-ba1e-4178-a791-eb539afb4042.jpeg");

for (String address : fileAddress) {

//添加一个section section = doc.addSection();

//添加一个段落 para = section.addParagraph();

para.appendText(address);

//应用Heading 1样式到段落 para.applyStyle(BuiltinStyle.Heading_1);

section.addParagraph();

// 通过httpget方式来实现我们的get请求 HttpGet httpGet = new HttpGet(address);

//设置请求头信息 httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");

// 通过client调用execute方法,得到我们的执行结果就是一个response,所有的数据都封装在response里面了 CloseableHttpResponse httpResponse = client.execute(httpGet);

// HttpEntity 是一个中间的桥梁,在httpClient里面,是连接我们的请求与响应的一个中间桥梁,所有的请求参数都是通过HttpEntity携带过去的 // 所有的响应的数据,也全部都是封装在HttpEntity里面 HttpEntity entity = httpResponse.getEntity();

InputStream in = entity.getContent();

//添加图片段落 Paragraph paraPicture = section.addParagraph();

DocPicture picture = paraPicture.appendPicture(in);

//设置图片宽度 picture.setWidth(490f);

//设置图片高度 picture.setHeight(400f);

//添加第一个段落 Paragraph paraText = section.addParagraph();

//给第一个段落和第二个段落设置水平居中对齐方式 paraPicture.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

paraText.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

//设置第一个段落的段后间距 paraPicture.getFormat().setAfterSpacing(15f);

//释放资源 EntityUtils.consume(entity);

}

//获取第一个节中的页脚 HeaderFooter footer = doc.getSections().get(0).getHeadersFooters().getFooter();

//添加段落到页脚 Paragraph footerParagraph = footer.addParagraph();

//添加文字、页码域和总页数域到段落 footerParagraph.appendText("第");

footerParagraph.appendField("page number", FieldType.Field_Page);

footerParagraph.appendText("页");

footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

//设置目录页码数字格式为罗马数字 doc.getSections().get(0).getPageSetup().setPageNumberStyle(PageNumberStyle.Roman_Lower);

//设置内容页码数字格式为阿拉伯数字 doc.getSections().get(1).getPageSetup().setPageNumberStyle(PageNumberStyle.Arabic);

//设置第二节页码从新开始编码,并设置起始页码数字 doc.getSections().get(1).getPageSetup().setRestartPageNumbering(true);

doc.getSections().get(1).getPageSetup().setPageStartingNumber(1);

//更新目录 doc.updateTableOfContents();

//保存到临时固定地址 String path = "F:\\tempDocFiles";

File dir = new File(path);

if (!dir.exists()) {

dir.mkdir();

}

//临时文件路径 String filePath = path + File.separator + UUID.randomUUID();

doc.saveToFile(filePath + ".docx", FileFormat.Docx);

//重新读取文档,进行操作 InputStream is = new FileInputStream(filePath + ".docx");

XWPFDocument document = new XWPFDocument(is);

//以上Spire.Doc 生成的文件会自带警告信息,这里来删除Spire.Doc 的警告 document.removeBodyElement(0);

//输出word内容文件流,提供下载 response.reset();

response.setContentType("application/x-msdownload");

String fileName = "" + System.currentTimeMillis() + ".docx";

response.addHeader("Content-Disposition", "attachment; filename=" + fileName);

delFile(dir);

ByteArrayOutputStream ostream = new ByteArrayOutputStream();

OutputStream servletOS = null;

try {

servletOS = response.getOutputStream();

document.write(ostream);

servletOS.write(ostream.toByteArray());

servletOS.flush();

servletOS.close();

} catch (Exception e) {

e.printStackTrace();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值