java使用poi操作world生成饼图,柱状图,折线图,组合图:二
- 上文和问题链接
- 直接上代码
- maven
- 测试类:单图表(入口)
- 测试类:组合图表(入口)
- 工具类:组合数据类
- 工具类:枚举解析图表类
- 工具类:图片写入
上文和问题链接
上文链接:java使用poi操作world生成饼图,柱状图,折线图,组合图:一
问题链接:java poi 操作word遇到的问题
直接上代码
maven
<!--#############world操作所需依赖###########-->
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.13</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
</dependency>
测试类:单图表(入口)
可能遇到读取不到指定的图表----------更换第二个下标
chartModel.setSingleChart(charts,2,0,ChartsEnum.COLUMN);
import lombok.SneakyThrows;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import java.io.*;
import java.util.*;
public class Single {
public static void main(String[] args) throws IOException, InvalidFormatException {
InputStream docis = new FileInputStream("C:\\Users\\16630\\Desktop\\htmlToLongImage\\haha.docx");
CustomXWPFDocument doc = new CustomXWPFDocument(docis);
List<XWPFChart> charts = doc.getCharts();
ChartModel chartModel = new ChartModel();
List<String> titleList = new ArrayList<String>();
titleList.add("type");
titleList.add("number");
chartModel.setTitleList(titleList);
List<String> numberList = new ArrayList<String>();
numberList.add("value1");
numberList.add("value2");
chartModel.setNumberList(numberList);
List<Map<String, String>> sourceModelList = new ArrayList<>();
Map<String, String> publicEm = new HashMap<>();
publicEm.put("value1", "设计模式之禅");
publicEm.put("value2", "555");
Map<String, String> publicEm1 = new HashMap<>();
publicEm1.put("value1", "effective java");
publicEm1.put("value2", "453");
Map<String, String> publicEm2 = new HashMap<>();
publicEm2.put("value1", "红楼梦");
publicEm2.put("value2", "982");
Map<String, String> publicEm3 = new HashMap<>();
publicEm3.put("value1", "水浒传");
publicEm3.put("value2", "759");
sourceModelList.add(publicEm);
sourceModelList.add(publicEm1);
sourceModelList.add(publicEm2);
sourceModelList.add(publicEm3);
chartModel.setSourceModelList(sourceModelList);
chartModel.setSingleChart(charts,2,0,ChartsEnum.COLUMN);
chartModel.executeFillModel("sheet1");
chartModel.setSingleChart(charts,3,0,ChartsEnum.PIE);
chartModel.executeFillModel("sheet1");
chartModel.setSingleChart(charts,4,0,ChartsEnum.LINE_CHART);
chartModel.executeFillModel("sheet1");
try (FileOutputStream fos = new FileOutputStream("test.docx")) {
doc.write(fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试类:组合图表(入口)
第一个图表:
对应Map数据源中第一个key做X轴 第二个key做Y轴
第二个图表:
对应Map数据源中第一个key做X轴 第三个key做Y轴
import lombok.SneakyThrows;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import java.io.*;
import java.util.*;
public class Combination{
@SneakyThrows
public static void main(String[] args) {
InputStream docis = new FileInputStream("C:\\Users\\16630\\Desktop\\htmlToLongImage\\haha.docx");
CustomXWPFDocument doc = new CustomXWPFDocument(docis);
List<XWPFChart> charts = doc.getCharts();
ChartModel chartModel = new ChartModel();
List<String> titleList = new ArrayList<String>();
titleList.add("type");
titleList.add("number");
titleList.add("max");
chartModel.setTitleList(titleList);
List<String> numberList = new ArrayList<String>();
numberList.add("value1");
numberList.add("value2");
numberList.add("value3");
chartModel.setNumberList(numberList);
List<Map<String, String>> sourceModelList = new ArrayList<>();
Map<String, String> publicEm = new HashMap<>();
publicEm.put("value1", "设计模式之禅");
publicEm.put("value2", "555");
publicEm.put("value3", "0");
Map<String, String> publicEm1 = new HashMap<>();
publicEm1.put("value1", "effective java");
publicEm1.put("value2", "453");
publicEm1.put("value3", "0");
Map<String, String> publicEm2 = new HashMap<>();
publicEm2.put("value1", "红楼梦");
publicEm2.put("value2", "982");
publicEm2.put("value3", "982");
Map<String, String> publicEm3 = new HashMap<>();
publicEm3.put("value1", "水浒传");
publicEm3.put("value2", "759");
publicEm3.put("value3", "759");
sourceModelList.add(publicEm);
sourceModelList.add(publicEm1);
sourceModelList.add(publicEm2);
sourceModelList.add(publicEm3);
chartModel.setSourceModelList(sourceModelList);
chartModel.setComBiChart(charts,0,0,
Arrays.asList(ChartsEnum.LINE_CHART,ChartsEnum.COLUMN));
chartModel.executeFillModel("sheet1");
chartModel.setComBiChart(charts,1,0,
Arrays.asList(ChartsEnum.LINE_CHART,ChartsEnum.SCATTER));
chartModel.executeFillModel("sheet1");
try (FileOutputStream fos = new FileOutputStream("test.docx")) {
doc.write(fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
工具类:组合数据类
package com.wyz.world.utils;
import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
@Getter
@ToString
public class ChartModel {
private Boolean isSingle = true;
private String sheetName;
private XWPFChart xwpfChart;
private List<XmlObject> xmlObjectList = new ArrayList<>();
private CTChart ctChart;
private List<String> titleList;
private List<String> numberList;
private List<Map<String, String>> sourceModelList;
private List<ChartsEnum> chartsEnumList;
public void setNumberList(List<String> numberList){
this.numberList = numberList;
}
public void setSourceModelList(List<Map<String, String>> sourceModelList){
this.sourceModelList = sourceModelList;
}
public void setTitleList(List<String> titleList){
this.titleList = titleList;
}
public void setSingleChart(List<XWPFChart> charts,int chartSeat,int xmlObjSeat,ChartsEnum chartsEnum){
List<ChartsEnum> chartsEnumList = Arrays.asList(chartsEnum);
this.packageBasic(charts, chartSeat,chartsEnumList);
XmlObject targetChart = chartsEnum.getTargetChart(chartSeat, this.ctChart, xmlObjSeat);
this.xmlObjectList = Arrays.asList(targetChart);
this.isSingle = true;
}
public void setComBiChart(List<XWPFChart> charts,int chartSeat,int xmlObjSeat,List<ChartsEnum> chartsEnumList){
this.packageBasic(charts, chartSeat,chartsEnumList);
this.xmlObjectList.clear();
chartsEnumList.stream().forEach(x->{
XmlObject targetChart = x.getTargetChart(chartSeat,this.ctChart, xmlObjSeat);
this.xmlObjectList.add(targetChart);
});
this.isSingle = false;
}
private void packageBasic(List<XWPFChart> charts, int chartSeat,List<ChartsEnum> chartsEnumList) {
if(CollectionUtils.isEmpty(charts)){
throw new RuntimeException("模板中图表元素为null; !!!ctChart:null");
}
if(CollectionUtils.isEmpty(chartsEnumList)){
throw new RuntimeException("图表目标为null;!!!chartsEnum:null");
}
this.chartsEnumList = chartsEnumList;
this.xwpfChart = charts.get(chartSeat);
this.ctChart = this.xwpfChart.getCTChart();
}
public void executeFillModel(String sheetName) throws IOException, InvalidFormatException {
this.sheetName = sheetName;
String s = this.isSingle ? this.abnormalCheckSingle() : this.abnormalCheckComBi();
ChartsEnum.refreshExcel(this);
for (int i = 0; i < chartsEnumList.size(); i++) {
ChartsEnum chartsEnum = chartsEnumList.get(i);
chartsEnum.fillModel(this,this.getXmlObjectList().get(i),i);
}
}
private String abnormalCheckSingle() {
if(CollectionUtils.isEmpty(this.numberList)){
throw new RuntimeException("数据源比对为null; !!!numberList:null");
}
if(CollectionUtils.isEmpty(this.titleList)){
throw new RuntimeException("标题为null; !!!titleList:null");
}
if(CollectionUtils.isEmpty(this.sourceModelList)){
throw new RuntimeException("数据源为null; !!!sourceModelList:null");
}
if(Objects.isNull(this.xwpfChart)){
throw new RuntimeException("模板中图表元素为null; !!!xwpfChart:null");
}
if(CollectionUtils.isEmpty(this.xmlObjectList)){
throw new RuntimeException("模板中具体图表为null;!!!xmlObjectList:null");
}
if(CollectionUtils.isEmpty(this.chartsEnumList)){
throw new RuntimeException("图表目标为null;!!!chartsEnum:null");
}
if(Objects.isNull(this.ctChart)){
throw new RuntimeException("图表绘制区域为null;!!!chartsEnum:null");
}
if(StringUtils.isEmpty(this.sheetName)){
throw new RuntimeException("内置excel页名为null;!!!sheetName:null");
}
return null;
}
private String abnormalCheckComBi() {
this.abnormalCheckSingle();
if (this.xmlObjectList.size() < 2) {
throw new RuntimeException("组合图中【图表】元素不足两个; !!!xmlObjectList.size !> 2");
}
if (this.sourceModelList.stream().filter(x->{return x.keySet().size() >= 3;}).collect(Collectors.toList()).size() < 0) {
throw new RuntimeException("组合图中【数据源】元素不足两个; !!!sourceModelList.map.keySet.size !>= 3");
}
if (this.numberList.size() < 3) {
throw new RuntimeException("组合图中【数据源对应的key】元素不足两个; !!!numberList.size !>= 3");
}
return null;
}
}
工具类:枚举解析图表类
package com.wyz.world.utils;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.impl.values.XmlComplexContentImpl;
import org.openxmlformats.schemas.drawingml.x2006.chart.*;
import org.openxmlformats.schemas.drawingml.x2006.chart.impl.CTBarChartImpl;
import org.openxmlformats.schemas.drawingml.x2006.chart.impl.CTLineChartImpl;
import org.openxmlformats.schemas.drawingml.x2006.chart.impl.CTPieChartImpl;
import org.openxmlformats.schemas.drawingml.x2006.chart.impl.CTScatterChartImpl;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
@Slf4j
@Getter
public enum ChartsEnum {
PIE(CTPieChart.class, CTPieChartImpl.class){
@Override
public void fillModel(ChartModel chartModel,XmlObject xmlObject,int bias) {
if (!this.chartImplClazz.getName().equals(xmlObject.getClass().getName())) {
ChartsEnum.getEnumByChartImplClazz(((XmlComplexContentImpl)xmlObject).getClass())
.fillModel(chartModel, xmlObject, bias);
return;
}
CTPieChart pieChart = (CTPieChart)xmlObject;
List<CTPieSer> serList = pieChart.getSerList();
for (int i = 0; i < serList.size(); i++) {
CTPieSer ser = pieChart.getSerArray(i);
CTAxDataSource cat = ser.getCat();
CTNumDataSource val = ser.getVal();
this.dataAnalysisFill(chartModel,i,bias,cat,val);
}
}
@Override
public XmlObject getTargetChart(int chartSeat, CTChart ctChart, int xmlObjSeat) {
try {
CTPieChart pieChart = ctChart.getPlotArea().getPieChartArray(xmlObjSeat);
return pieChart;
}catch (Exception e){
throw new RuntimeException("当前位置【" + chartSeat + "】不存在【饼图】!!!");
}
}
},
COLUMN(CTBarChart.class, CTBarChartImpl.class) {
@Override
public void fillModel(ChartModel chartModel,XmlObject xmlObject,int bias) {
if (!this.chartImplClazz.getName().equals(xmlObject.getClass().getName())) {
ChartsEnum.getEnumByChartImplClazz(((XmlComplexContentImpl)xmlObject).getClass())
.fillModel(chartModel, xmlObject, bias);
return;
}
CTBarChart chart = (CTBarChart)xmlObject;
List<CTBarSer> serList = chart.getSerList();
for (int i = 0; i < serList.size(); i++) {
CTBarSer ser = chart.getSerArray(i);
CTAxDataSource cat = ser.getCat();
CTNumDataSource val = ser.getVal();
this.dataAnalysisFill(chartModel,i,bias,cat,val);
}
}
@Override
public XmlObject getTargetChart(int chartSeat, CTChart ctChart, int xmlObjSeat) {
try {
CTBarChart barChart = ctChart.getPlotArea().getBarChartArray(xmlObjSeat);
return barChart;
}catch (Exception e){
throw new RuntimeException("当前位置【" + chartSeat + "】不存在【柱状图】!!!");
}
}
},
LINE_CHART(CTLineChart.class, CTLineChartImpl.class){
@Override
public void fillModel(ChartModel chartModel,XmlObject xmlObject,int bias) {
if (!this.chartImplClazz.getName().equals(xmlObject.getClass().getName())) {
ChartsEnum.getEnumByChartImplClazz(((XmlComplexContentImpl)xmlObject).getClass())
.fillModel(chartModel, xmlObject, bias);
return;
}
CTLineChart chart = (CTLineChart)xmlObject;
List<CTLineSer> serList = chart.getSerList();
for (int i = 0; i < serList.size(); i++) {
CTLineSer ser = chart.getSerArray(i);
CTAxDataSource cat = ser.getCat();
CTNumDataSource val = ser.getVal();
this.dataAnalysisFill(chartModel,i,bias,cat,val);
}
}
@Override
public XmlObject getTargetChart(int chartSeat, CTChart ctChart, int xmlObjSeat) {
try {
CTLineChart lineChart = ctChart.getPlotArea().getLineChartArray(xmlObjSeat);
return lineChart;
}catch (Exception e){
throw new RuntimeException("当前位置【" + chartSeat + "】不存在【折线图】!!!");
}
}
},
SCATTER(CTScatterChart.class, CTScatterChartImpl.class){
@Override
public void fillModel(ChartModel chartModel,XmlObject xmlObject,int bias) {
if (!this.chartImplClazz.getName().equals(xmlObject.getClass().getName())) {
ChartsEnum.getEnumByChartImplClazz(((XmlComplexContentImpl)xmlObject).getClass())
.fillModel(chartModel, xmlObject, bias);
return;
}
CTScatterChart chart = (CTScatterChart)xmlObject;
List<CTScatterSer> serList = chart.getSerList();
for (int i = 0; i < serList.size(); i++) {
CTScatterSer ser = chart.getSerArray(i);
CTAxDataSource cat = ser.getXVal();
CTNumDataSource val = ser.getYVal();
this.dataAnalysisFill(chartModel,i,bias,cat,val);
}
}
@Override
public XmlObject getTargetChart(int chartSeat, CTChart ctChart, int xmlObjSeat) {
try {
CTScatterChart scatterChart = ctChart.getPlotArea().getScatterChartArray(xmlObjSeat);
return scatterChart;
}catch (Exception e){
throw new RuntimeException("当前位置【" + chartSeat + "】不存在【散点图】!!!");
}
}
},
;
public Class<? extends XmlObject> chartClazz;
public Class<? extends XmlComplexContentImpl> chartImplClazz;
ChartsEnum(Class<? extends XmlObject> chartClazz,
Class<? extends XmlComplexContentImpl> chartImplClazz){
this.chartClazz = chartClazz;
this.chartImplClazz = chartImplClazz;
}
public abstract void fillModel(ChartModel chartModel,XmlObject xmlObject,int bias);
public abstract XmlObject getTargetChart(int chartSeat,CTChart ctChart,int xmlObjSeat);
public static ChartsEnum getEnumByChartImplClazz(Class<? extends XmlComplexContentImpl> chartImplClazz){
for (ChartsEnum value : ChartsEnum.values()) {
if (value.getChartImplClazz().equals(chartImplClazz)){
return value;
}
}
return null;
}
public static ChartsEnum getEnumByChartClazz(Class<? extends XmlObject> chartClazz){
for (ChartsEnum value : ChartsEnum.values()) {
if (value.getChartClazz().equals(chartClazz)){
return value;
}
}
return null;
}
public static boolean refreshExcel(ChartModel chartModel) throws IOException, InvalidFormatException {
List<String> titleList = chartModel.getTitleList();
List<String> numberList = chartModel.getNumberList();
List<Map<String, String>> sourceModelList = chartModel.getSourceModelList();
XWPFChart xwpfChart = chartModel.getXwpfChart();
boolean result = true;
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet(chartModel.getSheetName());
for (int i = 0; i < titleList.size(); i++) {
if (sheet.getRow(0) == null) {
sheet.createRow(0).createCell(i).setCellValue(titleList.get(i) == null ? "" : titleList.get(i));
} else {
sheet.getRow(0).createCell(i).setCellValue(titleList.get(i) == null ? "" : titleList.get(i));
}
}
for (int i = 0; i < sourceModelList.size(); i++) {
Map<String, String> baseFormMap = sourceModelList.get(i);
for (int j = 0; j < numberList.size(); j++) {
if (sheet.getRow(i + 1) == null) {
if (j == 0) {
try {
sheet.createRow(i + 1)
.createCell(j)
.setCellValue(baseFormMap.get(numberList.get(j)) == null ?
"" : baseFormMap.get(numberList.get(j)));
} catch (Exception e) {
if (baseFormMap.get(numberList.get(j)) == null) {
sheet.createRow(i + 1).createCell(j).setCellValue("");
} else {
sheet.createRow(i + 1)
.createCell(j)
.setCellValue(baseFormMap.get(numberList.get(j)));
}
}
}
} else {
BigDecimal b = new BigDecimal(baseFormMap.get(numberList.get(j)));
double value = 0D;
if (b != null) {
value = b.doubleValue();
}
if (value == 0D) {
sheet.getRow(i + 1).createCell(j);
} else {
sheet.getRow(i + 1).createCell(j).setCellValue(b.doubleValue());
}
}
}
}
List<POIXMLDocumentPart> pxdList = xwpfChart.getRelations();
if(pxdList!=null&&pxdList.size()>0) {
for (int i = 0; i < pxdList.size(); i++) {
if (pxdList.get(i).toString().contains("sheet")) {
POIXMLDocumentPart xlsPart = xwpfChart.getRelations().get(0);
OutputStream xlsOut = xlsPart.getPackagePart().getOutputStream();
try {
wb.write(xlsOut);
xlsOut.close();
} catch (IOException e) {
e.printStackTrace();
result = false;
} finally {
if (wb != null) {
try {
wb.close();
} catch (IOException e) {
e.printStackTrace();
result = false;
}
}
}
}
}
}
return result;
}
public void dataAnalysisFill (ChartModel chartModel,
int i,
int bias,
CTAxDataSource cat,
CTNumDataSource val) {
List<Map<String, String>> sourceModelList = chartModel.getSourceModelList();
List<String> numberList = chartModel.getNumberList();
CTStrData strData = cat.getStrRef().getStrCache();
CTNumData numData = val.getNumRef().getNumCache();
long idx = 0;
for (int j = 0; j < sourceModelList.size(); j++) {
String value = "0";
if (new BigDecimal(sourceModelList.get(j).get(numberList.get(i + 1))) != null) {
value = new BigDecimal(sourceModelList.get(j).get(numberList.get(i + 1))).toString();
}
if (!"0".equals(value)) {
CTNumVal numVal = numData.addNewPt();
numVal.setIdx(idx);
numVal.setV(value);
}
CTStrVal sVal = strData.addNewPt();
sVal.setIdx(idx);
sVal.setV(sourceModelList.get(j).get(numberList.get(0)));
idx++;
}
numData.getPtCount().setVal(idx);
strData.getPtCount().setVal(idx);
String axisDataRange = new
CellRangeAddress(1, sourceModelList.size(), 0, 0)
.formatAsString(chartModel.getSheetName(), false);
cat.getStrRef().setF(axisDataRange);
String numDataRange = new
CellRangeAddress(1, sourceModelList.size(), i + 1 + bias, i + 1 + bias)
.formatAsString(chartModel.getSheetName(), false);
val.getNumRef().setF(numDataRange);
}
}
工具类:图片写入
package com.wyz.world.utils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlToken;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class CustomXWPFDocument extends XWPFDocument{
public CustomXWPFDocument(InputStream in) throws IOException {
super(in);
}
public CustomXWPFDocument() {
super();
}
public CustomXWPFDocument(OPCPackage pkg) throws IOException {
super(pkg);
}
public void createPicture(int id, int width, int height,
XWPFParagraph paragraph) {
final int EMU = 9525;
width *= EMU;
height *= EMU;
String blipId = super.getRelationId(super.getAllPictures().get(id));
CTInline inline = paragraph.createRun().getCTR().addNewDrawing()
.addNewInline();
String picXml = ""
+ "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">"
+ " <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"
+ " <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"
+ " <pic:nvPicPr>" + " <pic:cNvPr id=\""
+ id
+ "\" name=\"Generated\"/>"
+ " <pic:cNvPicPr/>"
+ " </pic:nvPicPr>"
+ " <pic:blipFill>"
+ " <a:blip r:embed=\""
+ blipId
+ "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>"
+ " <a:stretch>"
+ " <a:fillRect/>"
+ " </a:stretch>"
+ " </pic:blipFill>"
+ " <pic:spPr>"
+ " <a:xfrm>"
+ " <a:off x=\"0\" y=\"0\"/>"
+ " <a:ext cx=\""
+ width
+ "\" cy=\""
+ height
+ "\"/>"
+ " </a:xfrm>"
+ " <a:prstGeom prst=\"rect\">"
+ " <a:avLst/>"
+ " </a:prstGeom>"
+ " </pic:spPr>"
+ " </pic:pic>"
+ " </a:graphicData>" + "</a:graphic>";
inline.addNewGraphic().addNewGraphicData();
XmlToken xmlToken = null;
try {
xmlToken = XmlToken.Factory.parse(picXml);
} catch (XmlException xe) {
xe.printStackTrace();
}
inline.set(xmlToken);
inline.setDistT(0);
inline.setDistB(0);
inline.setDistL(0);
inline.setDistR(0);
CTPositiveSize2D extent = inline.addNewExtent();
extent.setCx(width);
extent.setCy(height);
CTNonVisualDrawingProps docPr = inline.addNewDocPr();
docPr.setId(id);
docPr.setName("图片名称");
docPr.setDescr("描述信息");
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)