mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +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,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
|
||||
@ -138,11 +145,58 @@ 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>
|
||||
* 预定义文件可以通过{@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异常
|
||||
|
Loading…
Reference in New Issue
Block a user