栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java实现内容以Word文件的格式保存本地+网页下载

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java实现内容以Word文件的格式保存本地+网页下载

参考:

(java生成Word文档)

https://www.jb51.net/article/219818.htm

1、生成Word文档

1.1新建test.doc文档(docx类型不支持)

1.2里面写好具体的模板样式内容

1.3另存为.xml文件

1.4粘贴到代码上,将事先的模板内容替换成自己的变量

------------------------------------------------整个实现Word模板

------------------------------------------------接下来进行java对Word生成

        String title = "静夜思";
        String body = "床前明月光,疑是地上霜,举头望明月,低头思故乡";
		String doc = "注意:参考本文最后附录内容";
        //文件夹创建
        File file = new File("E:\test");
        if (!file.exists()) {
            file.mkdirs();
        }
        //文件创建
        File docFile = new File("E:\test", "test.doc");
        if (!docFile.exists()) {
            docFile.createNewFile();
        }
        //写入文件内容
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(docFile));
        bufferedWriter.write(doc);
		bufferedWriter.close();

------------------------------------------------end
文件写入内容除了用BufferedWriter,还有多种方式,依次介绍

参考地址:https://blog.csdn.net/ZXP2019/article/details/117197295

一、FileWriter

        FileWriter fileWriter = new FileWriter(docFile);
        fileWriter.write(doc);
		fileWriter.flush();
        fileWriter.close();

二、BufferedWriter

        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(docFile));
        bufferedWriter.write(doc);
		bufferedWriter.flush();
        bufferedWriter.close();

三、PrintWriter

        PrintWriter printWriter = new PrintWriter(docFile);
        printWriter.print(doc);
        printWriter.flush();
        printWriter.close();

四、FileOutputStream

        FileOutputStream fileOutputStream = new FileOutputStream(docFile);
        fileOutputStream.write(doc.getBytes());
        fileOutputStream.flush();
        fileOutputStream.close();

五、BufferedOutputStream

        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(docFile));
        bufferedOutputStream.write(doc.getBytes());
        bufferedOutputStream.flush();
        bufferedOutputStream.close();

六、Files

Files.write(Paths.get("E:\test\test.doc"), doc.getBytes());

推荐使用BufferedWriter

2、生成Word网页下载

采用前端get请求下载

   @GetMapping("/createFile")
   public void createFile(HttpServletResponse response) throws IOException {
       String title = "静夜思";
       String body = "床前明月光,疑是地上霜,举头望明月,低头思故乡";
       String doc = "注意:参考本文最后附录内容";
        response.setContentType("application/msword;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename="
                + new String(title.getBytes("utf-8"), "ISO8859-1") + ".doc");
       PrintWriter writer = response.getWriter();
       writer.print(doc);
       writer.flush();
       writer.close();


   }

2.1可以采用PrintWriter,直接输入文本内容,属于字符流形式

2.2可以采用ServletOutputStream,输入文本的字节形式,属于字节流形式

附录


静夜思    正文床前明月光,疑是地上霜,举头望明月,低头思故乡128280falsefalse32WPS Office_11.1.0.11636_F1E327BC-269C-435d-A152-05C5408002CA2021-08-14T15:14:00Zxb21cnadmin2022-05-12T02:38:21Z22052-11.1.0.11636A741DCA3C84549D39425B7F37E2E0111

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/914852.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号