mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-12-07 01:28:34 +08:00
add FontStyle
This commit is contained in:
parent
c34bdaf776
commit
b3d17e70ae
@ -0,0 +1,57 @@
|
||||
package cn.hutool.v7.poi.word;
|
||||
|
||||
import org.apache.poi.xwpf.usermodel.XWPFRun;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
|
||||
/**
|
||||
* 字体样式
|
||||
*
|
||||
* @param font 字体信息
|
||||
* @param color 字体颜色
|
||||
* @author looly
|
||||
* @since 7.0.0
|
||||
*/
|
||||
public record FontStyle(Font font, Color color) {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param name 字体名称
|
||||
* @param style 字体样式,见{@link Font#PLAIN}, {@link Font#BOLD}, {@link Font#ITALIC}
|
||||
* @param size 字体大小
|
||||
*/
|
||||
@SuppressWarnings("MagicConstant")
|
||||
public FontStyle(final String name, final int style, final int size) {
|
||||
this(new Font(name, style, size), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param name 字体名称
|
||||
* @param style 字体样式,见{@link Font#PLAIN}, {@link Font#BOLD}, {@link Font#ITALIC}
|
||||
* @param size 字体大小
|
||||
* @param color 字体颜色
|
||||
*/
|
||||
@SuppressWarnings("MagicConstant")
|
||||
public FontStyle(final String name, final int style, final int size, final Color color) {
|
||||
this(new Font(name, style, size), color);
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充字体样式到段落
|
||||
*
|
||||
* @param run 段落对象
|
||||
*/
|
||||
public void fill(final XWPFRun run) {
|
||||
run.setFontFamily(font.getFamily());
|
||||
run.setFontSize(font.getSize());
|
||||
run.setBold(font.isBold());
|
||||
run.setItalic(font.isItalic());
|
||||
if (null != color) {
|
||||
run.setColor(String.format("%02X", color.getRGB()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -16,13 +16,13 @@
|
||||
|
||||
package cn.hutool.v7.poi.word;
|
||||
|
||||
import org.apache.poi.common.usermodel.PictureType;
|
||||
import cn.hutool.v7.core.io.file.FileUtil;
|
||||
import cn.hutool.v7.core.array.ArrayUtil;
|
||||
import cn.hutool.v7.core.io.IORuntimeException;
|
||||
import cn.hutool.v7.core.io.IoUtil;
|
||||
import cn.hutool.v7.core.io.file.FileUtil;
|
||||
import cn.hutool.v7.core.lang.Assert;
|
||||
import cn.hutool.v7.core.array.ArrayUtil;
|
||||
import cn.hutool.v7.poi.POIException;
|
||||
import org.apache.poi.common.usermodel.PictureType;
|
||||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||
import org.apache.poi.util.Units;
|
||||
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
|
||||
@ -30,7 +30,6 @@ import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFRun;
|
||||
|
||||
import java.awt.Font;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
@ -51,7 +50,7 @@ public class Word07Writer implements Closeable {
|
||||
*/
|
||||
protected boolean isClosed;
|
||||
|
||||
// region ----- Constructor start
|
||||
// region ----- Constructor
|
||||
|
||||
/**
|
||||
* 构造
|
||||
@ -111,14 +110,15 @@ public class Word07Writer implements Closeable {
|
||||
return this;
|
||||
}
|
||||
|
||||
// region ----- addText
|
||||
/**
|
||||
* 增加一个段落
|
||||
*
|
||||
* @param font 字体信息{@link Font}
|
||||
* @param font 字体信息{@link FontStyle}
|
||||
* @param texts 段落中的文本,支持多个文本作为一个段落
|
||||
* @return this
|
||||
*/
|
||||
public Word07Writer addText(final Font font, final String... texts) {
|
||||
public Word07Writer addText(final FontStyle font, final String... texts) {
|
||||
return addText(null, font, texts);
|
||||
}
|
||||
|
||||
@ -126,11 +126,11 @@ public class Word07Writer implements Closeable {
|
||||
* 增加一个段落
|
||||
*
|
||||
* @param align 段落对齐方式{@link ParagraphAlignment}
|
||||
* @param font 字体信息{@link Font}
|
||||
* @param font 字体信息{@link FontStyle}
|
||||
* @param texts 段落中的文本,支持多个文本作为一个段落
|
||||
* @return this
|
||||
*/
|
||||
public Word07Writer addText(final ParagraphAlignment align, final Font font, final String... texts) {
|
||||
public Word07Writer addText(final ParagraphAlignment align, final FontStyle font, final String... texts) {
|
||||
final XWPFParagraph p = this.doc.createParagraph();
|
||||
if (null != align) {
|
||||
p.setAlignment(align);
|
||||
@ -141,15 +141,13 @@ public class Word07Writer implements Closeable {
|
||||
run = p.createRun();
|
||||
run.setText(text);
|
||||
if (null != font) {
|
||||
run.setFontFamily(font.getFamily());
|
||||
run.setFontSize(font.getSize());
|
||||
run.setBold(font.isBold());
|
||||
run.setItalic(font.isItalic());
|
||||
font.fill(run);
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
// endregion
|
||||
|
||||
/**
|
||||
* 增加表格数据
|
||||
@ -164,6 +162,7 @@ public class Word07Writer implements Closeable {
|
||||
return this;
|
||||
}
|
||||
|
||||
// region ----- addPicture
|
||||
/**
|
||||
* 增加图片,单独成段落
|
||||
*
|
||||
@ -249,7 +248,9 @@ public class Word07Writer implements Closeable {
|
||||
}
|
||||
return this;
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region ----- flush
|
||||
/**
|
||||
* 将Excel Workbook刷出到预定义的文件<br>
|
||||
* 如果用户未自定义输出的文件,将抛出{@link NullPointerException}<br>
|
||||
@ -308,6 +309,7 @@ public class Word07Writer implements Closeable {
|
||||
}
|
||||
return this;
|
||||
}
|
||||
// endregion
|
||||
|
||||
/**
|
||||
* 关闭Word文档<br>
|
||||
|
||||
@ -40,8 +40,8 @@ public class WordWriterTest {
|
||||
@Disabled
|
||||
public void writeTest() {
|
||||
final Word07Writer writer = new Word07Writer();
|
||||
writer.addText(new Font("方正小标宋简体", Font.PLAIN, 22), "我是第一部分", "我是第二部分");
|
||||
writer.addText(new Font("宋体", Font.PLAIN, 22), "我是正文第一部分", "我是正文第二部分");
|
||||
writer.addText(new FontStyle("方正小标宋简体", Font.PLAIN, 22), "我是第一部分", "我是第二部分");
|
||||
writer.addText(new FontStyle("宋体", Font.PLAIN, 22), "我是正文第一部分", "我是正文第二部分");
|
||||
writer.flush(FileUtil.file("e:/wordWrite.docx"));
|
||||
writer.close();
|
||||
Console.log("OK");
|
||||
@ -60,7 +60,7 @@ public class WordWriterTest {
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void writeTableTest(){
|
||||
public void writeTableTest() {
|
||||
final Word07Writer writer = new Word07Writer();
|
||||
final Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("姓名", "张三");
|
||||
@ -94,9 +94,9 @@ public class WordWriterTest {
|
||||
final ArrayList<Map<String, Object>> mapArrayList = ListUtil.of(data, data2);
|
||||
|
||||
// 添加段落(标题)
|
||||
writer.addText(new Font("方正小标宋简体", Font.PLAIN, 22), "我是第一部分");
|
||||
writer.addText(new FontStyle("方正小标宋简体", Font.PLAIN, 22), "我是第一部分");
|
||||
// 添加段落(正文)
|
||||
writer.addText(new Font("宋体", Font.PLAIN, 13), "我是正文第一部分");
|
||||
writer.addText(new FontStyle("宋体", Font.PLAIN, 13), "我是正文第一部分");
|
||||
writer.addTable(mapArrayList);
|
||||
// 写出到文件
|
||||
writer.flush(FileUtil.file("d:/test/a.docx"));
|
||||
@ -105,7 +105,7 @@ public class WordWriterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overflowTest(){
|
||||
public void overflowTest() {
|
||||
final Word07Writer word07Writer = new Word07Writer();
|
||||
final List<Object> list = ListUtil.of(false);
|
||||
final List<Object> list2 = ListUtil.of(false);
|
||||
@ -117,23 +117,23 @@ public class WordWriterTest {
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void writeBeanAsTableTest(){
|
||||
public void writeBeanAsTableTest() {
|
||||
final List<Vo> of = ListUtil.of(
|
||||
new Vo("测试1", new BigDecimal(12), new BigDecimal(2)),
|
||||
new Vo("测试2", new BigDecimal(13), new BigDecimal(2)),
|
||||
new Vo("测试3", new BigDecimal(15), new BigDecimal(3)),
|
||||
new Vo("测试4", new BigDecimal(112), new BigDecimal(5))
|
||||
new Vo("测试1", new BigDecimal(12), new BigDecimal(2)),
|
||||
new Vo("测试2", new BigDecimal(13), new BigDecimal(2)),
|
||||
new Vo("测试3", new BigDecimal(15), new BigDecimal(3)),
|
||||
new Vo("测试4", new BigDecimal(112), new BigDecimal(5))
|
||||
);
|
||||
|
||||
WordUtil.getWriter()
|
||||
.addTable(of)
|
||||
.flush(FileUtil.file("d:/test/beanValueTest.docx"))
|
||||
.close();
|
||||
.addTable(of)
|
||||
.flush(FileUtil.file("d:/test/beanValueTest.docx"))
|
||||
.close();
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
private static class Vo{
|
||||
private static class Vo {
|
||||
private String name;
|
||||
private BigDecimal amount;
|
||||
private BigDecimal onYear;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user