废话少说,直接上代码,看不懂私信我。
代码流程1、从文件目录中模糊匹配到要导出的md文件 (可忽视)
2、将md的内容读出来 (可从数据库中读出来md字符串)
3、将md的内容字符串转html字符串
4、导出html字符串成doc文件
工具类com.atlassian.commonmark commonmark0.14.0 com.atlassian.commonmark commonmark-ext-gfm-tables0.14.0 com.atlassian.commonmark commonmark-ext-heading-anchor0.14.0 com.github.houbb markdown-toc1.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('下载成功') })
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)