Spring boot + poi 导出Word

Spring boot + poi 导出Word,第1张

Spring boot + poi 导出Word

poi导出Word
    • Pom
    • Controller
    • Util
      • XWPFRun
    • 效果

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

Pom
 		
        
            org.apache.poi
            poi
            4.1.2
        
        
            org.apache.poi
            poi-scratchpad
            4.1.2
        
		
Controller
    @PostMapping("/exportWord")
    @ResponseBody
    public AjaxResult exportWord(SysUser user)
    {
        List list = userService.selectUserList(user);
    	ExportWord ew = new ExportWord();
        return ew.word(list, "导出文件夹名称");
    }
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 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;
    }

    
    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;
    }
}
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()回车键 效果


链接:

  1. Maven: https://mvnrepository.com/
  2. Csnd: https://www.csdn.net/

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5683967.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存