Browse Source

多html合并word

chen 3 weeks ago
parent
commit
3559dcc6de

+ 47 - 0
yudao-module-contract/yudao-module-contract-biz/src/main/java/cn/iocoder/yudao/module/contract/utils/HtmlToFileConverter.java

@@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.contract.utils;
 
 import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.io.IoUtil;
+import cn.hutool.core.util.StrUtil;
 import cn.iocoder.yudao.module.infra.api.file.FileApi;
 import com.itextpdf.text.Document;
 import com.itextpdf.text.pdf.PdfWriter;
@@ -13,6 +14,7 @@ import freemarker.template.TemplateException;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.poifs.filesystem.DirectoryEntry;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.jsoup.Jsoup;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
@@ -21,6 +23,8 @@ import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 
 @Component
@@ -84,6 +88,48 @@ public class HtmlToFileConverter {
     }
 
 
+    /**
+     * html转doc
+     *
+     * @param htmlContents
+     * @param keyWord
+     */
+    public String htmlToWord(List<String> htmlContents, String path, String keyWord){
+        // 组装html(替换为自己的html)
+        String htmlContent = StrUtil.join("", htmlContents);
+        htmlContent = Jsoup.parse(htmlContent).html();
+        if (StringUtils.isNotEmpty(htmlContent)) {
+            // 生成临时文件(doc)
+            try {
+                File file = File.createTempFile(keyWord,".doc");
+                // 生成doc格式的word文档,需要手动改为docx
+                byte by[] = htmlContent.getBytes("UTF-8");
+                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(by);
+                POIFSFileSystem poifsFileSystem = new POIFSFileSystem();
+                DirectoryEntry directoryEntry = poifsFileSystem.getRoot();
+                directoryEntry.createDocument("WordDocument", byteArrayInputStream);
+                // 文件路径
+                // 保存doc文档
+                FileOutputStream outputStream = new FileOutputStream(file.getPath());
+                poifsFileSystem.writeFilesystem(outputStream);
+                byteArrayInputStream.close();
+                outputStream.close();
+
+                return fileApi.createFile(keyWord+".doc",path+"/"+file.getName(),getFileBytes(file.getPath()));
+//                return FileUploadUtils.getPathFileName(filePath, fileName);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+        return null;
+    }
+//    public static void main(String[] args) {
+//        List<String> htmlContents = new ArrayList<>();
+//        htmlContents.add("<html><body><h1>First Page</h1><p>This is the content of the first page.</p></body></html>");
+//        htmlContents.add("<html><body><h1>Second Page</h1><p>This is the content of the second page.</p></body></html>");
+//        htmlToWord(htmlContents,"","123123");
+//    }
+
     /**
      * html转pdf
      *
@@ -119,4 +165,5 @@ public class HtmlToFileConverter {
         Path path = Paths.get(filePath);
         return Files.readAllBytes(path);
     }
+
 }