您的位置:首页 > 其它

POI WORD

2015-12-03 00:11 686 查看
//${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);
curT++;
}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("第一行"+(char)11+"第一列的数据第一");

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);

String str = "活动主题:\r\n20劵优惠\r\n劵活动";
System.out.println(Arrays.asList(str.split("\r\n")));
for(String s:str.split("\r\n")){
XWPFRun runText1=actTheme.createRun();
runText1.setText(s);
runText1.addBreak();
}

//runText1.setFontSize(15);

tableCells.get(1).setParagraph(actTheme);

List<XWPFTableCell> tableCellsq = table.getRow(1).getTableCells();
tableCellsq.get(0).setText("第二行第二列的数据");
tableCellsq.get(1).setText("第二行第二列的数据");
i++;*/

//XWPFParagraph ph = (XWPFParagraph)o;
XWPFTable table = doc.createTable(1, 1);
System.out.println(table.getWidth());
CTTblBorders borders=table.getCTTbl().getTblPr().addNewTblBorders();
CTBorder hBorder=borders.addNewInsideH();
hBorder.setVal(STBorder.Enum.forString("none"));
hBorder.setSz(new BigInteger("1"));
hBorder.setColor("0000FF");

CTBorder vBorder=borders.addNewInsideV();
vBorder.setVal(STBorder.Enum.forString("none"));
vBorder.setSz(new BigInteger("1"));
vBorder.setColor("00FF00");

CTBorder lBorder=borders.addNewLeft();
lBorder.setVal(STBorder.Enum.forString("none"));
lBorder.setSz(new BigInteger("1"));
lBorder.setColor("3399FF");

CTBorder rBorder=borders.addNewRight();
rBorder.setVal(STBorder.Enum.forString("none"));
rBorder.setSz(new BigInteger("1"));
rBorder.setColor("F2B11F");

CTBorder tBorder=borders.addNewTop();
tBorder.setVal(STBorder.Enum.forString("none"));
tBorder.setSz(new BigInteger("1"));
tBorder.setColor("C3599D");

CTBorder bBorder=borders.addNewBottom();
bBorder.setVal(STBorder.Enum.forString("none"));
bBorder.setSz(new BigInteger("1"));
bBorder.setColor("F7E415");
CTTcPr tcpr = table.getRow(0).getTableCells().get(0).getCTTc().addNewTcPr();
CTTblWidth ceelw = tcpr.addNewTcW();
ceelw.setType(STTblWidth.DXA);

ceelw.setW(BigInteger.valueOf(18000));
XWPFParagraph ph = doc.createParagraph();
String str = "活动主题:\r\n20劵优惠\r\n劵活动";
System.out.println(Arrays.asList(str.split("\r\n")));
for(String s:str.split("\r\n")){
XWPFRun runText1=ph.createRun();
runText1.setText(s);
runText1.addBreak();
}
ph.setAlignment(ParagraphAlignment.CENTER);
table.getRow(0).getCell(0).setParagraph(ph);
/*doc.setParagraph(ph, j);*/
table.setCellMargins(150, 13000, 150, 150);
XWPFParagraph ph1 = doc.createParagraph();
XWPFRun runText1=ph1.createRun();
runText1.setText("测试1");
runText1.addBreak();
XWPFRun runText2=ph1.createRun();
runText2.setBold(true);
runText2.setText("测试1测试1测试1测试1测试1");
//runText2.setSubscript(VerticalAlign.);
runText2.addBreak();

ph1.setAlignment(ParagraphAlignment.RIGHT);

i++;
break;
}
}

FileOutputStream out = new FileOutputStream("E:\\test.docx");

doc.write(out);
out.close();

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  POI WORD 换行