Reemplace la etiqueta con texto en un cuadro de texto dentro de la hoja de trabajo
Contents
[
Hide
]
Posibles escenarios de uso
Los cuadros de texto pueden tener etiquetas que se pueden reemplazar con algún texto en tiempo de ejecución para configurarlos de acuerdo con el requisito. Las etiquetas pueden ser alguna etiqueta encerrada entre paréntesis angulares ‘<’ and ‘>. Puede haber múltiples etiquetas dentro de un solo cuadro de texto. El archivo de muestra se puede descargar desde el siguiente enlace.
ejemploReplaceTagWithText.xlsx
Código de muestra
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)); | |
} | |
} | |
} | |
} | |
} |