Ersetzen Sie das Tag durch Text in einem Textfeld im Arbeitsblatt
Contents
[
Hide
]
Mögliche Nutzungsszenarien
Textfelder können Tags haben, die zur Laufzeit durch Text ersetzt werden können, um sie den Anforderungen entsprechend zu konfigurieren. Tags können ein in spitze Klammern eingeschlossenes Label sein ‘<’ and ‘>. Es können mehrere Tags in einem einzelnen Textfeld vorhanden sein. Die Beispieldatei kann unter folgendem Link heruntergeladen werden.
Beispielcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
public static void main(String[] args) throws Exception { | |
Workbook wb = new Workbook(srcDir + "sampleReplaceTagWithText.xlsx"); | |
String tag = "TAG_2#TAG_1"; | |
String replace = "1#ys"; | |
for (int i = 0; i < tag.split("#").length; i++) { | |
sheetReplace(wb, "<" + tag.split("#")[i] + ">", replace.split("#")[i]); | |
} | |
PdfSaveOptions opts = new PdfSaveOptions(); | |
wb.save(outDir + "outputReplaceTagWithText.pdf", opts); | |
// Print the message | |
System.out.println("ReplaceTagWithTextInTextBox executed successfully."); | |
} | |
public static void sheetReplace(Workbook workbook, String sFind, String sReplace) throws Exception | |
{ | |
String finding = sFind; | |
for (Object obj : workbook.getWorksheets()) { | |
Worksheet sheet = (Worksheet)obj; | |
sheet.replace(finding, sReplace); | |
for (int j = 0; j < 3; j++) { | |
if (sheet.getPageSetup().getHeader(j) != null) { | |
sheet.getPageSetup().setHeader(j, sheet.getPageSetup().getHeader(j).replace(finding, sReplace)); | |
} | |
if (sheet.getPageSetup().getFooter(j) != null) { | |
sheet.getPageSetup().setFooter(j, sheet.getPageSetup().getFooter(j).replace(finding, sReplace)); | |
} | |
} | |
} | |
for (Object obj: workbook.getWorksheets()) { | |
Worksheet sheet = (Worksheet)obj; | |
sFind = sFind.replace("<", "<"); | |
sFind = sFind.replace(">", ">"); | |
for (Object obj1 : sheet.getTextBoxes()) { | |
TextBox mytextbox = (TextBox)obj1; | |
if (mytextbox.getHtmlText() != null) { | |
if (mytextbox.getHtmlText().indexOf(sFind) >= 0) { | |
mytextbox.setHtmlText(mytextbox.getHtmlText().replace(sFind, sReplace)); | |
} | |
} | |
} | |
} | |
} |