无关风月
2025-03-06 d8b5536c53c7eddd0c6705d63babeaea005944b0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//package com.ruoyi.web.controller.tool;
//
//import org.apache.poi.xwpf.usermodel.*;
//import java.io.*;
//import java.time.LocalDateTime;
//import java.time.format.DateTimeFormatter;
//
//public class WordPdfGenerator {
//
//    // 示例数据类
//    public static class UserData {
//        private String name;
//        private String id;
//        private String department;
//        private LocalDateTime createTime;
//
//        public UserData(String name, String id, String department) {
//            this.name = name;
//            this.id = id;
//            this.department = department;
//            this.createTime = LocalDateTime.now();
//        }
//
//        // getter方法省略
//    }
//
//    public static void generateDocument(UserData userData, String outputPath) {
//        try (XWPFDocument document = new XWPFDocument()) {
//            // 创建标题
//            XWPFParagraph title = document.createParagraph();
//            title.setAlignment(ParagraphAlignment.CENTER);
//            XWPFRun titleRun = title.createRun();
//            titleRun.setText("用户信息表");
//            titleRun.setBold(true);
//            titleRun.setFontSize(20);
//            titleRun.setFontFamily("宋体");
//
//            // 添加空行
//            document.createParagraph();
//
//            // 创建表格
//            XWPFTable table = document.createTable(5, 2);
//            table.setWidth("100%");
//
//            // 设置表格数据
//            setCellText(table, 0, "姓名", userData.name);
//            setCellText(table, 1, "ID", userData.id);
//            setCellText(table, 2, "部门", userData.department);
//            setCellText(table, 3, "创建时间",
//                userData.createTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//
//            // 保存Word文档
//            String wordFile = outputPath + ".docx";
//            try (FileOutputStream out = new FileOutputStream(wordFile)) {
//                document.write(out);
//            }
//
//            // 转换为PDF
//            convertToPdf(wordFile, outputPath + ".pdf");
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    private static void setCellText(XWPFTable table, int row, String label, String value) {
//        XWPFTableRow tableRow = table.getRow(row);
//        tableRow.getCell(0).setText(label);
//        tableRow.getCell(1).setText(value);
//    }
//
//    private static void convertToPdf(String wordPath, String pdfPath) {
//        try {
//            // 使用LibreOffice进行转换
//            ProcessBuilder pb = new ProcessBuilder(
//                "soffice",
//                "--headless",
//                "--convert-to", "pdf",
//                "--outdir", new File(pdfPath).getParent(),
//                wordPath
//            );
//            Process process = pb.start();
//
//            // 等待转换完成
//            int exitCode = process.waitFor();
//            if (exitCode == 0) {
//                System.out.println("PDF转换成功!");
//            } else {
//                System.out.println("PDF转换失败!");
//            }
//
//            // 删除临时Word文件
//            new File(wordPath).delete();
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    public static void main(String[] args) {
//        // 示例使用
//        UserData userData = new UserData("张三", "EMP001", "技术部");
//        String outputPath = "E:\\";
//        generateDocument(userData, outputPath);
//    }
//}