org.apache.poi.xwpf.usermodel.XWPFDocument类的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(11.2k)|赞(0)|评价(0)|浏览(1963)

本文整理了Java中org.apache.poi.xwpf.usermodel.XWPFDocument类的一些代码示例,展示了XWPFDocument类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。XWPFDocument类的具体详情如下:
包路径:org.apache.poi.xwpf.usermodel.XWPFDocument
类名称:XWPFDocument

XWPFDocument介绍

[英]Experimental class to do low level processing of docx files. If you're using these low level classes, then you will almost certainly need to refer to the OOXML specifications from http://www.ecma-international.org/publications/standards/Ecma-376.htm WARNING - APIs expected to change rapidly
[中]实验班做docx文件的底层处理。如果使用这些低级类,那么几乎肯定需要参考http://www.ecma-international.org/publications/standards/Ecma-376.htm警告-预计API将迅速变化

代码示例

代码示例来源:origin: stackoverflow.com

XWPFDocument document = new XWPFDocument();
XWPFParagraph tmpParagraph = document.createParagraph();
XWPFRun tmpRun = tmpParagraph.createRun();
tmpRun.setText("LALALALAALALAAAA");
tmpRun.setFontSize(18);
document.write(new FileOutputStream(new File("yourpathhere")));
document.close();

代码示例来源:origin: stackoverflow.com

XWPFDocument doc = new XWPFDocument(OPCPackage.open("input.docx"));
for (XWPFParagraph p : doc.getParagraphs()) {
  List<XWPFRun> runs = p.getRuns();
  if (runs != null) {
    for (XWPFRun r : runs) {
      String text = r.getText(0);
      if (text != null && text.contains("needle")) {
        text = text.replace("needle", "haystack");
        r.setText(text, 0);
      }
    }
  }
}
for (XWPFTable tbl : doc.getTables()) {
  for (XWPFTableRow row : tbl.getRows()) {
   for (XWPFTableCell cell : row.getTableCells()) {
     for (XWPFParagraph p : cell.getParagraphs()) {
      for (XWPFRun r : p.getRuns()) {
       String text = r.getText(0);
       if (text.contains("needle")) {
        text = text.replace("needle", "haystack");
        r.setText(text);
       }
      }
     }
   }
  }
}
doc.write(new FileOutputStream("output.docx"));

代码示例来源:origin: org.apache.poi/poi-ooxml

/**
 *
 */
public void createTOC() {
  CTSdtBlock block = this.getDocument().getBody().addNewSdt();
  TOC toc = new TOC(block);
  for (XWPFParagraph par : paragraphs) {
    String parStyle = par.getStyle();
    if (parStyle != null && parStyle.startsWith("Heading")) {
      try {
        int level = Integer.parseInt(parStyle.substring("Heading".length()));
        toc.addRow(level, par.getText(), 1, "112723803");
      } catch (NumberFormatException e) {
        LOG.log(POILogger.ERROR, "can't format number in TOC heading", e);
      }
    }
  }
}

代码示例来源:origin: org.apache.poi/poi-ooxml

private void setFooterReference(Enum type, XWPFHeaderFooter wrapper) {
  CTHdrFtrRef ref = doc.getDocument().getBody().getSectPr().addNewFooterReference();
  ref.setType(type);
  ref.setId(doc.getRelationId(wrapper));
}

代码示例来源:origin: stackoverflow.com

final XWPFDocument docx = new XWPFDocument(new FileInputStream(new File(inFileNameString)));
final FileOutputStream out = new FileOutputStream(outFileNameString); docx.write(out);
out.close();
docx.close();

代码示例来源:origin: stackoverflow.com

String outpath = "F:\\Test.docx";
XWPFDocument doc = new XWPFDocument(new FileInputStream(filepath));
for (XWPFParagraph p : doc.getParagraphs()){
  int numberOfRuns = p.getRuns().size();
  for (XWPFRun r : p.getRuns()){
    int pos = r.getTextPosition();
    if(r.getText(pos) != null) {
      sb.append(r.getText(pos));
      p.removeRun(i);
doc.write(new FileOutputStream(outpath));

代码示例来源:origin: stackoverflow.com

XWPFDocument wordDocument = new XWPFDocument(new FileInputStream(docxFile));
POIXMLProperties props = workDocument.getProperties();

String thumbnail = props.getThumbnailFilename();
if (thumbnail == null) {
  // No thumbnail
} else {
  FileOutputStream fos = new FileOutputStream("c:\\temp\\"+thumbnail);
  IOUtils.copy(props.getThumbnailImage(), fos);
}

代码示例来源:origin: stackoverflow.com

XWPFDocument doc = new XWPFDocument();
 XWPFParagraph title = doc.createParagraph();    
 XWPFRun run = title.createRun();
 run.setText("Fig.1 A Natural Scene");
 run.setBold(true);
 title.setAlignment(ParagraphAlignment.CENTER);
 String imgFile = "encabezado.jpg"
 FileInputStream is = new FileInputStream(imgFile);
 run.addBreak();
 run.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, imgFile, Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels
 is.close();
 FileOutputStream fos = new FileOutputStream("test4.docx");
 doc.write(fos);
 fos.close();

代码示例来源:origin: stackoverflow.com

XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraphOne = document.createParagraph();
XWPFRun paragraphOneRunOne = paragraphOne.createRun();
paragraphOneRunOne.setBold(true);
paragraphOneRunOne.setItalic(true);
paragraphOneRunOne.setText("Hello world! This is paragraph one!");
paragraphOneRunOne.addBreak();
  FileOutputStream outStream = new FileOutputStream("D:\\Users\\user2777005\\Desktop\\MyDov.docx");
  document.write(outStream);
  outStream.close();
} catch (IOException e) {
  e.printStackTrace();
XWPFDocument doc = new XWPFDocument(is);
List<XWPFParagraph> paras = doc.getParagraphs(); //This list will hold the paragraphs
XWPFWordExtractor ex = new XWPFWordExtractor(doc);  //To get the words
String words = ""; //This will hold all the text
  for(XWPFParagraph p : paras){  //For each paragraph we retrieved from the document
   words += p.getText();    //Add the text we retrieve to the words string  
  XWPFDocument newDoc = new XWPFDocument(); 
  XWPFParagraph para = newDoc.createParagraph();
  XWPFRun run = para.createRun();     
  run.setText(words);
  newDoc.write(new FileOutputStream(new File("D:\\Users\\user2777005\\Desktop\\mydoc.docx")));}
  catch (IOException e)
{System.out.println(e);}

代码示例来源:origin: stackoverflow.com

XWPFDocument doc = new XWPFDocument(new FileInputStream("source.docx"));
XWPFDocument destDoc = new XWPFDocument();
OutputStream out = new FileOutputStream("Destination.docx");
XWPFParagraph pr = doc.getParagraphs().get(0);
destDoc.createParagraph();
int pos = destDoc.getParagraphs().size() - 1;
destDoc.setParagraph(pr, pos);
destDoc.write(out);

代码示例来源:origin: stackoverflow.com

public static void main(String[] args) throws Exception {
  FileOutputStream fos = new FileOutputStream(new File("transformed.docx"));
  XWPFDocument doc = new XWPFDocument(new FileInputStream(new File("original.docx")));
  for(XWPFParagraph p:doc.getParagraphs()){
    for(XWPFRun r:p.getRuns()){
      for(CTText ct:r.getCTR().getTList()){
        String str = ct.getStringValue();
        if(str.contains("NAME")){
          str = str.replace("NAME", "Java Dev");
          ct.setStringValue(str);
        }
      }
    }
  }
  doc.write(fos);
}

代码示例来源:origin: stackoverflow.com

FileInputStream fis = new FileInputStream("document.docx");
XWPFDocument doc = new XWPFDocument(fis);
List<XWPFTable> tables = doc.getTables();
XWPFTable table = tables.get(0);
XWPFTableRow[] rows = table.getRows().toArray(new XWPFTableRow[0]);
for (int r = 0; r < rows.length; r++) {
 if (r > 0) {
 XWPFTableRow row = rows[r];
 table.removeRow(1); //remove second row. others shift upwards
 table.createRow(); //add new row at the end
doc.write(new FileOutputStream("new document.docx"));

代码示例来源:origin: stackoverflow.com

XWPFDocument wordDocument = new XWPFDocument(new FileInputStream(docxFile));

ArrayList<PackagePart> packageParts= wordDocument.getPackage().getPartsByRelationshipType
("http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail");

PackagePart packagePart = packageParts.get(0);
FileOutputStream fos = new FileOutputStream("c:\\temp\\thumb.emf");
IOUtils.copy(packagePart.getInputStream(), fos);

代码示例来源:origin: stackoverflow.com

InputStream inputstream = new FileInputStream(m_filepath); 
//read the file 
XWPFDocument adoc= new XWPFDocument(inputstream);
//and place it in a xwpf format

aString = new XWPFWordExtractor(adoc).getText();           
//gets the full text

代码示例来源:origin: stackoverflow.com

XWPFDocument doc = new XWPFDocument(new FileInputStream(fileName));
     List<XWPFTable> table = doc.getTables();
     for (XWPFTable xwpfTable : table) {
       List<XWPFTableRow> row = xwpfTable.getRows();
       for (XWPFTableRow xwpfTableRow : row) {
         List<XWPFTableCell> cell = xwpfTableRow.getTableCells();
         for (XWPFTableCell xwpfTableCell : cell) {
           if (xwpfTableCell != null) {
             System.out.println(xwpfTableCell.getText());
             String s = xwpfTableCell.getText();
             for (XWPFParagraph p : xwpfTableCell.getParagraphs()) {
               for (XWPFRun run : p.getRuns()) {
                 for (XWPFPicture pic : run.getEmbeddedPictures()) {
                   byte[] pictureData = pic.getPictureData().getData();
                   System.out.println("picture : " + pictureData);
                 }
               }
             }
           }
         }
       }
     }

代码示例来源:origin: stackoverflow.com

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  public void readDocxFile() {
      try {
        File file = new File("C:/NetBeans Output/documentx.docx");
        FileInputStream fis = new FileInputStream(file.getAbsolutePath());

        XWPFDocument document = new XWPFDocument(fis);

        List<XWPFParagraph> paragraphs = document.getParagraphs();

        for (XWPFParagraph para : paragraphs) {
          System.out.println(para.getText());
        }
        fis.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

代码示例来源:origin: stackoverflow.com

XWPFDocument document = new XWPFDocument();
FileOutputStream out = new FileOutputStream(new File("create_table.docx"));
XWPFParagraph para = document.createParagraph();
XWPFRun run = para.createRun();
run.setText("Hi");
XWPFTable table = document.createTable();
XWPFTableRow tableRowOne = table.getRow(0);
tableRowOne.getCell(0).setText("col one, row one");
tableRowOne.addNewTableCell().setText("col two, row one");
tableRowOne.addNewTableCell().setText("col three, row one");
XWPFTableRow tableRowThree = table.createRow();
tableRowThree.getCell(0).setText("col one, row three");
tableRowThree.getCell(1).setText("col two, row three");
XWPFParagraph para2 = document.createParagraph();
XWPFRun run2 = para2.createRun();
run2.setText("Bye");
document.write(out);
out.close();
System.out.println("create_table.docx written successully");

代码示例来源:origin: stackoverflow.com

public static void main(String[] args) throws IOException {
   XWPFDocument document = new XWPFDocument();
   XWPFTable tableOne = document.createTable();
   XWPFTableRow tableOneRowOne = tableOne.getRow(0);
   tableOneRowOne.getCell(0).setText("Header1");
   tableOneRowOne.addNewTableCell().setText("header2");
   XWPFTableRow tableOneRowTwo = tableOne.createRow();
   tableOneRowTwo.getCell(0).setText("Data1");
   tableOneRowTwo.getCell(1).setText("Data2");
   FileOutputStream outStream = new FileOutputStream("test.doc");
   document.write(outStream);
   outStream.close();
 }

代码示例来源:origin: stackoverflow.com

//create word doc
XWPFDocument doc = new XWPFDocument();
// create para and run
XWPFParagraph para= doc.createParagraph();    
XWPFRun run = para.createRun();

para.setAlignment(ParagraphAlignment.CENTER);

// convert buffered image to Input Stream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(screenFullImage, "jpeg", baos);
baos.flush();
ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
baos.close();

// add image to word doc
run.addBreak();
run.addPicture(bis, XWPFDocument.PICTURE_TYPE_JPEG, "image file", Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels
bis.close();
// write word doc to file
FileOutputStream fos = new FileOutputStream("WordDocWithImage.docx");
doc.write(fos);
fos.close();

代码示例来源:origin: stackoverflow.com

public class Bill {

  public static void writeDate(XWPFDocument document) {

    SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy         hh:mm:ss");
    String date = sdf.format(new Date()); 

    XWPFParagraph paragraph = document.createParagraph();
    XWPFRun run = paragraph.createRun();
    run.setText(date);
  }

  public static void main(String[] args) throws Exception {

    XWPFDocument document = new XWPFDocument();
    writeDate(document);
    FileOutputStream out = new FileOutputStream("createparagraph.docx");
    document.write(out);
    out.close();
  }
}

相关文章

微信公众号

最新文章

更多

XWPFDocument类方法