Skip to content

Commit ab9c4a5

Browse files
committed
复杂的word操作工具类
1 parent 814af07 commit ab9c4a5

File tree

5 files changed

+227
-3
lines changed

5 files changed

+227
-3
lines changed

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,12 @@
867867
<version>1.6.4</version>
868868
</dependency>
869869

870+
<dependency>
871+
<groupId>jfree</groupId>
872+
<artifactId>jfreechart</artifactId>
873+
<version>1.0.13</version>
874+
</dependency>
875+
870876
<dependency>
871877
<groupId>jdk.tools</groupId>
872878
<artifactId>jdk.tools</artifactId>

src/main/java/com/caozj/framework/util/office/WordUtil.java

+56
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package com.caozj.framework.util.office;
22

3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.Map;
6+
7+
import org.apache.commons.io.FileUtils;
8+
9+
import com.caozj.framework.util.freemarker.FreemarkerUtil;
10+
import com.caozj.model.constant.ConstantData;
11+
12+
import freemarker.core.ParseException;
13+
import freemarker.template.MalformedTemplateNameException;
14+
import freemarker.template.TemplateException;
15+
import freemarker.template.TemplateNotFoundException;
16+
317
/**
418
* 复杂的word操作工具类(包括使用freemark语法解析word,插入表格,插入图表,插入图片等)
519
*
@@ -8,4 +22,46 @@
822
*/
923
public class WordUtil {
1024

25+
/**
26+
* 使用freemarker语法解析word(word必须先使用xml格式另存为)
27+
*
28+
* @param content word文件的xml格式内容
29+
* @param param 参数
30+
* @return
31+
* @throws TemplateNotFoundException
32+
* @throws MalformedTemplateNameException
33+
* @throws ParseException
34+
* @throws IOException
35+
* @throws TemplateException
36+
*/
37+
public static String parseByFreemarker(String content, Map<String, Object> param)
38+
throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException,
39+
TemplateException {
40+
content = content.replaceAll("<", "&1lt;").replaceAll(">", "&1gt;");
41+
content = content.replaceAll("&lt;", "<").replaceAll("&gt;", ">");
42+
String word = FreemarkerUtil.parseContent(content, param);
43+
word = word.replaceAll("&1lt;", "<").replaceAll("&1gt;", ">");
44+
return word;
45+
}
46+
47+
/**
48+
* 使用freemarker语法解析word(word必须先使用xml格式另存为)
49+
*
50+
* @param inWord xml格式的输入word
51+
* @param outWord xml格式的输出word
52+
* @param param 参数
53+
* @throws TemplateNotFoundException
54+
* @throws MalformedTemplateNameException
55+
* @throws ParseException
56+
* @throws IOException
57+
* @throws TemplateException
58+
*/
59+
public static void parseByFreemarker(File inWord, File outWord, Map<String, Object> param)
60+
throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException,
61+
TemplateException {
62+
String content = FileUtils.readFileToString(inWord, ConstantData.DEFAULT_CHARSET);
63+
String word = parseByFreemarker(content, param);
64+
FileUtils.writeStringToFile(outWord, word);
65+
}
66+
1167
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.caozj.test;
2+
3+
import java.awt.Color;
4+
import java.awt.Font;
5+
import java.io.ByteArrayInputStream;
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.FileOutputStream;
8+
import java.text.DecimalFormat;
9+
import java.text.NumberFormat;
10+
11+
import org.apache.poi.xwpf.usermodel.XWPFDocument;
12+
import org.apache.poi.xwpf.usermodel.XWPFTable;
13+
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
14+
import org.jfree.chart.ChartFactory;
15+
import org.jfree.chart.ChartUtilities;
16+
import org.jfree.chart.JFreeChart;
17+
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
18+
import org.jfree.chart.plot.PiePlot;
19+
import org.jfree.chart.plot.PlotOrientation;
20+
import org.jfree.data.category.DefaultCategoryDataset;
21+
import org.jfree.data.general.DefaultPieDataset;
22+
23+
public class CreateTablesWithPOI {
24+
public static void main(String[] args) {
25+
String outputFile = "D:\\test222.docx";
26+
XWPFDocument document = new XWPFDocument();
27+
XWPFTable tableOne = document.createTable();
28+
XWPFTableRow tableOneRowOne = tableOne.getRow(0);
29+
tableOneRowOne.getCell(0).setText("第1行第1列");
30+
tableOneRowOne.addNewTableCell().setText("第1行第2列");
31+
tableOneRowOne.addNewTableCell().setText("第1行第3列");
32+
tableOneRowOne.addNewTableCell().setText("第1行第4列");
33+
XWPFTableRow tableOneRowTwo = tableOne.createRow();
34+
tableOneRowTwo.getCell(0).setText("第2行第1列");
35+
tableOneRowTwo.getCell(1).setText("第2行第2列");
36+
tableOneRowTwo.getCell(2).setText("第2行第3列");
37+
FileOutputStream fOut;
38+
try {
39+
fOut = new FileOutputStream(outputFile);
40+
ByteArrayInputStream in = getPieChartImage();
41+
String ind = document.addPictureData(in, XWPFDocument.PICTURE_TYPE_JPEG);
42+
System.out.println("pic ID=" + ind);
43+
// document.createPicture(document.getAllPictures().size() - 1, 200, 200, " ");
44+
// 放第二张图
45+
ind = document.addPictureData(getBarChartImage(), XWPFDocument.PICTURE_TYPE_JPEG);
46+
System.out.println("pic ID=" + ind);
47+
// document.createPicture(document.getAllPictures().size() - 1, 200, 200, " ");
48+
document.write(fOut);
49+
fOut.flush();
50+
// 操作结束,关闭文件
51+
fOut.close();
52+
} catch (Exception e) {
53+
e.printStackTrace();
54+
}
55+
}
56+
57+
public static ByteArrayInputStream getPieChartImage() {
58+
ByteArrayInputStream in = null;
59+
DefaultPieDataset pieDataset = new DefaultPieDataset();
60+
pieDataset.setValue(" 北京局 ", 20);
61+
pieDataset.setValue(" 上海局 ", 18);
62+
pieDataset.setValue(" 天津局 ", 16);
63+
pieDataset.setValue(" 重庆局 ", 15);
64+
pieDataset.setValue(" 山东局 ", 45);
65+
JFreeChart chart = ChartFactory.createPieChart3D(" 企业备案图 ", pieDataset, true, false, false);
66+
// 设置标题字体样式
67+
chart.getTitle().setFont(new Font(" 黑体 ", Font.BOLD, 20));
68+
// 设置饼状图里描述字体样式
69+
PiePlot piePlot = (PiePlot) chart.getPlot();
70+
piePlot.setLabelFont(new Font(" 黑体 ", Font.BOLD, 10));
71+
// 设置显示百分比样式
72+
piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator((" {0}({2}) "),
73+
NumberFormat.getNumberInstance(), new DecimalFormat(" 0.00% ")));
74+
// 设置统计图背景
75+
piePlot.setBackgroundPaint(Color.white);
76+
// 设置图片最底部字体样式
77+
chart.getLegend().setItemFont(new Font(" 黑体 ", Font.BOLD, 10));
78+
try {
79+
ByteArrayOutputStream out = new ByteArrayOutputStream();
80+
ChartUtilities.writeChartAsPNG(out, chart, 400, 300);
81+
in = new ByteArrayInputStream(out.toByteArray());
82+
} catch (Exception e) {
83+
e.printStackTrace();
84+
}
85+
return in;
86+
}
87+
88+
public static ByteArrayInputStream getBarChartImage() {
89+
ByteArrayInputStream in = null;
90+
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
91+
dataset.addValue(100, "Spring Security", "Jan");
92+
dataset.addValue(200, "jBPM 4", "Jan");
93+
dataset.addValue(300, "Ext JS", "Jan");
94+
dataset.addValue(400, "JFreeChart", "Jan");
95+
JFreeChart chart = ChartFactory.createBarChart("chart", "num", "type", dataset,
96+
PlotOrientation.VERTICAL, true, false, false);
97+
// 设置标题字体样式
98+
chart.getTitle().setFont(new Font(" 黑体 ", Font.BOLD, 20));
99+
// 设置饼状图里描述字体样式
100+
// 设置图片最底部字体样式
101+
chart.getLegend().setItemFont(new Font(" 黑体 ", Font.BOLD, 10));
102+
try {
103+
ByteArrayOutputStream out = new ByteArrayOutputStream();
104+
ChartUtilities.writeChartAsPNG(out, chart, 400, 300);
105+
in = new ByteArrayInputStream(out.toByteArray());
106+
} catch (Exception e) {
107+
e.printStackTrace();
108+
}
109+
return in;
110+
}
111+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.caozj.test;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
import com.caozj.framework.util.office.WordUtil;
11+
12+
import freemarker.core.ParseException;
13+
import freemarker.template.MalformedTemplateNameException;
14+
import freemarker.template.TemplateException;
15+
import freemarker.template.TemplateNotFoundException;
16+
17+
public class TWordXML {
18+
19+
public static void main(String[] args) throws TemplateNotFoundException,
20+
MalformedTemplateNameException, ParseException, IOException, TemplateException {
21+
String file = "D:/test.xml";
22+
Map<String, Object> param = new HashMap<String, Object>();
23+
List<String> list = new ArrayList<>();
24+
list.add("测试1");
25+
list.add("测试2");
26+
list.add("测试3");
27+
list.add("测试4");
28+
list.add("测试5");
29+
list.add("测试6" + System.currentTimeMillis());
30+
param.put("list", list);
31+
param.put("pageType", "a");
32+
WordUtil.parseByFreemarker(new File(file), new File("D:/444.xml"), param);
33+
}
34+
35+
}
+19-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
package com.caozj.test;
22

3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
7+
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
8+
import org.apache.poi.xwpf.usermodel.XWPFDocument;
9+
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
10+
import org.apache.poi.xwpf.usermodel.XWPFTable;
11+
312
public class TestWord {
413

5-
//table \ charts
6-
public static void main(String[] args) {
14+
// table \ charts
15+
public static void main(String[] args) throws InvalidFormatException, FileNotFoundException {
716
String wordFile = "d://test.docx";
8-
17+
int picWidth = 240;
18+
int picHeight = 150;
19+
int picType = XWPFDocument.PICTURE_TYPE_JPEG;
20+
XWPFParagraph paragraph = null;
21+
XWPFTable table = null;
22+
XWPFDocument doc = new XWPFDocument();
23+
paragraph = doc.createParagraph();
24+
doc.addPictureData(new FileInputStream(new File("F:\\test.bmp")), picType);
925
}
1026

1127
}

0 commit comments

Comments
 (0)