Free Support Forum - aspose.com

How to replace {excel} with 1.xlsx embeding in word?

I want to replace {excel} with 1.xlsx embeding in input.docx and save it as output.docx.
However the following code can only embed 1.xlsx to the beginning of input.docx.
What should I do to replace {excel} tag with 1.xlsx?

input.docx I got
image

output.docx which I want
image

output.docx which I actually got
image

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;

public class Aspose
{
public static void main( String[] args ) throws Exception
{
try {
Document doc = new Document(“E:/workspace/Aspose/input.docx”);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertOleObject(“E:/workspace/Aspose/1.xlsx”, “xlsx”, false, true, null);
doc.save(“E:/workspace/Aspose/” + “output.docx”);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Hi there,


Thanks for your inquiry. Please refer to the following article about find and replace the text.
Find and Replace

Following code example shows how to find text {Excel} and replace it with OLE object. Hope this helps you.

Document doc = new Document(MyDir + "tag-example.docx");

FindReplaceOptions options =
new FindReplaceOptions();
options.
ReplacingCallback = new FindandInsertOLE("c:\\temp\\Book1.xlsx");
Pattern regex = Pattern.compile(
"\\{excel\\}", Pattern.CASE_INSENSITIVE);
doc.getRange().replace(regex,
"", options);

// Save the output document.
doc.save(MyDir + "output.docx");

class FindandInsertOLE implements IReplacingCallback {

    String olepath;
public FindandInsertOLE(String path)
{
olepath = path;
}

public int replacing(ReplacingArgs e) throws Exception {
// This is a Run node that contains either the beginning or the complete match.
Node currentNode = e.getMatchNode();

// The first (and may be the only) run can contain text before the match,
// in this case it is necessary to split the run.
if (e.getMatchOffset() > 0)
currentNode = splitRun((Run) currentNode, e.getMatchOffset());

ArrayList runs = new ArrayList();

// Find all runs that contain parts of the match string.
int remainingLength = e.getMatch().group().length();
while ((remainingLength > 0) && (currentNode != null) && (currentNode.getText().length() <= remainingLength)) {
runs.add(currentNode);
remainingLength = remainingLength - currentNode.getText().length();

// Select the next Run node.
// Have to loop because there could be other nodes such as BookmarkStart etc.
do {
currentNode = currentNode.getNextSibling();
} while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));
}

// Split the last run that contains the match if there is any text left.
if ((currentNode != null) && (remainingLength > 0)) {
splitRun((Run) currentNode, remainingLength);
runs.add(currentNode);
}

DocumentBuilder builder = new DocumentBuilder((Document)e.getMatchNode().getDocument());
builder.moveTo((Run)runs.get(0));
builder.insertOleObject(olepath, "Excel.Sheet.12", false, true, null);
// Remove matched text
for (Run run : (Iterable) runs)
run.remove();

// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.SKIP;
}

/**
* Splits text of the specified run into two runs. Inserts the new run just
* after the specified run.
*/
private Run splitRun(Run run, int position) throws Exception {
Run afterRun = (Run) run.deepClone(true);
afterRun.setText(run.getText().substring(position));
run.setText(run.getText().substring((0), (0) + (position)));
run.getParentNode().insertAfter(afterRun, run);
return afterRun;
}
}
  

Moreover, we suggest you please use LINQ Reporting to replace tags e.g. {last_name}, {first_name} with some values. Please check the charts examples from following documentation link. Hope this helps you.

Appendix C. Typical Templates

It works! Thank you for your help!