//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);
|
// }
|
//}
|