10.31开发

This commit is contained in:
hejinbo
2023-10-31 19:15:46 +08:00
parent 580901e91c
commit 2238224e83
7 changed files with 495 additions and 58 deletions
@@ -7,8 +7,14 @@ 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 org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.wp.usermodel.Paragraph;
import org.apache.poi.xwpf.usermodel.*;
import javax.print.Doc;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
@@ -93,7 +99,7 @@ public class WordUtil {
style.setFontSize(12.0);
ParagraphStyle paragraphStyle = ParagraphStyle.builder()
.withAlign(ParagraphAlignment.CENTER)
// .withBackgroundColor(headerBackgroundColor)
// .withBackgroundColor(headerBackgroundColor)
.withDefaultTextStyle(style)
.build();
paragraphRenderData.addText(headerList.get(i));
@@ -139,18 +145,62 @@ public class WordUtil {
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();
// 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();
} 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);
XWPFTemplate template = XWPFTemplate.compile(modalFilePath, config).render(datas);
template.writeAndClose(new FileOutputStream(resultFilePath));
return resultFilePath;
}
public static void changeText(XWPFDocument document) {
//获取文字段落集合
List<XWPFParagraph> paragraphs = document.getParagraphs();
//所有类型集合(文字段落、表格、图片等)
List<IBodyElement> listBe = document.getBodyElements();
List<Integer> runList = new ArrayList<>();
int n = 0;
for (int i = 0; i < listBe.size(); i++) {
//BodyElementType.PARAGRAPH : 枚举中的文字段落
//文字为空时,先添加到list中;
//注意picture类型也在PARAGRAPH中,需要校验embeddedPictures的长度是否为0
//为0表示空行,大于0表示有图片,可能还有其他类型,暂时没遇到,各位自行斟酌
if (paragraphs.size() > n && !paragraphs.get(n).getRuns().isEmpty()) {
if (StringUtils.isEmpty(paragraphs.get(n).getRuns().get(0).text())
&& paragraphs.get(n).getRuns().get(0).getEmbeddedPictures().size() == 0) {
runList.add(i);
}
}
n++;
//非文字段落n-1
if (listBe.get(i).getElementType() != BodyElementType.PARAGRAPH) {
n--;
}
}
//遍历list删除
if (!runList.isEmpty()) {
for (int i = runList.size() - 1; i >= 0; i--) {
int index = runList.get(i);
if (index >= 0 && index < document.getBodyElements().size()) {
document.removeBodyElement(index);
}
}
}
}
}