Looly 5 years ago
parent
commit
ba8d7b2625

+ 1 - 0
CHANGELOG.md

@@ -16,6 +16,7 @@
 * 【core  】     增加EnumUtil.getEnumAt方法
 * 【core  】     增强EnumConvert判断能力(issue#I17082@Gitee)
 * 【all   】     log、template、tokenizer使用SPI机制代替硬编码
+* 【poi   】     Word07Writer增加addPicture
 
 ### Bug修复
 

+ 1 - 0
hutool-extra/src/main/java/cn/hutool/extra/tokenizer/AbstractResult.java

@@ -49,6 +49,7 @@ public abstract class AbstractResult implements Result{
 		throw new UnsupportedOperationException("Jcseg result not allow to remove !");
 	}
 
+	@SuppressWarnings("NullableProblems")
 	@Override
 	public Iterator<Word> iterator() {
 		return this;

+ 7 - 8
hutool-json/src/test/java/cn/hutool/json/JSONUtilTest.java

@@ -1,19 +1,18 @@
 package cn.hutool.json;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Map;
-
-import cn.hutool.core.lang.Console;
-import org.junit.Assert;
-import org.junit.Test;
-
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.lang.Console;
 import cn.hutool.core.map.MapUtil;
 import cn.hutool.json.test.bean.Price;
 import cn.hutool.json.test.bean.UserA;
 import cn.hutool.json.test.bean.UserC;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
 
 public class JSONUtilTest {
 

+ 41 - 0
hutool-poi/src/main/java/cn/hutool/poi/word/PicType.java

@@ -0,0 +1,41 @@
+package cn.hutool.poi.word;
+
+import org.apache.poi.xwpf.usermodel.Document;
+
+/**
+ * Word中的图片类型
+ *
+ * @author looly
+ * @since 5.1.6
+ */
+public enum PicType {
+	EMF(Document.PICTURE_TYPE_EMF),
+	WMF(Document.PICTURE_TYPE_WMF),
+	PICT(Document.PICTURE_TYPE_PICT),
+	JPEG(Document.PICTURE_TYPE_JPEG),
+	PNG(Document.PICTURE_TYPE_PNG),
+	DIB(Document.PICTURE_TYPE_DIB),
+	GIF(Document.PICTURE_TYPE_GIF),
+	TIFF(Document.PICTURE_TYPE_TIFF),
+	EPS(Document.PICTURE_TYPE_EPS),
+	WPG(Document.PICTURE_TYPE_WPG);
+
+	/**
+	 * 构造
+	 * @param value 图片类型值
+	 */
+	PicType(int value) {
+		this.value = value;
+	}
+
+	private int value;
+
+	/**
+	 * 获取图片类型对应值
+	 *
+	 * @return 图片值
+	 */
+	public int getValue() {
+		return this.value;
+	}
+}

+ 1 - 1
hutool-poi/src/main/java/cn/hutool/poi/word/TableUtil.java

@@ -75,7 +75,7 @@ public class TableUtil {
 		if(rowBean instanceof Map) {
 			rowMap = (Map) rowBean;
 		} else if (BeanUtil.isBean(rowBean.getClass())) {
-			rowMap = BeanUtil.beanToMap(rowBean, new LinkedHashMap<String, Object>(), false, false);
+			rowMap = BeanUtil.beanToMap(rowBean, new LinkedHashMap<>(), false, false);
 		} else {
 			// 其它转为字符串默认输出
 			writeRow(row, CollUtil.newArrayList(rowBean), isWriteKeyAsHead);

+ 73 - 19
hutool-poi/src/main/java/cn/hutool/poi/word/Word07Writer.java

@@ -5,6 +5,8 @@ import cn.hutool.core.io.IORuntimeException;
 import cn.hutool.core.io.IoUtil;
 import cn.hutool.core.lang.Assert;
 import cn.hutool.core.util.ArrayUtil;
+import cn.hutool.poi.exceptions.POIException;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
 import org.apache.poi.xwpf.usermodel.XWPFDocument;
 import org.apache.poi.xwpf.usermodel.XWPFParagraph;
@@ -14,20 +16,25 @@ import java.awt.Font;
 import java.io.Closeable;
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 
 /**
  * Word生成器
- * 
+ *
  * @author looly
  * @since 4.4.1
  */
 public class Word07Writer implements Closeable {
 
 	private XWPFDocument doc;
-	/** 目标文件 */
+	/**
+	 * 目标文件
+	 */
 	protected File destFile;
-	/** 是否被关闭 */
+	/**
+	 * 是否被关闭
+	 */
 	protected boolean isClosed;
 
 	// -------------------------------------------------------------------------- Constructor start
@@ -37,7 +44,7 @@ public class Word07Writer implements Closeable {
 
 	/**
 	 * 构造
-	 * 
+	 *
 	 * @param destFile 写出的文件
 	 */
 	public Word07Writer(File destFile) {
@@ -46,7 +53,7 @@ public class Word07Writer implements Closeable {
 
 	/**
 	 * 构造
-	 * 
+	 *
 	 * @param doc {@link XWPFDocument}
 	 */
 	public Word07Writer(XWPFDocument doc) {
@@ -55,8 +62,8 @@ public class Word07Writer implements Closeable {
 
 	/**
 	 * 构造
-	 * 
-	 * @param doc {@link XWPFDocument}
+	 *
+	 * @param doc      {@link XWPFDocument}
 	 * @param destFile 写出的文件
 	 */
 	public Word07Writer(XWPFDocument doc, File destFile) {
@@ -68,7 +75,7 @@ public class Word07Writer implements Closeable {
 
 	/**
 	 * 获取{@link XWPFDocument}
-	 * 
+	 *
 	 * @return {@link XWPFDocument}
 	 */
 	public XWPFDocument getDoc() {
@@ -77,7 +84,7 @@ public class Word07Writer implements Closeable {
 
 	/**
 	 * 设置写出的目标文件
-	 * 
+	 *
 	 * @param destFile 目标文件
 	 * @return this
 	 */
@@ -88,8 +95,8 @@ public class Word07Writer implements Closeable {
 
 	/**
 	 * 增加一个段落
-	 * 
-	 * @param font 字体信息{@link Font}
+	 *
+	 * @param font  字体信息{@link Font}
 	 * @param texts 段落中的文本,支持多个文本作为一个段落
 	 * @return this
 	 */
@@ -99,9 +106,9 @@ public class Word07Writer implements Closeable {
 
 	/**
 	 * 增加一个段落
-	 * 
+	 *
 	 * @param align 段落对齐方式{@link ParagraphAlignment}
-	 * @param font 字体信息{@link Font}
+	 * @param font  字体信息{@link Font}
 	 * @param texts 段落中的文本,支持多个文本作为一个段落
 	 * @return this
 	 */
@@ -128,7 +135,7 @@ public class Word07Writer implements Closeable {
 
 	/**
 	 * 增加表格数据
-	 * 
+	 *
 	 * @param data 表格数据,多行数据。元素表示一行数据,当为集合或者数组时,为一行;当为Map或者Bean时key表示标题,values为数据
 	 * @return this
 	 * @since 4.5.16
@@ -139,10 +146,57 @@ public class Word07Writer implements Closeable {
 	}
 
 	/**
+	 * 增加图片,单独成段落
+	 *
+	 * @param picFile 图片文件
+	 * @param width   宽度
+	 * @param height  高度
+	 * @return this
+	 * @since 5.1.6
+	 */
+	public Word07Writer addPicture(File picFile, int width, int height) {
+		final String fileName = picFile.getName();
+		final String extName = FileUtil.extName(fileName).toUpperCase();
+		PicType picType;
+		try {
+			picType = PicType.valueOf(extName);
+		} catch (IllegalArgumentException e) {
+			// 默认值
+			picType = PicType.JPEG;
+		}
+		return addPicture(FileUtil.getInputStream(picFile), picType, fileName, width, height);
+	}
+
+	/**
+	 * 增加图片,单独成段落
+	 *
+	 * @param in       图片流
+	 * @param picType  图片类型,见Document.PICTURE_TYPE_XXX
+	 * @param fileName 文件名
+	 * @param width    宽度
+	 * @param height   高度
+	 * @return this
+	 * @since 5.1.6
+	 */
+	public Word07Writer addPicture(InputStream in, PicType picType, String fileName, int width, int height) {
+		final XWPFParagraph paragraph = doc.createParagraph();
+		final XWPFRun run = paragraph.createRun();
+		try {
+			run.addPicture(in, picType.getValue(), fileName, width, height);
+		} catch (InvalidFormatException e) {
+			throw new POIException(e);
+		} catch (IOException e) {
+			throw new IORuntimeException(e);
+		}
+
+		return this;
+	}
+
+	/**
 	 * 将Excel Workbook刷出到预定义的文件<br>
 	 * 如果用户未自定义输出的文件,将抛出{@link NullPointerException}<br>
 	 * 预定义文件可以通过{@link #setDestFile(File)} 方法预定义,或者通过构造定义
-	 * 
+	 *
 	 * @return this
 	 * @throws IORuntimeException IO异常
 	 */
@@ -153,7 +207,7 @@ public class Word07Writer implements Closeable {
 	/**
 	 * 将Excel Workbook刷出到文件<br>
 	 * 如果用户未自定义输出的文件,将抛出{@link NullPointerException}
-	 * 
+	 *
 	 * @param destFile 写出到的文件
 	 * @return this
 	 * @throws IORuntimeException IO异常
@@ -165,7 +219,7 @@ public class Word07Writer implements Closeable {
 
 	/**
 	 * 将Word Workbook刷出到输出流
-	 * 
+	 *
 	 * @param out 输出流
 	 * @return this
 	 * @throws IORuntimeException IO异常
@@ -176,8 +230,8 @@ public class Word07Writer implements Closeable {
 
 	/**
 	 * 将Word Document刷出到输出流
-	 * 
-	 * @param out 输出流
+	 *
+	 * @param out        输出流
 	 * @param isCloseOut 是否关闭输出流
 	 * @return this
 	 * @throws IORuntimeException IO异常