java向word寫入數據


pom.xml

<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>

 

代碼:

private static String fileDir = "/home/xdja/conf/edums_js/";
    private static String errorWordFilePath = "importWordErrorDetails.doc";
    private static String errorExcelFilePath = "importExcelErrorDetails.doc";
/**
     * 寫導入題庫錯誤文件類容
     * @param fileDir    文件路徑
     * @param filePath   文件路徑
     * @param content    內容
     * @param type       文件類型  1---excel 2 word;
     * @author niugang
     */
    private void writerErrorFileContent(String fileDir, String filePath, List<String> content, String type) {
        XWPFDocument document = new XWPFDocument();
        OutputStream stream = null;
        BufferedOutputStream bufferStream = null;
        try {
            File dir = new File(fileDir);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            stream = new FileOutputStream(new File(fileDir+filePath));
            bufferStream = new BufferedOutputStream(stream, 1024);
            // 創建一個段落
            XWPFParagraph p1 = document.createParagraph();
            // 設置居中
            p1.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun r1 = p1.createRun();
           // 是否加粗
            r1.setBold(true);
            // 與下一行的距離
            r1.setTextPosition(30);
            if (EdumsConstants.FILE_TYPE_XLS.equals(type)) {
                // 段落名稱
                r1.setText("EXCEL導入題目錯誤格式詳解");
            }
            if (EdumsConstants.FILE_TYPE_DOC.equals(type)) {
                // 段落名稱
                r1.setText("WORD導入題目錯誤格式詳解");
            }

            // 字體大小
            r1.setFontSize(18);// 字體大小
            // 增加換行
            r1.addCarriageReturn();
            // 創建第二個段落
            XWPFParagraph p2 = document.createParagraph();
            XWPFRun r2 = p2.createRun();
            for (int i = 0; i < content.size(); i++) {
                r2.setText(i + 1 + "、" + content.get(i));
                r2.addCarriageReturn();
            }
            // 設置字體
            r2.setFontFamily("仿宋");
            r2.setFontSize(14);// 字體大小
            document.write(stream);
            stream.close();
            bufferStream.close();
            } catch (Exception ex) {
            logger.error("寫word或Excel錯誤文件失敗:{}", ex.getMessage());
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    logger.error("寫word或Excel錯誤文件失敗:{}", e.getMessage());
                }
            }
            if (bufferStream != null) {
                try {
                    bufferStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    logger.error("寫word或Excel錯誤文件失敗:{}", e.getMessage());
                }
            }
        }

    }

 

效果:

相關博客:

https://www.cnblogs.com/dayuruozhi/p/6490793.html

https://www.cnblogs.com/unruly/archive/2017/09/06/7483858.html

   

微信公眾號

                          


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM