POI WORD

package des.tiny.imonline.test;

import java.io.*;
import java.math.BigInteger;
import java.util.*;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

public class Test2 {
	public static void getWordXAndStyle(InputStream in,String fileName,String path,String type) throws Exception {
		//${biaoji}
		XWPFDocument docx =  new XWPFDocument(new FileInputStream("E:/simple.docx"));
	    
		List<Object> l = new ArrayList<Object>();
		
	    Iterator<IBodyElement> iBody = docx.getBodyElementsIterator();
	    int curT = 0;//当前操作对象的索引
	    int curP = 0;//当前操作对象的索引
	    //htmlText = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title></title></head><body>";
	    String htmlText = "";
	    while(iBody.hasNext()){
	        IBodyElement body = iBody.next();
	        if(BodyElementType.TABLE.equals(body.getElementType())){//处理表格
	            XWPFTable table = body.getBody().getTableArray(curT);
	            List<XWPFTable> tables = body.getBody().getTables();
	            table = tables.get(curT);
	            l.add(table);
	        }else if(BodyElementType.PARAGRAPH.equals(body.getElementType())){//处理段落
	            XWPFParagraph ph = body.getBody().getParagraphArray(curP);
	            l.add(ph);
	        }
	    }
	    
	    XWPFDocument doc = new XWPFDocument();
	    int i = 0;
	    for(Object o:l){
	    	if(o instanceof XWPFTable){
	    		i++;
	    		XWPFTable table = (XWPFTable)o;
	    		doc.setTable(i, table);
	    	}else if(o instanceof XWPFParagraph){
	    		i++;
	    		XWPFParagraph ph = (XWPFParagraph)o;
	    		doc.setParagraph(ph, i);
	    	}
	    }
	    
	    FileOutputStream out = new FileOutputStream("E:\\test.docx");
        
        doc.write(out);
        out.close();

	}

	    public static String readTableX(XWPFTable tb) throws Exception {
	    	String htmlTextTbl = null;
	    List<XWPFTableRow> rows = tb.getRows();
	    //遍历行
	    for(XWPFTableRow row:rows){
	        //int rowHight = row.getHeight();
	        String tr = "";
	        List<XWPFTableCell> cells = row.getTableCells();
	        //遍历列
	        for(XWPFTableCell cell:cells){
	            String text = "";
	            List<XWPFParagraph> graphs = cell.getParagraphs();
	            //遍历段落
	            for(XWPFParagraph pg:graphs){
	                text = text+pg.getText()+"<br/>";
	            }
	            tr += "<td>"+text+"</td>";
	        }
	        htmlTextTbl += "<tr>"+tr+"</tr>";
	    }
	    htmlTextTbl = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" class=\"tbl2\">"+htmlTextTbl+"</table><br/>";
	    return htmlTextTbl;
	}

	    public static String readParagraphX(XWPFParagraph p) throws Exception {
	    String tempStr = "";
	    String text = p.getText();
	    if(text == null||"".equals(text)){
	        tempStr = tempStr + "<br/>";
	    }else{
	        tempStr = tempStr+"<span>"+text+"</span><br/>";
	    }
	    return tempStr;
	}
	    
    public static void main(String[] args) throws Exception {
    	//${biaoji}
		XWPFDocument docx =  new XWPFDocument(new FileInputStream("E:/simple.docx"));
	    
		List<Object> l = new ArrayList<Object>();
		
	    Iterator<IBodyElement> iBody = docx.getBodyElementsIterator();
	    int curT = 0;//当前操作对象的索引
	    int curP = 0;//当前操作对象的索引
	    //htmlText = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title></title></head><body>";
	    while(iBody.hasNext()){
	        IBodyElement body = iBody.next();
	        if(BodyElementType.TABLE.equals(body.getElementType())){//处理表格
	            XWPFTable table = body.getBody().getTableArray(curT);
	            List<XWPFTable> tables = body.getBody().getTables();
	            table = tables.get(curT);
	            l.add(table);
	            curP++;
	        }else if(BodyElementType.PARAGRAPH.equals(body.getElementType())){//处理段落
	            XWPFParagraph ph = body.getBody().getParagraphArray(curP);
	            if("${biaoji}".equals(ph.getText())){
	            	l.add("${biaoji}");
	            	curP++;
	            	continue;
	            }

	            l.add(ph);
	            curP++;
	        }
	    }
	    
	    XWPFDocument doc = new XWPFDocument();
	    int i = 0;
	    int j = 0;
	    for(Object o:l){
	    	if(o instanceof XWPFTable){
	    		XWPFTable table = (XWPFTable)o;
	    		doc.createTable();
	    		doc.setTable(i, table);
	    		i++;
	    	}else if(o instanceof XWPFParagraph){
	    		XWPFParagraph ph = (XWPFParagraph)o;
	    		doc.createParagraph();
	    		doc.setParagraph(ph, j);
	    		j++;
	    	}else if(o instanceof String){
	    		    XWPFTable table = doc.createTable(11, 2);
	    		    
	    		    CTTblPr tblPr = table.getCTTbl().getTblPr();
	    		    tblPr.getTblW().setType(STTblWidth.DXA);
	    		    tblPr.getTblW().setW(new BigInteger("8000"));
	    		    
	    		    // 设置上下左右四个方向的距离,可以将表格撑大
	    		    //上左下右边
	    		    table.setCellMargins(10, 10, 10, 10);
	    		    // table.set
	    		    List<XWPFTableCell> tableCells = table.getRow(0).getTableCells();
	    		    tableCells.get(0).setText("第一行第一列的数据第一");
	    		    
	    		    CTTcPr tcpr = tableCells.get(0).getCTTc().addNewTcPr();
	    		    CTTblWidth ceelw = tcpr.addNewTcW();
	    		    ceelw.setType(STTblWidth.DXA);
	    		    ceelw.setW(BigInteger.valueOf(1000));
	    		    
	    		    CTTcPr tcpr1 = tableCells.get(1).getCTTc().addNewTcPr();
	    		    CTTblWidth ceelw1 = tcpr1.addNewTcW();
	    		    ceelw1.setType(STTblWidth.DXA);
	    		    ceelw1.setW(BigInteger.valueOf(2000));
	    		    tableCells.get(1).setText("第一行第二列的数据的数11111111");
	    		    
	    		    XWPFParagraph actTheme = doc.createParagraph();//设置活动主题
	    	        actTheme.setAlignment(ParagraphAlignment.RIGHT);
	    	        actTheme.setVerticalAlignment(TextAlignment.CENTER);
	    	        XWPFRun runText1=actTheme.createRun();
	    	        runText1.setText("活动主题:20劵优惠劵活动");
	    	        //runText1.setFontSize(15);
	    		    
	    		    tableCells.get(1).setParagraph(actTheme);
	    		    
	    		   
	    		    
	    		    List<XWPFTableCell> tableCellsq = table.getRow(1).getTableCells();
	    		    tableCellsq.get(0).setText("第二行第二列的数据");
	    		    tableCellsq.get(1).setText("第二行第二列的数据");
	    		i++;
	    	}
	    }
	    
	    FileOutputStream out = new FileOutputStream("E:\\test.docx");
        
        doc.write(out);
        out.close();
	}
}


POI WORD_第1张图片


POI WORD_第2张图片

你可能感兴趣的:(POI WORD)