Замена тега текстом в текстовом поле внутри Рабочей Книги
Contents
[
Hide
]
Возможные сценарии использования
Text boxes can have tags that can be replaced with some text at run time to configure them according to the requirement. Tags can be some label enclosed in angle brackets ‘<’ and ‘>’. There can be multiple tags within a single textbox. The sample file can be downloaded from the following link.
Образец кода
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)); | |
} | |
} | |
} | |
} | |
} |