1. 程式人生 > >Apache POI XWPF 爬坑指南之二特定位置插入表格、段落、圖片

Apache POI XWPF 爬坑指南之二特定位置插入表格、段落、圖片

alloc ktr bubuko cdn filepath 年齡 rac stream oid

轉載: https://www.jianshu.com/p/de58ab550157

技術分享圖片


package com.yxg;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.List;


import org.apache.poi.POIXMLDocument;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.xmlbeans.XmlCursor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;



public class TestWord {

public static void main(String[] args) throws IOException, InvalidFormatException {
String filepath = "C:\\Users\\Administration\\Desktop\\test.docx";
String destpath = "C:\\Users\\Administration\\Desktop\\test_result.docx";


OPCPackage opcPackage = POIXMLDocument.openPackage(filepath);
XWPFDocument xwpfDocument = new XWPFDocument(opcPackage);
List<XWPFParagraph> xwpfParas = xwpfDocument.getParagraphs();
int num=0;
for(int i=0;i<xwpfParas.size();i++){
if(num==3) break;
XWPFParagraph xwpfParagraph = xwpfParas.get(i);
String text=xwpfParagraph.getText();


//插入段落
if(text.equals("${mark_newParagraph}")){
XmlCursor cursor = xwpfParagraph .getCTP().newCursor();
XWPFParagraph newPara = xwpfDocument.insertNewParagraph(cursor);
newPara.setAlignment(ParagraphAlignment.BOTH);//兩端對齊
newPara.setIndentationFirstLine(480);//首行縮進24磅
XWPFRun newParaRun = newPara.createRun();
newParaRun.setText("這是新插入的段落!");
newParaRun.setFontFamily("宋體");
newParaRun.setFontSize(12);
newParaRun.setBold(false);
xwpfDocument.removeBodyElement(xwpfDocument.getPosOfParagraph(xwpfParagraph));
}


//插入表格
if(text.equals("${mark_newTable}")){
XmlCursor cursor= xwpfParagraph.getCTP().newCursor();
XWPFTable table = xwpfDocument.insertNewTbl(cursor);


XWPFTableRow row_0 = table.getRow(0);
row_0.getCell(0).setText("姓名");
row_0.addNewTableCell().setText("年齡");


XWPFTableRow row_1 = table.createRow();
row_1.getCell(0).setText("隔壁老王");
row_1.getCell(1).setText("48");


setTableLocation(table,"center");
setCellLocation(table,"CENTER","center");
xwpfDocument.removeBodyElement(xwpfDocument.getPosOfParagraph(xwpfParagraph));
}


//插入圖片
if(text.equals("${mark_newPicture}")){


XmlCursor cursor = xwpfParagraph .getCTP().newCursor();
XWPFParagraph newPara = xwpfDocument.insertNewParagraph(cursor);
newPara.setAlignment(ParagraphAlignment.CENTER);//居中
XWPFRun newParaRun = newPara.createRun();

URL url = new URL("https://gd1.alicdn.com/imgextra/i2/758877415/O1CN0124e7IIJkzKJrYwp_!!758877415.jpg");
newParaRun.addPicture(url.openStream(),XWPFDocument.PICTURE_TYPE_PNG,"bus.png,",Units.toEMU(200), Units.toEMU(200));
xwpfDocument.removeBodyElement(xwpfDocument.getPosOfParagraph(xwpfParagraph));
}
}


write(xwpfDocument,destpath);
}



private static void write(XWPFDocument document, String destpath) {
try {
OutputStream stream = new FileOutputStream(destpath);
document.write(stream);
stream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}



/**
* 設置單元格水平位置和垂直位置
*
* @param xwpfTable
* @param verticalLoction 單元格中內容垂直上TOP,下BOTTOM,居中CENTER,BOTH兩端對齊
* @param horizontalLocation 單元格中內容水平居中center,left居左,right居右,both兩端對齊
*/
public static void setCellLocation(XWPFTable xwpfTable, String verticalLoction, String horizontalLocation) {
List<XWPFTableRow> rows = xwpfTable.getRows();
for (XWPFTableRow row : rows) {
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
CTTc cttc = cell.getCTTc();
CTP ctp = cttc.getPList().get(0);
CTPPr ctppr = ctp.getPPr();
if (ctppr == null) {
ctppr = ctp.addNewPPr();
}
CTJc ctjc = ctppr.getJc();
if (ctjc == null) {
ctjc = ctppr.addNewJc();
}
ctjc.setVal(STJc.Enum.forString(horizontalLocation)); //水平居中
cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.valueOf(verticalLoction));//垂直居中
}
}
}


/**
* 設置表格位置
*
* @param xwpfTable
* @param location 整個表格居中center,left居左,right居右,both兩端對齊
*/
public static void setTableLocation(XWPFTable xwpfTable, String location) {
CTTbl cttbl = xwpfTable.getCTTbl();
CTTblPr tblpr = cttbl.getTblPr() == null ? cttbl.addNewTblPr() : cttbl.getTblPr();
CTJc cTJc = tblpr.addNewJc();
cTJc.setVal(STJc.Enum.forString(location));
}


}

 

Apache POI XWPF 爬坑指南之二特定位置插入表格、段落、圖片