赞
踩
基于若依框架:Spring boot + poi 导出Word
<!-- 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-->
@PostMapping("/exportWord")
@ResponseBody
public AjaxResult exportWord(SysUser user)
{
List<SysUser> list = userService.selectUserList(user);
ExportWord ew = new ExportWord();
return ew.word(list, "导出文件夹名称");
}
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; } }
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() | 回车键 |
链接:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。