Заменить тег текстом в текстовом поле внутри рабочего листа
Contents
[
Hide
]
Возможные сценарии использования
Текстовые поля могут иметь теги, которые можно заменить текстом во время выполнения, чтобы настроить их в соответствии с требованиями. Тегами могут быть некоторые метки, заключенные в угловые скобки ‘<’ and ‘>. В одном текстовом поле может быть несколько тегов.
Образец кода
Следующий пример кода заменяет теги TAG_1 и ТЭГ_2 с каким-то текстом, скажем, «ys» и «1». Образец файла для тестирования приведенного ниже кода можно загрузить по следующей ссылке:
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-.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); |
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-.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("<", "<"); | |
sFind = sFind.Replace(">", ">"); | |
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); | |
} | |
} | |
} | |
} | |
} |