Sostituisci il tag con il testo in una casella di testo all'interno del foglio di lavoro

Possibili scenari di utilizzo

Le caselle di testo possono avere tag che possono essere sostituiti con del testo in fase di esecuzione per configurarli in base ai requisiti. I tag possono essere alcune etichette racchiuse tra parentesi angolari ‘<’ and ‘>. Possono esserci più tag all’interno di una singola casella di testo.

Codice d’esempio

Il seguente codice di esempio sostituisce i tag TAG_1 e TAG_2 con del testo dice ‘ys’ e ‘1’. Il file di esempio per il test sotto il codice può essere scaricato dal seguente link:

sampleReplaceTagWithText.xlsx

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
Workbook wb = new Workbook(sourceDir + "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(outputDir + "outputReplaceTagWithText.pdf", opts);
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
public static void sheetReplace(Workbook workbook, string sFind, string sReplace)
{
string finding = sFind;
foreach (Worksheet sheet in workbook.Worksheets)
{
sheet.Replace(finding, sReplace);
for (int j = 0; j < 3; j++)
{
if (sheet.PageSetup.GetHeader(j) != null)
sheet.PageSetup.SetHeader(j, sheet.PageSetup.GetHeader(j).Replace(finding, sReplace));
if (sheet.PageSetup.GetFooter(j) != null)
sheet.PageSetup.SetFooter(j, sheet.PageSetup.GetFooter(j).Replace(finding, sReplace));
}
}
foreach (Worksheet sheet in workbook.Worksheets)
{
sFind = sFind.Replace("<", "&lt;");
sFind = sFind.Replace(">", "&gt;");
foreach (Aspose.Cells.Drawing.TextBox mytextbox in sheet.TextBoxes)
{
if (mytextbox.HtmlText != null)
{
if (mytextbox.HtmlText.IndexOf(sFind) >= 0)
{
mytextbox.HtmlText = mytextbox.HtmlText.Replace(sFind, sReplace);
}
}
}
}
}