废话少说,直接上代码,看不懂私信我。
代码流程1、从文件目录中模糊匹配到要导出的md文件 (可忽视)
2、将md的内容读出来 (可从数据库中读出来md字符串)
3、将md的内容字符串转html字符串
4、导出html字符串成doc文件
com.atlassian.commonmark
commonmark
0.14.0
com.atlassian.commonmark
commonmark-ext-gfm-tables
0.14.0
com.atlassian.commonmark
commonmark-ext-heading-anchor
0.14.0
com.github.houbb
markdown-toc
1.0.8
工具类
拷别人的,原创:https://www.cnblogs.com/yuanfy008/p/4500480.html
import org.commonmark.Extension;
import org.commonmark.ext.gfm.tables.TableBlock;
import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.ext.heading.anchor.HeadingAnchorExtension;
import org.commonmark.node.link;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.AttributeProvider;
import org.commonmark.renderer.html.AttributeProviderContext;
import org.commonmark.renderer.html.AttributeProviderFactory;
import org.commonmark.renderer.html.HtmlRenderer;
import java.util.*;
public class MarkdownUtils{
public static String markdownToHtml(String markdown){
Parser parser = Parser.builder().build();
Node document = parser.parse(markdown);
HtmlRenderer renderer = HtmlRenderer.builder().build();
return renderer.render(document);
}
public static String markdownToHtmlExtensitons(String markdown){
//h标题生成id
Set headingAnchorExtensions = Collections.singleton(HeadingAnchorExtension.create());
//转换table的HTML
List tableExtension = Arrays.asList(TablesExtension.create());
Parser parser = Parser.builder()
.extensions(tableExtension)
.build();
Node document = parser.parse(markdown);
HtmlRenderer renderer = HtmlRenderer.builder()
.extensions(headingAnchorExtensions)
.extensions(tableExtension)
.attributeProviderFactory(new AttributeProviderFactory() {
@Override
public AttributeProvider create(AttributeProviderContext attributeProviderContext) {
return new CustomAttributeProvider();
}
})
.build();
return renderer.render(document);
}
static class CustomAttributeProvider implements AttributeProvider{
@Override
public void setAttributes(Node node, String s, Map attributes) {
//改变a标签的target
if (node instanceof link){//instanceof是一个双目运算符,link为类或接口,若node为link子类或实现类将返回true
// attributes.put("target","_blank");
}
if (node instanceof TableBlock){
attributes.put("class","ui celled table");
}
}
}
}
使用
@GetMapping("/downloadCommDocV2")
public ResponseEntity downloadCommonFile() throws Exception{
// 支持文件名称后缀名模糊查询
String fileFolderName = "document";
String fileDocNameLike = "接口文档";
File folderFiles = new File(fileFolderName);
String[] fileList = folderFiles.list();
if(fileList.length == 0){
return new ResponseEntity("文档文件夹下为空".getBytes(StandardCharsets.UTF_8), null, HttpStatus.BAD_REQUEST);
}
String finalFileName = null;
for (String fn : fileList) {
if(StrUtil.contains(fn,fileDocNameLike)){
finalFileName = fn;
}
if(StrUtil.contains(fn,fileDocNameLike) && StrUtil.contains(fn,"最新")){
finalFileName = fn;
break;
}
}
String filePath = fileFolderName+File.separator+finalFileName;
// 关键=============== 开始
HttpHeaders headers = new HttpHeaders();
String fileName = filePath.split("/")[1];
File file = new File(filePath);
byte[] bytes = FileUtils.readFileToByteArray(file);
String text = new String(bytes);
String html = MarkdownUtils.markdownToHtmlExtensitons(text);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("content-disposition", URLEncoder.encode(fileName, "UTF-8"));
// 关键==================结束
return new ResponseEntity(html.getBytes(StandardCharsets.UTF_8), headers, HttpStatus.CREATED);
}
前端
this.$axios({
url: '/downloadCommDocV2',
method: 'GET',
responseType: 'blob'
}).then(res => {
const link = document.createElement('a') // 创建元素
// , { type: 'application/vnd.ms-excel' }
let blob = new Blob([res.data])
link.style.display = 'none'
link.href = URL.createObjectURL(blob) // 创建下载的链接
//num++
link.setAttribute('download', '接口文档.doc') // 给下载后的文件命名
document.body.appendChild(link)
link.click() // 点击下载
document.body.removeChild(link) // 下载完成移除元素
window.URL.revokeObjectURL(link.href) // 释放掉blob对象
this.$message.success('下载成功')
})



