org.apache.poi.xwpf.usermodel.XWPFDocument.write()方法的使用及代码示例

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

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

XWPFDocument.write介绍

暂无

代码示例

代码示例来源:origin: looly/hutool

/**
 * 将Word Document刷出到输出流
 * 
 * @param out 输出流
 * @param isCloseOut 是否关闭输出流
 * @return this
 * @throws IORuntimeException IO异常
 */
public Word07Writer flush(OutputStream out, boolean isCloseOut) throws IORuntimeException {
  Assert.isFalse(this.isClosed, "WordWriter has been closed!");
  try {
    this.doc.write(out);
    out.flush();
  } catch (IOException e) {
    throw new IORuntimeException(e);
  } finally {
    if (isCloseOut) {
      IoUtil.close(out);
    }
  }
  return this;
}

代码示例来源:origin: looly/hutool

/**
 * 将Word Document刷出到输出流
 * 
 * @param out 输出流
 * @param isCloseOut 是否关闭输出流
 * @return this
 * @throws IORuntimeException IO异常
 */
public Word07Writer flush(OutputStream out, boolean isCloseOut) throws IORuntimeException {
  Assert.isFalse(this.isClosed, "WordWriter has been closed!");
  try {
    this.doc.write(out);
    out.flush();
  } catch (IOException e) {
    throw new IORuntimeException(e);
  } finally {
    if (isCloseOut) {
      IoUtil.close(out);
    }
  }
  return this;
}

代码示例来源: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: 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: zhangyd-c/springboot-learning

public static void buildXssf(File tmpFile, Map<String, Object> contentMap, String exportFile) throws Exception {
  FileInputStream tempFileInputStream = new FileInputStream(tmpFile);
  XWPFDocument document = new XWPFDocument(tempFileInputStream);
  // 替换段落里面的变量
  replaceInPara(document, contentMap);
  // 导出到文件
  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  document.write(byteArrayOutputStream);
  write(exportFile, byteArrayOutputStream);
  document.close();
}

代码示例来源:origin: youseries/ureport

document.write(outputStream);			
}catch(Exception ex){
  throw new ReportComputeException(ex);

代码示例来源:origin: org.hswebframework/hsweb-expands-office

@Override
  public void done(Object document) throws Exception {
    try {
      //执行表达式
      runner.run();
    } catch (Throwable throwable) {
      throw new Exception(throwable);
    }
    XWPFDocument docx = ((XWPFDocument) document);
    docx.write(outputStream);
  }
}

代码示例来源:origin: hs-web/hsweb-expands

@Override
  public void done(Object document) throws Exception {
    try {
      //执行表达式
      runner.run();
    } catch (Throwable throwable) {
      throw new Exception(throwable);
    }
    XWPFDocument docx = ((XWPFDocument) document);
    docx.write(outputStream);
  }
}

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

XWPFDocument doc = new XWPFDocument();
doc.write(new FileOutputStream(new File("test.docx")));

CustomXWPFDocument document = new CustomXWPFDocument(new FileInputStream(new File("test.docx")));
FileOutputStream fos = new FileOutputStream(new File("test.docx"));

String blipId = document.addPictureData(new FileInputStream(new File("res/Tulips.jpg")), Document.PICTURE_TYPE_JPEG);

System.out.println(document.getNextPicNameNumber(Document.PICTURE_TYPE_JPEG));

//System.out.println(document.getNextPicNameNumber(Document.PICTURE_TYPE_JPEG));
document.createPicture(blipId,document.getNextPicNameNumber(Document.PICTURE_TYPE_JPEG), 100, 150);

document.write(fos);
fos.flush();
fos.close();

代码示例来源: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: zhangdaiscott/jeasypoi

@Override
  protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    String codedFileName = "临时文件.docx";
    if (model.containsKey(TemplateWordConstants.FILE_NAME)) {
      codedFileName = (String) model.get(TemplateWordConstants.FILE_NAME) + ".docx";
    }
    if (isIE(request)) {
      codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF8");
    } else {
      codedFileName = new String(codedFileName.getBytes("UTF-8"), "ISO-8859-1");
    }
    response.setHeader("content-disposition", "attachment;filename=" + codedFileName);
    XWPFDocument document = WordExportUtil.exportWord07((String) model.get(TemplateWordConstants.URL), (Map<String, Object>) model.get(TemplateWordConstants.MAP_DATA));
    ServletOutputStream out = response.getOutputStream();
    document.write(out);
    out.flush();
  }
}

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

XWPFDocument document = new XWPFDocument(PoiTest.class.getResourceAsStream("twocolumn.docx"));
 XWPFParagraph tmpParagraph = document.getParagraphs().get(0);
 for (int i = 0; i < 100; i++) {
   XWPFRun tmpRun = tmpParagraph.createRun();
   tmpRun.setText("LALALALAALALAAAA");
   tmpRun.setFontSize(18);
 }
 document.write(new FileOutputStream(new File("C:\\temp\\poi.docx")));

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

String text = "Test";
 File docxFile = new File("C:/testeXWPF.docx");
 FileInputStream finStream = new FileInputStream(docxFile.getAbsolutePath());
 XWPFDocument doc = new XWPFDocument(finStream);
 XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();
 if (policy == null) {
   policy = new XWPFHeaderFooterPolicy(doc);
 }
 CTP ctP1 = CTP.Factory.newInstance();
 CTR ctR1 = ctP1.addNewR();
 CTText t = ctR1.addNewT();
 t.setStringValue(text);
 XWPFParagraph codePara = new XWPFParagraph(ctP1);
 XWPFParagraph[] newparagraphs = new XWPFParagraph[1];
 newparagraphs[0] = codePara;
 policy.createFooter(policy.DEFAULT, newparagraphs);
 FileOutputStream fileOut = new FileOutputStream(docxFile);
 doc.write(fileOut);
 fileOut.close();

代码示例来源: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: cn.hutool/hutool-all

/**
 * 将Word Document刷出到输出流
 * 
 * @param out 输出流
 * @param isCloseOut 是否关闭输出流
 * @return this
 * @throws IORuntimeException IO异常
 */
public Word07Writer flush(OutputStream out, boolean isCloseOut) throws IORuntimeException {
  Assert.isFalse(this.isClosed, "WordWriter has been closed!");
  try {
    this.doc.write(out);
    out.flush();
  } catch (IOException e) {
    throw new IORuntimeException(e);
  } finally {
    if (isCloseOut) {
      IoUtil.close(out);
    }
  }
  return this;
}

代码示例来源: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")));

代码示例来源: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

public void test1() throws IOException{

  XWPFDocument sampleDoc = new XWPFDocument();
  XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
  //in an empty document always will be null
  if(policy==null){
    CTSectPr sectPr = sampleDoc.getDocument().getBody().addNewSectPr();
    policy = new  XWPFHeaderFooterPolicy( sampleDoc, sectPr );
  }

  if (policy.getDefaultHeader() == null && policy.getFirstPageHeader() == null
      && policy.getDefaultFooter() == null) {
    XWPFHeader headerD = policy.createHeader(policy.DEFAULT);
    headerD.getParagraphs().get(0).createRun().setText("Hello Header World!");

  } 
  FileOutputStream out = new FileOutputStream(System.currentTimeMillis()+"_test1_header.docx");
  sampleDoc.write(out);
  out.close();
  sampleDoc.close();
}

代码示例来源: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: org.apache.poi/poi-examples

public static void main(String[] args) throws IOException {
    try (XWPFDocument doc = new XWPFDocument()) {

      XWPFParagraph p = doc.createParagraph();

      XWPFRun r = p.createRun();
      r.setText("Some Text");
      r.setBold(true);
      r = p.createRun();
      r.setText("Goodbye");

      // create header/footer functions insert an empty paragraph
      XWPFHeader head = doc.createHeader(HeaderFooterType.DEFAULT);
      head.createParagraph().createRun().setText("header");

      XWPFFooter foot = doc.createFooter(HeaderFooterType.DEFAULT);
      foot.createParagraph().createRun().setText("footer");

      try (OutputStream os = new FileOutputStream(new File("header2.docx"))) {
        doc.write(os);
      }
    }
  }
}

相关文章

微信公众号

最新文章

更多

XWPFDocument类方法