话不多说直接上代码
package com.youxue.portal;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.*;
import java.util.*;
/**
* <p>
* 导出为html文件
* <p/>
*
* @author dx
* @since 2021/1/6 18:01
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ExportHtmlTest{
/**
* PrintStream 永远不会抛出 IOException
* 可以实现 将基本数据类型格式化成字符串输出
* 而HTML文件基本上都是由成对的标签组成,
* 咱们直接用字符串拼接成html代码
* 再通过打印流直接生成就行了
*/
@Test
public void exportWord() throws IOException {
// 1: 先初始化需要添加到html页面的内容,可以根据需求随意改动
Map<String, String> map = new LinkedHashMap<>();
map.put("投诉平台:", "黑猫投诉+工商");
map.put("平台连接:", "www.baidu.com");
map.put("投诉对象:", "教务服务");
map.put("投诉人报班手机号:", "1111111111111");
map.put("投诉人名称:", "test01");
map.put("联系电话:", "2222222222222");
map.put("投诉相关商品信息:", "2021-**********");
map.put("投诉项目:", "2222222222222");
map.put("涉事金额:", "2222222222222");
map.put("营销人:", "test02");
map.put("营销老师号:", "test03");
map.put("分配投诉处理人:", "test04");
map.put("团队主管/经理:", "陈主管");
map.put("投诉内容详情:", "***********");
map.put("投诉截图:", "https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png");
map.put("处理时间:", "2021-04.09 15:12:16");
map.put("步骤1:", "了解投诉基本情况");
map.put("处理结果:", "11111111");
map.put("step1:", "https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png,https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png");
map.put("步骤2:", "了解投诉基本情况");
map.put("处理结果2:", "22222222");
map.put("step2:", "https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png,https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png");
map.put("协商退款金额:", "50");
// 2:创建本地html文件
File file = new File("C:\\Users\\Youxue\\Desktop\\test.html");
// 3: 将文件流转换为输出流,初始化打印流 {@link PrintStream}
PrintStream printStream = new PrintStream(new FileOutputStream(file));
// 拼接HTML代码
StringBuilder sb = new StringBuilder();
sb.append("<html>")
.append("<head>")
.append("<title>投诉数据详情</title>")
.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />")
.append("<style type=\"text/css\">")
.append("TABLE{border-collapse:collapse;border-left:solid 1 #000000; border-top:solid 1 #000000;padding:5px;}")
.append("TH{border-right:solid 1 #000000;border-bottom:solid 1 #000000;}")
.append("TD{font:normal;border-right:solid 1 #000000;border-bottom:solid 1 #000000;}")
.append("</style></head>")
.append("<body bgcolor=\"#FFF8DC\">")
.append("<div align=\"center\">")
.append("<table border=\"1\" style=\"font-size: larger;\">");
for (Map.Entry<String, String> entry : map.entrySet()) {
String k = entry.getKey();
String v = entry.getValue();
// 这里判断初始化的map中如果包含.png则示为图片,而多个图片以,号分割
if (v.contains(".png")) {
String str = "";
for (String s : v.split(",")) {
str += "<img src=\"" + s + "\"/>";
}
v = str;
}
sb.append("<tr>")
.append("<td>")
.append(k)
.append("</td>")
.append("<td>")
.append(v)
.append("</td>")
.append("</tr>");
}
sb.append("<br/>")
.append("<br/>")
.append("</div></body></html>");
// 生成html
printStream.println(sb.toString());
}
}
效果图
以PrintStream 打印流打印字符串至文创建好的HTML文件中
图片地址不要已相对路径引入
在HTML转doc文件时会保留HTML文件的样式以及内容
HTML转doc文件
//
// 继上面生成HTML文件后续 ...
File docFile = File.createTempFile(String.valueOf(System.currentTimeMillis()), ".doc");
OutputStream os = new FileOutputStream(docFile);
InputStream is = new FileInputStream(file);
POIFSFileSystem fs = new POIFSFileSystem();
fs.createDocument(is, "投诉数据");
fs.writeFilesystem(os);
os.close();
is.close();
// 最后通过oss工具类上传文件获取到文件地址(或者更改doc文件生成地址为本地地址)
String s = ossTemplate.uploadFile("word/", "test04_word.doc", new FileInputStream(file));
效果图2
