mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add pic
This commit is contained in:
parent
985ffb33a5
commit
ba8d7b2625
@ -16,6 +16,7 @@
|
||||
* 【core 】 增加EnumUtil.getEnumAt方法
|
||||
* 【core 】 增强EnumConvert判断能力(issue#I17082@Gitee)
|
||||
* 【all 】 log、template、tokenizer使用SPI机制代替硬编码
|
||||
* 【poi 】 Word07Writer增加addPicture
|
||||
|
||||
### Bug修复
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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
hutool-poi/src/main/java/cn/hutool/poi/word/PicType.java
Normal file
41
hutool-poi/src/main/java/cn/hutool/poi/word/PicType.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
@ -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,6 +16,7 @@ import java.awt.Font;
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
@ -25,9 +28,13 @@ import java.io.OutputStream;
|
||||
public class Word07Writer implements Closeable {
|
||||
|
||||
private XWPFDocument doc;
|
||||
/** 目标文件 */
|
||||
/**
|
||||
* 目标文件
|
||||
*/
|
||||
protected File destFile;
|
||||
/** 是否被关闭 */
|
||||
/**
|
||||
* 是否被关闭
|
||||
*/
|
||||
protected boolean isClosed;
|
||||
|
||||
// -------------------------------------------------------------------------- Constructor start
|
||||
@ -56,7 +63,7 @@ public class Word07Writer implements Closeable {
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param doc {@link XWPFDocument}
|
||||
* @param doc {@link XWPFDocument}
|
||||
* @param destFile 写出的文件
|
||||
*/
|
||||
public Word07Writer(XWPFDocument doc, File destFile) {
|
||||
@ -89,7 +96,7 @@ public class Word07Writer implements Closeable {
|
||||
/**
|
||||
* 增加一个段落
|
||||
*
|
||||
* @param font 字体信息{@link Font}
|
||||
* @param font 字体信息{@link Font}
|
||||
* @param texts 段落中的文本,支持多个文本作为一个段落
|
||||
* @return this
|
||||
*/
|
||||
@ -101,7 +108,7 @@ public class Word07Writer implements Closeable {
|
||||
* 增加一个段落
|
||||
*
|
||||
* @param align 段落对齐方式{@link ParagraphAlignment}
|
||||
* @param font 字体信息{@link Font}
|
||||
* @param font 字体信息{@link Font}
|
||||
* @param texts 段落中的文本,支持多个文本作为一个段落
|
||||
* @return this
|
||||
*/
|
||||
@ -138,6 +145,53 @@ public class Word07Writer implements Closeable {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加图片,单独成段落
|
||||
*
|
||||
* @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>
|
||||
@ -177,7 +231,7 @@ public class Word07Writer implements Closeable {
|
||||
/**
|
||||
* 将Word Document刷出到输出流
|
||||
*
|
||||
* @param out 输出流
|
||||
* @param out 输出流
|
||||
* @param isCloseOut 是否关闭输出流
|
||||
* @return this
|
||||
* @throws IORuntimeException IO异常
|
||||
|
Loading…
x
Reference in New Issue
Block a user