当前位置:   article > 正文

Spring boot + poi 导出Word_若依导出oom

若依导出oom

基于若依框架:Spring boot + poi 导出Word

Pom

 		<!-- Word Start-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>4.1.2</version>
        </dependency>
		<!-- Word End-->
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Controller

    @PostMapping("/exportWord")
    @ResponseBody
    public AjaxResult exportWord(SysUser user)
    {
        List<SysUser> list = userService.selectUserList(user);
    	ExportWord ew = new ExportWord();
        return ew.word(list, "导出文件夹名称");
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Util

package com.ruoyi.web.controller.system;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.UtilException;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;

public class ExportWord {
    private static final Logger log = LoggerFactory.getLogger(ExportWord.class);

    public AjaxResult word(List<SysUser> userList, String fileName){
        String uuid = UUID.randomUUID().toString();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //格式化当前系统日期
        XWPFDocument doc = new XWPFDocument();
        OutputStream out = null;
        try
        {
            for (int i = 0; i < userList.size(); i++) {
                //创建一个段落
                XWPFParagraph para = doc.createParagraph();
                //一个XWPFRun代表具有相同属性的一个区域:一段文本
                XWPFRun run16 = para.createRun();
                run16.setText("客户名称:" + userList.get(i).getKhmc());//文本
                run16.setBold(true);//加粗
                run16.setFontSize(16);//字体大小
                run16.setFontFamily("微软雅黑");//字体,范围----效果不详
                run16.addCarriageReturn();//回车键

                XWPFRun run12 = para.createRun();
                run12.setText("联系人:"  + userList.get(i).getUserName());
                run12.setBold(false);//加粗
                run12.setFontSize(12);//字体大小
                run12.setFontFamily("微软雅黑");//字体,范围----效果不详
                run12.addCarriageReturn();//回车键

                run12.setText("联系电话:" + userList.get(i).getPhonenumber());
                run12.setBold(false);//加粗
                run12.setFontSize(12);//字体大小
                run12.setFontFamily("微软雅黑");//字体,范围----效果不详
                run12.addCarriageReturn();//回车键

                XWPFRun run16_2 = para.createRun();
                run16_2.setText("件数共:"  + userList.get(i).getSex());//文本
                run16_2.setBold(true);//加粗
                run16_2.setFontSize(16);//字体大小
                run16_2.setFontFamily("微软雅黑");//字体,范围----效果不详
                run16_2.addCarriageReturn();//回车键

                XWPFRun run12_2 = para.createRun();
                run12_2.setText("运输方式:" + userList.get(i).getYxfs());//文本
                run12_2.setBold(false);//加粗
                run12_2.setFontSize(12);//字体大小
                run12_2.setFontFamily("微软雅黑");//字体,范围----效果不详
                run12_2.addCarriageReturn();//回车键

                run12_2.setText("发货日期:" + sdf.format(userList.get(i).getCreateTime()));//文本
                run12_2.addCarriageReturn();//回车键

                run12_2.setText("实际收货地址:" + userList.get(i).getDz());//文本
                if(i == 0){
                    run12_2.addCarriageReturn();//回车键
                    run12_2.addCarriageReturn();//回车键
                    run12_2.addCarriageReturn();//回车键
                    run12_2.addCarriageReturn();//回车键
                    run12_2.addCarriageReturn();//回车键
                }else if(i != userList.size()-1){
					run12_2.addCarriageReturn();//回车键
                    run12_2.addCarriageReturn();//回车键
                    run12_2.addCarriageReturn();//回车键
                    run12_2.addCarriageReturn();//回车键
                }
            }
            out = new FileOutputStream(getAbsoluteFile(uuid,fileName));
            doc.write(out);
            return AjaxResult.success(encodingFilename(uuid,fileName));
        }
        catch (Exception e)
        {
            log.error("导出Excel异常{}", e.getMessage());
            throw new UtilException("导出Excel失败,请联系网站管理员!");
        }
        finally
        {

            IOUtils.closeQuietly(doc);
            IOUtils.closeQuietly(out);
        }
    }

    /**
     * 编码文件名
     */
    public String encodingFilename(String uuid,String filename)
    {
        filename = uuid + "_" + filename + ".docx";
        return filename;
    }

    /**
     * 获取下载路径
     *
     * @param filename 文件名称
     */
    public String getAbsoluteFile(String uuid,String filename)
    {
        String downloadPath = RuoYiConfig.getDownloadPath() + uuid + "_" + filename + ".docx";
        File desc = new File(downloadPath);
        if (!desc.getParentFile().exists())
        {
            desc.getParentFile().mkdirs();
        }
        return downloadPath;
    }
}
  • 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
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127

XWPFRun

XWPFRun 对象属性。例如:

方法类型描述
run.setText()String写入Word内容
run.setBold()Boolean加粗
run.setColor()String设置颜色–十六进制
run.setDoubleStrikethrough()Boolean双删除线
run.setFontFamily()String字体
run.setFontSize()int字体大小
run.setImprinted()Boolean印迹(悬浮阴影)
run.setItalic()Boolean斜体(字体倾斜)
run.setShadow()Boolean阴影
run.setStrikeThrough()Boolean单删除线
run.addTab()tab键
run.addCarriageReturn()回车键

效果

SoringBoot Poi 导出Word
链接:

  1. Maven: https://mvnrepository.com/
  2. Csnd: https://www.csdn.net/
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/853381
推荐阅读
相关标签
  

闽ICP备14008679号