Remplacer la balise par du texte dans une zone de texte à l'intérieur de la feuille de calcul
Contents
[
Hide
]
Scénarios d’utilisation possibles
Les zones de texte peuvent avoir des balises qui peuvent être remplacées par du texte au moment de l’exécution pour les configurer en fonction des besoins. Les balises peuvent être une étiquette entre crochets ‘<’ and ‘>. Il peut y avoir plusieurs balises dans une même zone de texte. Le fichier d’exemple peut être téléchargé à partir du lien suivant.
exempleReplaceTagWithText.xlsx
Exemple de code
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)); | |
} | |
} | |
} | |
} | |
} |