9.21日开发提交
This commit is contained in:
@@ -145,6 +145,12 @@
|
||||
<version>3.1.701</version>
|
||||
</dependency>
|
||||
|
||||
<!-- poi-tl-->
|
||||
<dependency>
|
||||
<groupId>com.deepoove</groupId>
|
||||
<artifactId>poi-tl</artifactId>
|
||||
<version>1.9.1</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
|
||||
import com.deepoove.poi.XWPFTemplate;
|
||||
import com.deepoove.poi.config.Configure;
|
||||
import com.deepoove.poi.data.*;
|
||||
import com.deepoove.poi.data.style.ParagraphStyle;
|
||||
import com.deepoove.poi.data.style.Style;
|
||||
import com.deepoove.poi.util.PoitlIOUtils;
|
||||
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author: ymbgy
|
||||
* @Date: 2022-09-21 13:26
|
||||
*/
|
||||
public class WordUtil {
|
||||
|
||||
private CellRenderData cell;
|
||||
|
||||
/**
|
||||
* 查询生成的word文件流
|
||||
*
|
||||
* @param response
|
||||
* @param datas
|
||||
* @param modalFilePath
|
||||
* @param resultFilePath
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void getDocStreamFile(HttpServletResponse response, Map<String, Object> datas, String modalFilePath, String resultFilePath, String fileName) throws IOException {
|
||||
//获取word模板和填充数据
|
||||
XWPFTemplate template = XWPFTemplate.compile(modalFilePath).render(datas);
|
||||
//设置返回类型及文件名
|
||||
response.setContentType("application/octet-stream");
|
||||
response.setHeader("Content-disposition", "attachment;filename=\"" + fileName + "\"");
|
||||
//获取返回输出流
|
||||
OutputStream out = response.getOutputStream();
|
||||
BufferedOutputStream bos = new BufferedOutputStream(out);
|
||||
template.write(bos);
|
||||
bos.flush();
|
||||
out.flush();
|
||||
PoitlIOUtils.closeQuietlyMulti(template, bos, out);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询生成的word文件路径
|
||||
*
|
||||
* @param datas
|
||||
* @param modalFilePath
|
||||
* @param resultFilePath
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String getDocFilePath(Map<String, Object> datas, String modalFilePath, String resultFilePath) throws IOException {
|
||||
//获取word模板和填充数据
|
||||
XWPFTemplate template = XWPFTemplate.compile(modalFilePath).render(datas);
|
||||
template.writeAndClose(new FileOutputStream(resultFilePath));
|
||||
return resultFilePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造绘制表格的方法
|
||||
*/
|
||||
public static TableRenderData rebuildWordTableData(List<String> headerList, String headerBackgroundColor, String textColor, List<List<String>> tableContentList) {
|
||||
RowRenderData header = rebuildWordTableHead(headerList, headerBackgroundColor, textColor);
|
||||
List<RowRenderData> content = rebuildWordTableContent(tableContentList);
|
||||
TableRenderData tableRenderData = Tables.create().addRow(header);
|
||||
for (int i = 0; content != null && i < content.size(); i++) {
|
||||
tableRenderData.addRow(content.get(i));
|
||||
}
|
||||
return tableRenderData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造表格表头
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static RowRenderData rebuildWordTableHead(List<String> headerList, String headerBackgroundColor, String textColor) {
|
||||
RowRenderData header = new RowRenderData();
|
||||
for (int i = 0; headerList != null && i < headerList.size(); i++) {
|
||||
CellRenderData cellRenderData = new CellRenderData();
|
||||
ParagraphRenderData paragraphRenderData = new ParagraphRenderData();
|
||||
Style style = new Style();
|
||||
style.setColor(textColor);
|
||||
style.setFontSize(12.0);
|
||||
ParagraphStyle paragraphStyle = ParagraphStyle.builder()
|
||||
.withAlign(ParagraphAlignment.CENTER)
|
||||
// .withBackgroundColor(headerBackgroundColor)
|
||||
.withDefaultTextStyle(style)
|
||||
.build();
|
||||
paragraphRenderData.addText(headerList.get(i));
|
||||
paragraphRenderData.setParagraphStyle(paragraphStyle);
|
||||
cellRenderData.addParagraph(paragraphRenderData);
|
||||
header.addCell(cellRenderData);
|
||||
}
|
||||
return header;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造表格内容
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static List<RowRenderData> rebuildWordTableContent(List<List<String>> tableContentList) {
|
||||
List<RowRenderData> rowContentList = new ArrayList<>();
|
||||
for (int i = 0; tableContentList != null && i < tableContentList.size(); i++) {
|
||||
List<String> rowdataList = tableContentList.get(i);
|
||||
RowRenderData rowcontent = new RowRenderData();
|
||||
for (int j = 0; rowdataList != null && j < rowdataList.size(); j++) {
|
||||
CellRenderData cellRenderData = new CellRenderData();
|
||||
ParagraphRenderData paragraphRenderData = new ParagraphRenderData();
|
||||
Style style = new Style();
|
||||
style.setFontSize(12.0);
|
||||
ParagraphStyle paragraphStyle = ParagraphStyle.builder()
|
||||
.withAlign(ParagraphAlignment.CENTER)
|
||||
.withDefaultTextStyle(style)
|
||||
.build();
|
||||
paragraphRenderData.addText(rowdataList.get(j));
|
||||
paragraphRenderData.setParagraphStyle(paragraphStyle);
|
||||
cellRenderData.addParagraph(paragraphRenderData);
|
||||
rowcontent.addCell(cellRenderData);
|
||||
}
|
||||
rowContentList.add(rowcontent);
|
||||
}
|
||||
return rowContentList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建图片内容
|
||||
*/
|
||||
public static PictureRenderData rebuildImageContent(Integer with, Integer height, String imageUrl, String relatedPath, Byte[] imageBytes) {
|
||||
PictureRenderData pictureRenderData = null;
|
||||
if (!StringUtils.isBlank(imageUrl)) {
|
||||
// pictureRenderData = Pictures.of(imageUrl).size(with, height).create();
|
||||
//Pictures.PictureBuilder pictureBuilder = Pictures.of("https://res.wx.qq.com/a/wx_fed/weixin_portal/res/static/img/1EtCRvm.png");
|
||||
}else if (!StringUtils.isBlank(relatedPath)) {
|
||||
pictureRenderData = Pictures.ofLocal(relatedPath).size(with,height).create();
|
||||
// Pictures.PictureBuilder pictureBuilder = Pictures.of("https://res.wx.qq.com/a/wx_fed/weixin_portal/res/static/img/1EtCRvm.png");
|
||||
}
|
||||
return pictureRenderData;
|
||||
}
|
||||
public static String getResultFilePath(Map<String, Object> datas, Configure config, String modalFilePath, String resultFilePath) throws IOException {
|
||||
//获取word模板和填充数据
|
||||
XWPFTemplate template = XWPFTemplate.compile(modalFilePath,config).render(datas);
|
||||
template.writeAndClose(new FileOutputStream(resultFilePath));
|
||||
return resultFilePath;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user