Blanc79 2023-08-15 20:30 采纳率: 0%
浏览 64
已结题

java 使用Apache接口导出.docx格式的word文档打开报错,用wps或者后缀改成.doc却能打开

java 使用Apache接口导出.docx格式的word文档打开报错,用wps或者后缀改成.doc却能打开


    public static void exportWords(HttpServletRequest request, HttpServletResponse response, String text, String fileName) {
        ServletOutputStream ostream = null;
        POIFSFileSystem poifs = null;
        ByteArrayInputStream bais = null;
        try {
            //设置编码
            byte bArr[] = text.getBytes("utf-8");
            bais = new ByteArrayInputStream(bArr);
            //Apache office处理API
            poifs = new POIFSFileSystem();
            DirectoryEntry directory = poifs.getRoot();
            DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
            request.setCharacterEncoding("utf-8");
            response.setContentType("application/msword");
            response.addHeader("Content-Disposition", "attachment;filename=" +
                    new String(fileName.getBytes("GB2312"), "iso8859-1") + ".doc");
            ostream = response.getOutputStream();
            poifs.writeFilesystem(ostream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bais != null) {
                    bais.close();
                }
                if (ostream != null) {
                    ostream.close();
                }
                if (poifs != null) {
                    poifs.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

img

  • 写回答

11条回答 默认 最新

  • 2301_79233340 2023-08-15 21:03
    关注

    引用chatgpt内容作答:
    一种常见的方法是将 HTML 转换为富文本格式,然后将其插入到 XWPFDocument 中。这需要使用一些 HTML 解析库(例如 Jsoup)将 HTML 转换为适合插入到 Word 文档中的格式,例如基于段落、字体样式和其他格式的文本。

    以下是一个简单的示例,演示如何使用 Jsoup 将 HTML 转换为富文本格式,并插入到 XWPFDocument 中:

    import org.apache.poi.xwpf.usermodel.*;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    
    import java.io.FileOutputStream;
    
    public class HtmlToDocxExporter {
        public static void exportHtmlToDocx(String htmlContent, String fileName) {
            try (FileOutputStream fileOut = new FileOutputStream(fileName)) {
                XWPFDocument document = new XWPFDocument();
                XWPFParagraph paragraph = document.createParagraph();
    
                Document jsoupDoc = Jsoup.parse(htmlContent);
                Elements elements = jsoupDoc.body().children();
    
                for (Element element : elements) {
                    XWPFRun run = paragraph.createRun();
                    processHtmlElement(element, run);
                }
    
                document.write(fileOut);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private static void processHtmlElement(Element element, XWPFRun run) {
            String tagName = element.tagName();
            switch (tagName) {
                case "b":
                    run.setBold(true);
                    break;
                case "i":
                    run.setItalic(true);
                    break;
                // Handle other HTML tags as needed
                default:
                    run.setText(element.text());
                    break;
            }
    
            for (Element child : element.children()) {
                processHtmlElement(child, run);
            }
        }
    
        public static void main(String[] args) {
            String htmlContent = "<p>This is <b>bold</b> and <i>italic</i> text.</p>";
            String fileName = "output.docx";
            exportHtmlToDocx(htmlContent, fileName);
        }
    }
    

    这只是一个简单的示例,需要根据您的具体需求进行调整。此示例会尝试将 <b> 元素转换为粗体样式,将 <i> 元素转换为斜体样式,并将其他内容作为普通文本插入到 Word 文档中。

    将 HTML 转换为适合插入到 Word 文档的富文本格式可能需要进行更复杂的处理,根据实际的 HTML 结构和样式需求。

    评论

报告相同问题?

问题事件

  • 系统已结题 8月23日
  • 创建了问题 8月15日