使用java代码(不包括垃圾字符)将数据替换为文档在Alfresco中

时间:2016-05-20 05:35:57

标签: java node.js excel alfresco-share

我正在Alfresco做批量上传任务。

在此之前我创建了调用java代码的自定义操作,我也成功从excel表中读取数据,并且我找到了目标文档的节点引用以及源文档。使用该节点引用我也可以创建新的多个文档。 现在我的要求是,我想在新创建的Document中替换Excel Data。我试图替换它,但它仅在第一行文档中替换字符串,并且还删除新创建的文档中的其余现有内容。我为此写了下面的代码。

在下面的代码中,我首先只是尝试将一些硬编码数据替换为Document。 但我的要求是我想替换我已经从excel文件中读取的文档中的数据。

Java代码:

  public class MoveReplacedActionExecuter extends ActionExecuterAbstractBase {
  InputStream is;
  Cell cell = null;
  public static final String NAME = "move-replaced";
  private FileFolderService fileFolderService;
  private NodeService nodeService;
  private ContentService contentService;
  private SearchService searchService;
  @Override
  protected void addParameterDefinitions(List < ParameterDefinition > paramList) {

  }
  public void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef) {

   try {
    ContentReader contentReader = contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT);
    is = contentReader.getContentInputStream();
   } catch (NullPointerException ne) {
    System.out.println("Null Pointer Exception" + ne);
   }

   try {

    Workbook workbook = new XSSFWorkbook(is);

    Sheet firstSheet = workbook.getSheetAt(0);
    Iterator < Row > iterator = firstSheet.rowIterator();

    while (iterator.hasNext()) {
     ArrayList < String > al = new ArrayList < > ();
     System.out.println("");
     Row nextRow = iterator.next();
     Iterator < Cell > cellIterator = nextRow.cellIterator();
     while (cellIterator.hasNext()) {
      cell = cellIterator.next();
      switch (cell.getCellType()) {
       case Cell.CELL_TYPE_STRING:
        System.out.print("\t" + cell.getStringCellValue());
        al.add(cell.getStringCellValue());
        break;
       case Cell.CELL_TYPE_BOOLEAN:
        System.out.print("\t" + cell.getBooleanCellValue());
        al.add(String.valueOf(cell.getBooleanCellValue()));
        break;
       case Cell.CELL_TYPE_NUMERIC:
        System.out.print("\t" + cell.getNumericCellValue());
        al.add(String.valueOf(cell.getNumericCellValue()));
        break;
      }
     }
    }

    is.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
   String query = "PATH:\"/app:company_home/cm:Dipak/cm:OfferLetterTemplate.doc\"";
   SearchParameters sp = new SearchParameters();
   StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
   sp.addStore(storeRef);
   sp.setLanguage(SearchService.LANGUAGE_LUCENE);
   sp.setQuery(query);
   ResultSet resultSet = searchService.query(sp);
   System.out.println("Result Set" + resultSet.length());
   NodeRef sourceNodeRef = null;
   for (ResultSetRow row: resultSet) {
    NodeRef currentNodeRef = row.getNodeRef();
    sourceNodeRef = currentNodeRef;
    System.out.println(currentNodeRef.toString());
   }
   NodeRef n = new NodeRef("workspace://SpacesStore/78342318-37b8-4b42-aadc-bb0ed5d413d9");

   try {
    org.alfresco.service.cmr.model.FileInfo fi = fileFolderService.copy(sourceNodeRef, n, "JustCreated" + Math.random() + ".doc");
    NodeRef newNode = fi.getNodeRef();

    QName TYPE_AUTHORTY = QName.createQName("sunpharma.hr.model", "hrdoctype");
    nodeService.setType(newNode, TYPE_AUTHORTY);

    ContentReader contentReader1 = contentService.getReader(newNode, ContentModel.PROP_CONTENT);
    InputStream is2 = contentReader1.getContentInputStream();

    POIFSFileSystem fs = new POIFSFileSystem(is2);
    HWPFDocument doc = new HWPFDocument(fs);

    doc = replaceText1(doc, "Company", "Datamatics");
    ContentWriter writerDoc = contentService.getWriter(newNode, ContentModel.PROP_CONTENT, true);
    writerDoc.putContent(doc.getDocumentText());

   } catch (FileExistsException | FileNotFoundException e) {

    e.printStackTrace();
   } catch (IOException e) {

    e.printStackTrace();
   }

  }
  private static HWPFDocument replaceText1(HWPFDocument doc, String findText, String replaceText) {
   System.out.println("In the method replacetext" + replaceText);

   Range r1 = doc.getRange();
   System.out.println("Range of Doc : " + r1);

   for (int i = 0; i < r1.numSections(); ++i) {
    Section s = r1.getSection(i);
    for (int x = 0; x < s.numParagraphs(); x++) {
     Paragraph p = s.getParagraph(x);
     for (int z = 0; z < p.numCharacterRuns(); z++) {
      CharacterRun run = p.getCharacterRun(z);
      String text = run.text();
      if (text.contains(findText)) {
       run.replaceText(findText, replaceText);
      } else {
       System.out.println("NO text found");
      }
     }
    }
   }
   return doc;
  }
  public void setFileFolderService(FileFolderService fileFolderService) {
   this.fileFolderService = fileFolderService;
  }
  public void setNodeService(NodeService nodeService) {
   this.nodeService = nodeService;
  }
  public void setContentService(ContentService contentService) {
   this.contentService = contentService;
  }
  public void setSearchService(SearchService searchService) {
   this.searchService = searchService;
  }
 }

1 个答案:

答案 0 :(得分:3)

无法在露天采取直接文件流对象。 所以我在本地驱动器上创建了一个文件,在后台我执行了所有替换操作。然后我用文件输入流对象读取所有数据。后来我使用了带节点的文件流。

它给了我想要的输出。 :)