• Java查询数据放入word模板中并在前端导出下载


    需求:查询数据放入word模板中并在前端导出下载

    解决方法:在模板的位置定义参数如 {{name}}  {{age}}等等,使用 poi 处理

    伪代码:

    1. @PostMapping("/practiceAppr")
    2. public AjaxResult practiceAppr(OutputStream outputStream, @RequestBody ExportToExcelParamDto paramDto) {
    3. //查询数据 ExportToWordByPracticeApprDto 为定义的模板中的参数
    4. ExportToWordByPracticeApprDto app= baseService.practiceApprExport(paramDto);
    5. try {
    6. //获取模板文件
    7. try (InputStream is = TrActivityGroupServiceImpl.class.getClassLoader().getResourceAsStream("word/导出模板A4.docx")
    8. ) {
    9. try (XWPFDocument doc = new XWPFDocument(is)
    10. ) {
    11. Map replaceMap = BeanUtil.beanToMap(app);
    12. Map resultMap = new HashMap<>();
    13. //word中的占位符格式是{{}}
    14. replaceMap.forEach((placeholder, replacement) -> resultMap.put("{{" + placeholder + "}}", replacement));
    15. //处理文件替换参数为实际值
    16. replacePlaceholders(doc, resultMap);
    17. doc.write(outputStream);
    18. outputStream.close();
    19. is.close();
    20. }
    21. }
    22. } catch (Exception e) {
    23. logger.error("文件导出错误{}", e.getMessage());
    24. }
    25. return null;
    26. }
    27. private void replacePlaceholders(XWPFDocument document, Map placeholders) throws IOException, InvalidFormatException {
    28. //处理普通word文字 不包含表格
    29. for (XWPFParagraph paragraph : document.getParagraphs()) {
    30. List runs = paragraph.getRuns();
    31. for (XWPFRun run : runs) {
    32. String text = run.getText(0);
    33. if (text != null) {
    34. for (Map.Entry entry : placeholders.entrySet()) {
    35. if (text.contains(entry.getKey())) {
    36. text = text.replace(entry.getKey(), entry.getValue() != null ? (String) entry.getValue() : "");
    37. run.setText(text, 0);
    38. }
    39. }
    40. }
    41. }
    42. }
    43. // 处理替换表格中的占位符
    44. for (XWPFTable table : document.getTables()) {
    45. for (XWPFTableRow row : table.getRows()) {
    46. for (XWPFTableCell cell : row.getTableCells()) {
    47. for (XWPFParagraph paragraph : cell.getParagraphs()) {
    48. List runs = paragraph.getRuns();
    49. for (XWPFRun run : runs) {
    50. String text = run.getText(0);
    51. if (text != null) {
    52. for (Map.Entry entry : placeholders.entrySet()) {
    53. if (text.contains(entry.getKey())) {
    54. //获取、处理图片略
    55. ...
    56. ...
    57. int format = XWPFDocument.PICTURE_TYPE_PNG;
    58. //图片地址
    59. BufferedImage image = ImageIO.read(new URL(value));
    60. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    61. //suffix为图片的后缀 .png
    62. ImageIO.write(image, suffix, outputStream);
    63. byte[] imageBytes = outputStream.toByteArray();
    64. //后两个参数是宽高
    65. run.addPicture(new ByteArrayInputStream(imageBytes), format, fileName, Units.toEMU(80), Units.toEMU(40));
    66. //替换文字 图片和文字如果都展示
    67. text = text.replace(entry.getKey(), entry.getValue() != null ? (String) entry.getValue() : "");
    68. run.setText(text, 0);
    69. }
    70. }
    71. }
    72. }
    73. }
    74. }
    75. }
    76. }
    77. }

    最后前端处理进行下载即可。

  • 相关阅读:
    【网络通信 -- WebRTC】项目实战记录 -- linux 环境下 libmediasoup 编译与测试
    【跟学C++】C++STL标准模板库——算法详细整理(中)(Study18)
    Web安全知识
    CDN助力行业互联网持续提速
    03梯度下降
    Qt之QSS基础
    qt-双臂SCARA机器人动画
    大型连锁百货运维审计用什么软件好?有哪些功能?
    【智慧零售】门店管理设备解决方案,为企业数字化运营升级赋能
    系统提示0x00000709错误怎么办?
  • 原文地址:https://blog.csdn.net/qq_29958413/article/details/134531381