Java 读取数据写入word文档并导出

18 篇文章 0 订阅
13 篇文章 0 订阅

项目所遇到的问题,因项目需求从数据库中读取数据,并且写到word后导出该word文档。

话不多说,上代码

首先需要依赖的包:

  <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.8</version>
        </dependency>

工具类:



import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ZeroCopyHttpOutputMessage;
import org.springframework.http.server.reactive.ServerHttpResponse;

import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;


/**
 * 创建doc工具类
 *
 * @author John
 * @since 2021/09/28
 */
public class WordUtils {

    public static void createWord(String path, String fileName) {
        //判断目录是否存在
        File file = new File(path);
        //exists()测试此抽象路径名表示的文件或目录是否存在。
        //mkdir()创建此抽象路径名指定的目录。
        //mkdirs()创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。
        if (!file.exists()) file.mkdirs();
        //因为HWPFDocument并没有提供公共的构造方法 所以没有办法构造word
        //这里使用word2007及以上的XWPFDocument来进行构造word
        XWPFDocument document = new XWPFDocument();
        OutputStream stream = null;
        try {
            stream = new FileOutputStream(new File(file, fileName));
            document.write(stream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (stream != null) ;
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 有些方法需要传特殊类型的参数的时候,一般可以用★静态的接口.参数★来传参
     * 向word中写入数据
     *
     * @param path
     * @param data
     */
    public static void writeDataDocx(String path, String data, boolean bold, int size) {
        InputStream istream = null;
        OutputStream ostream = null;
        try {
            istream = new FileInputStream(path);
            ostream = new FileOutputStream(path);
            XWPFDocument document = new XWPFDocument();
            //添加一个段落
            XWPFParagraph p1 = document.createParagraph();
            //setAlignment()指定应适用于此段落中的文本的段落对齐方式。CENTER LEFT...
            //p1.setAlignment(ParagraphAlignment.LEFT);
            //p1.setBorderBetween(Borders.APPLES);
            //p1.setBorderBottom(Borders.APPLES);
            //p1.setBorderLeft(Borders.APPLES);指定应显示在左边页面指定段周围的边界。
            //p1.setBorderRight(Borders.ARCHED_SCALLOPS);指定应显示在右侧的页面指定段周围的边界。
            //p1.setBorderTop(Borders.ARCHED_SCALLOPS);指定应显示上方一组有相同的一组段边界设置的段落的边界。这几个是对段落之间的格式的统一,相当于格式刷
            //p1.setFirstLineIndent(99);//---正文宽度会稍微变窄
            //p1.setFontAlignment(1);//---段落的对齐方式 1左 2中 3右 4往上 左 不可写0和负数
            //p1.setIndentationFirstLine(400);//---首行缩进,指定额外的缩进,应适用于父段的第一行。
            //p1.setIndentationHanging(400);//---首行前进,指定的缩进量,应通过第一行回到开始的文本流的方向上移动缩进从父段的第一行中删除。
            //p1.setIndentationLeft(400);//---整段缩进(右移)指定应为从左到右段,该段的内容的左边的缘和这一段文字左边的距和右边文本边距和左段权中的那段文本的右边缘之间的缩进,如果省略此属性,则应假定其值为零。
            //p1.setIndentationRight(400);//---指定应放置这一段,该段的内容从左到右段的右边缘的正确文本边距和右边文本边距和左段权中的那段文本的右边缘之间的缩进,如果省略此属性,则应假定其值为零。
            //p1.setIndentFromLeft(400);//---整段右移
            //p1.setIndentFromRight(400);
            //p1.setNumID(BigInteger.TEN);
            //p1.setPageBreak(true);//--指定当渲染此分页视图中的文档,这一段的内容都呈现在文档中的新页的开始。
            //p1.setSpacingAfter(6);//--指定应添加在文档中绝对单位这一段的最后一行之后的间距。
            //p1.setSpacingAfterLines(6);//--指定应添加在此线单位在文档中的段落的最后一行之后的间距。
            //p1.setSpacingBefore(6);//--指定应添加上面这一段文档中绝对单位中的第一行的间距。
            //p1.setSpacingBeforeLines(6);//--指定应添加在此线单位在文档中的段落的第一行之前的间距。
            //p1.setSpacingLineRule(LineSpacingRule.AT_LEAST);//--指定行之间的间距如何计算存储在行属性中。
            //p1.setStyle("");//--此方法提供了样式的段落,这非常有用.
            //p1.setVerticalAlignment(TextAlignment.CENTER);//---指定的文本的垂直对齐方式将应用于此段落中的文本
            //p1.setWordWrapped(true);//--此元素指定是否消费者应中断超过一行的文本范围,通过打破这个词 (打破人物等级) 的两行或通过移动到下一行 (在词汇层面上打破) 这个词的拉丁文字。
            XWPFRun r1 = p1.createRun();//p1.createRun()将一个新运行追加到这一段
            //setText(String value)或
            //setText(String value,int pos)
            //value - the literal text which shall be displayed in the document
            //pos - - position in the text array (NB: 0 based)
            r1.setText(data);
            //r1.setTextPosition(20);//这个相当于设置行间距的,具体这个20是怎么算的,不清楚,此元素指定文本应为此运行在关系到周围非定位文本的默认基线升降的量。不是真正意义上的行间距
            //---This element specifies the amount by which text shall be ★raised or lowered★ for this run in relation to the default baseline of the surrounding non-positioned text.
            //r1.setStrike(true);//---设置删除线的,坑人!!!
            //r1.setStrikeThrough(true);---也是设置删除线,可能有细微的区别吧
            //r1.setEmbossed(true);---变的有重影(变黑了一点)
            //r1.setDoubleStrikethrough(true);---设置双删除线
            //r1.setColor("33CC00");//---设置字体颜色 ★
            //r1.setFontFamily("fantasy");
            //r1.setFontFamily("cursive");//---设置ASCII(0 - 127)字体样式
            r1.setBold(bold);//---"加黑加粗"
            r1.setFontSize(size);//---字体大小
            //r1.setImprinted(true);//感觉与setEmbossed(true)类似,有重影
            //r1.setItalic(true);//---文本会有倾斜,是一种字体?
            //r1.setShadow(true);//---文本会变粗有重影,与前面两个有重影效果的方法感觉没什么区别
            //r1.setSmallCaps(true);//---改变了  英文字母  的格式
            //r1.setSubscript(VerticalAlign.BASELINE);//---valign垂直对齐的
            //r1.setUnderline(UnderlinePatterns.DASH);//--填underline type设置下划线
            //document.createTable(2, 2);//--创建一个制定行列的表
            //document.enforceReadonlyProtection();//--强制执行制度保护

            /**
             * r1.setDocumentbackground(doc, "FDE9D9");//设置页面背景色
             r1.testSetUnderLineStyle(doc);//设置下划线样式以及突出显示文本
             r1.addNewPage(doc, BreakType.PAGE);
             r1.testSetShdStyle(doc);//设置文字底纹
             */
            document.write(ostream);
            System.out.println("创建word成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (istream != null) {
                try {
                    istream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ostream != null) {
                try {
                    ostream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * @param response
     * @param data
     */
    public static void writeDataDocx(ServerHttpResponse response, List<String> data) {
        OutputStream outputStream = null;
        File finalFile = null;
        try {
            String fileName = System.currentTimeMillis() + ".docx";
            outputStream = new FileOutputStream(fileName);
            ZeroCopyHttpOutputMessage zeroCopyResponse = (ZeroCopyHttpOutputMessage) response;
            XWPFDocument document = new XWPFDocument();
            for (int i = 0; i < data.size(); i++) {
                //添加一个段落
                XWPFParagraph xwpfParagraph = document.createParagraph();
                XWPFRun xwpfRun = xwpfParagraph.createRun();//xwpfRun.createRun()将一个新运行追加到这一段
                xwpfRun.setBold(true);//---"加黑加粗"
                xwpfRun.setFontSize(12);//---字体大小
                xwpfRun.setText(data.get(i));
            }
            document.write(outputStream);
            HttpHeaders headers = zeroCopyResponse.getHeaders();
            headers.set("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
            headers.set("file-name", URLEncoder.encode(fileName, "UTF-8"));
            headers.set("Access-Control-Allow-Origin", "*");
            MediaType application = new MediaType("application","octet-stream", StandardCharsets.UTF_8);
            headers.setContentType(application);
            finalFile = new File(fileName);
            zeroCopyResponse.writeWith(finalFile, 0, finalFile.length()).subscribe();
            System.out.println("=====创建word成功===============>");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (finalFile != null) {
                finalFile.delete();
            }
        }
    }

    /**
     * 读数据
     *
     * @param filePath
     * @return
     */
    public static String readDataDocx(String filePath) {
        String content = "";
        InputStream istream = null;
        try {
            istream = new FileInputStream(filePath);
            XWPFDocument document = new XWPFDocument(istream);
            //getLastParagraph()返回包含页眉或页脚的文本的段落
            //getText()返回文档所有文本
            content = document.getLastParagraph().getText();//★★★★★
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (istream != null) {

            }
        }
        return content;
    }

    public static void main(String[] args) {
        String path = "D:\\poi\\";
        String fileName = "poi.docx";
        String filePath = path + fileName;
        //创建word
        WordUtils.createWord(path, fileName);
        //写入数据
        String data = "本文是以poi3.9读写2010word、2010excel、2010ppt,记录学习的脚步相应的功能在代码都有注释,就不解释了 详情可以参看poi3.9的文档主测试函数 TestMain.java";
        WordUtils.writeDataDocx(filePath, data, true, 12);
        //WordUtils.writeDataDoc(filePath,data);

        //读取数据
        //String contentWord=WordUtils.readDataDoc(filePath);
        String contentWord = WordUtils.readDataDocx(filePath);
        System.out.println("word的内容为:\n" + contentWord);
        System.out.println();
    }
}

温馨提示:

我是属于webflux 工程项目,如果是spring-web 可以使用HttpServletRequest ,因为我是webflux 所以只能用 ServerHttpResponse,根据自己需求改动吧

### 回答1: Java POI 是一个用于操作 Microsoft Office 格式文件的 Java 库,包括 Word、Excel 和 PowerPoint 等文件。在 Java 中使用 POI 库可以方便地读取、修改和创建 Office 文件。 在使用 POI 导出 Word 文档时,需要先创建一个空的 Word 文档,然后向其中添加内容。可以使用 POI 提供的 XWPFDocument 类来创建 Word 文档对象,使用 XWPFParagraph 类来创建段落对象,使用 XWPFRun 类来创建文本对象。 在创建段落对象时,可以设置段落的样式,如字体、字号、颜色、对齐方式等。在创建文本对象时,可以设置文本的样式,如加粗、斜体、下划线等。 除了文本内容,还可以向 Word 文档中添加表格、图片、超链接等元素。可以使用 POI 提供的 XWPFTable 类来创建表格对象,使用 XWPFTableRow 和 XWPFTableCell 类来创建表格行和单元格对象。可以使用 XWPFParagraph 类的 addPicture 方法来添加图片,使用 XWPFHyperlink 类来添加超链接。 最后,将创建好的 Word 文档保存到本地文件系统或输出流中即可。可以使用 XWPFDocument 类的 write 方法将文档保存到文件中,使用 XWPFDocument 类的 write 方法将文档输出到输出流中。 总之,使用 Java POI 导出 Word 文档需要掌握 XWPFDocument、XWPFParagraph、XWPFRun、XWPFTable、XWPFTableRow、XWPFTableCell、XWPFHyperlink 等类的使用方法,以及如何设置样式、添加元素和保存文档。 ### 回答2: Java POI是一个流行的Java库,它提供了对微软Office格式的读取和写入支持,其中包括Word文档导出。在使用此库导出Word文档时,可以按照以下步骤进行操作: 第一步:导入所需的库文件和工具 我们需要导入Apache POI库以及相关的库文件来使用Java POI。可以从maven中央仓库中下载这些库文件,也可以通过其他方式来获取这些文件。在项目中添加所需的库文件,并在相关的类中导入这些库文件。 第二步:创建Word文档Java中,可以使用XWPFDocument类来创建一个空白的Word文档。在此类中,可以添加标题,正文文本,表格等内容,以构建新的Word文档。可以使用以下代码来创建一个新的Word文档。 XWPFDocument doc = new XWPFDocument(); 第三步:添加内容 在Java POI中,可以使用XWPFParagraph类来添加Word文档中的段落。可以使用此类来添加文字,图片等。以下是如何使用XWPFParagraph类来添加段落的示例代码: XWPFParagraph para = doc.createParagraph(); XWPFRun run = para.createRun(); run.setText("这是一个段落"); 可以使用XWPFTable类来添加Word文档中的表格。以下是如何使用XWPFTable类来添加表格的示例代码: XWPFTable table = doc.createTable(); XWPFTableRow row = table.getRow(0); row.getCell(0).setText("第一行第一列"); row.addNewTableCell().setText("第一行第二列"); 第四步:保存文档 完成了Word文档的创建和内容添加之后,需要将其保存到磁盘上。可以使用FileOutputStream类和XWPFDocument类的write() 方法来完成保存。以下代码片段演示了如何将文档保存到磁盘上: FileOutputStream outputStream = new FileOutputStream(new File("output.docx")); doc.write(outputStream); outputStream.close(); 总结 Java POI提供了一种通过代码来创建和编辑Word文档的方式。使用Java POI,我们可以创建空白的Word文档,向文档中添加内容,例如文字,图片和表格。最后,我们可以将文档保存到磁盘上。通过这些步骤,我们可以轻松地使用Java POI导出Word文档。 ### 回答3: Java POI是一个开源的Java库,用于处理各种Microsoft Office格式文件,包括Word文档(.docx)、Excel表格(.xlsx)和PowerPoint演示文稿(.pptx)。在Java POI中,我们可以使用XWPFDocument对象导出Word文档。 1. 导入依赖 在使用Java POI库之前,首先必须要引入相应的依赖包。我们需要使用以下依赖: ``` <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建一个Word文档 我们可以使用XWPFDocument类创建一个新的Word文档。默认情况下,文档中没有任何内容: ``` XWPFDocument document = new XWPFDocument(); ``` 3. 添加标题 添加标题可以使用XWPFParagraph和XWPFRun类。XWPFParagraph用于存储段落内容,而XWPFRun用于添加样式。以下是一个添加标题的示例代码: ``` XWPFParagraph titlePara = document.createParagraph(); XWPFRun titleRun = titlePara.createRun(); titleRun.setBold(true); titleRun.setFontSize(16); titleRun.setText("这是一个标题"); ``` 4. 添加正文 添加正文和添加标题类似,只不过我们可以在添加文本之前使用setBold、setItalic、setUnderline等方法设置样式。以下是添加正文的示例代码: ``` XWPFParagraph contentPara = document.createParagraph(); XWPFRun contentRun = contentPara.createRun(); contentRun.setFontSize(12); contentRun.setText("这是一个正文"); ``` 5. 添加图片 在Word文档中添加图片可以使用XWPFRun类的addPicture方法。需要注意的是,图片必须先被转换为byte数组。以下是添加图片的示例代码: ``` // 读取图片文件 FileInputStream fis = new FileInputStream("path/to/image.png"); byte[] imageData = IOUtils.toByteArray(fis); fis.close(); // 添加图片 XWPFParagraph imagePara = document.createParagraph(); XWPFRun imageRun = imagePara.createRun(); imageRun.addPicture(new ByteArrayInputStream(imageData), XWPFDocument.PICTURE_TYPE_PNG, "image.png", Units.toEMU(300), Units.toEMU(200)); ``` 6. 保存文档 在完成文档的内容添加后,我们可以将文档保存到指定的文件中。以下是保存文档的示例代码: ``` OutputStream os = new FileOutputStream("path/to/output.docx"); document.write(os); os.close(); document.close(); ``` 以上就是使用Java POI导出Word文档的详细步骤。通过使用Java POI,我们可以方便地创建和编辑Word文档,并将其保存为docx格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值