This commit is contained in:
Looly 2020-03-02 21:13:04 +08:00
parent 985ffb33a5
commit ba8d7b2625
6 changed files with 124 additions and 28 deletions

View File

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

View File

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

View File

@ -1,19 +1,18 @@
package cn.hutool.json; 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.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
import cn.hutool.json.test.bean.Price; import cn.hutool.json.test.bean.Price;
import cn.hutool.json.test.bean.UserA; import cn.hutool.json.test.bean.UserA;
import cn.hutool.json.test.bean.UserC; 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 { public class JSONUtilTest {

View 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;
}
}

View File

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

View File

@ -5,6 +5,8 @@ import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil; 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.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFParagraph;
@ -14,20 +16,25 @@ import java.awt.Font;
import java.io.Closeable; import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
/** /**
* Word生成器 * Word生成器
* *
* @author looly * @author looly
* @since 4.4.1 * @since 4.4.1
*/ */
public class Word07Writer implements Closeable { public class Word07Writer implements Closeable {
private XWPFDocument doc; private XWPFDocument doc;
/** 目标文件 */ /**
* 目标文件
*/
protected File destFile; protected File destFile;
/** 是否被关闭 */ /**
* 是否被关闭
*/
protected boolean isClosed; protected boolean isClosed;
// -------------------------------------------------------------------------- Constructor start // -------------------------------------------------------------------------- Constructor start
@ -37,7 +44,7 @@ public class Word07Writer implements Closeable {
/** /**
* 构造 * 构造
* *
* @param destFile 写出的文件 * @param destFile 写出的文件
*/ */
public Word07Writer(File destFile) { public Word07Writer(File destFile) {
@ -46,7 +53,7 @@ public class Word07Writer implements Closeable {
/** /**
* 构造 * 构造
* *
* @param doc {@link XWPFDocument} * @param doc {@link XWPFDocument}
*/ */
public Word07Writer(XWPFDocument doc) { public Word07Writer(XWPFDocument doc) {
@ -55,8 +62,8 @@ public class Word07Writer implements Closeable {
/** /**
* 构造 * 构造
* *
* @param doc {@link XWPFDocument} * @param doc {@link XWPFDocument}
* @param destFile 写出的文件 * @param destFile 写出的文件
*/ */
public Word07Writer(XWPFDocument doc, File destFile) { public Word07Writer(XWPFDocument doc, File destFile) {
@ -68,7 +75,7 @@ public class Word07Writer implements Closeable {
/** /**
* 获取{@link XWPFDocument} * 获取{@link XWPFDocument}
* *
* @return {@link XWPFDocument} * @return {@link XWPFDocument}
*/ */
public XWPFDocument getDoc() { public XWPFDocument getDoc() {
@ -77,7 +84,7 @@ public class Word07Writer implements Closeable {
/** /**
* 设置写出的目标文件 * 设置写出的目标文件
* *
* @param destFile 目标文件 * @param destFile 目标文件
* @return this * @return this
*/ */
@ -88,8 +95,8 @@ public class Word07Writer implements Closeable {
/** /**
* 增加一个段落 * 增加一个段落
* *
* @param font 字体信息{@link Font} * @param font 字体信息{@link Font}
* @param texts 段落中的文本支持多个文本作为一个段落 * @param texts 段落中的文本支持多个文本作为一个段落
* @return this * @return this
*/ */
@ -99,9 +106,9 @@ public class Word07Writer implements Closeable {
/** /**
* 增加一个段落 * 增加一个段落
* *
* @param align 段落对齐方式{@link ParagraphAlignment} * @param align 段落对齐方式{@link ParagraphAlignment}
* @param font 字体信息{@link Font} * @param font 字体信息{@link Font}
* @param texts 段落中的文本支持多个文本作为一个段落 * @param texts 段落中的文本支持多个文本作为一个段落
* @return this * @return this
*/ */
@ -128,7 +135,7 @@ public class Word07Writer implements Closeable {
/** /**
* 增加表格数据 * 增加表格数据
* *
* @param data 表格数据多行数据元素表示一行数据当为集合或者数组时为一行当为Map或者Bean时key表示标题values为数据 * @param data 表格数据多行数据元素表示一行数据当为集合或者数组时为一行当为Map或者Bean时key表示标题values为数据
* @return this * @return this
* @since 4.5.16 * @since 4.5.16
@ -138,11 +145,58 @@ public class Word07Writer implements Closeable {
return this; 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> * 将Excel Workbook刷出到预定义的文件<br>
* 如果用户未自定义输出的文件将抛出{@link NullPointerException}<br> * 如果用户未自定义输出的文件将抛出{@link NullPointerException}<br>
* 预定义文件可以通过{@link #setDestFile(File)} 方法预定义或者通过构造定义 * 预定义文件可以通过{@link #setDestFile(File)} 方法预定义或者通过构造定义
* *
* @return this * @return this
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
*/ */
@ -153,7 +207,7 @@ public class Word07Writer implements Closeable {
/** /**
* 将Excel Workbook刷出到文件<br> * 将Excel Workbook刷出到文件<br>
* 如果用户未自定义输出的文件将抛出{@link NullPointerException} * 如果用户未自定义输出的文件将抛出{@link NullPointerException}
* *
* @param destFile 写出到的文件 * @param destFile 写出到的文件
* @return this * @return this
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
@ -165,7 +219,7 @@ public class Word07Writer implements Closeable {
/** /**
* 将Word Workbook刷出到输出流 * 将Word Workbook刷出到输出流
* *
* @param out 输出流 * @param out 输出流
* @return this * @return this
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
@ -176,8 +230,8 @@ public class Word07Writer implements Closeable {
/** /**
* 将Word Document刷出到输出流 * 将Word Document刷出到输出流
* *
* @param out 输出流 * @param out 输出流
* @param isCloseOut 是否关闭输出流 * @param isCloseOut 是否关闭输出流
* @return this * @return this
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常