How can I replace a placeholder by html content?

Hello Aspose,
I have a template excel file with a placeholder like this : Hello[@AsposeHtml@].
So I have a question: How can I replace [@AsposeHtml@] with html content(data saved from rich textbox).

I written a function replace like this but I can not replace with html content like above:
private void ReplaceDataByAspose(Workbook workbook, Hashtable allfields)
{

        ReplaceOptions replaceOptions = new ReplaceOptions();
        replaceOptions.CaseSensitive = false;
        replaceOptions.MatchEntireCellContents = false;
        foreach (var key in allfields.Keys)
        {
            var text = allfields[key].ToString();
            workbook.Replace(key.ToString(), text, replaceOptions);
        }
    }

Thank you very much!

@nguyenhungkloon,

Thanks for your query.

How could you perform the task in MS Excel manually? Could you share the template file and output file by Aspose.Cells APIs. Also provide your desired file (created in MS Excel manually), we will check it soon.

ReplaceTextByHtmlContentAndImage.zip (7.3 KB)

yes,

I have attach a file template. And I have 2 question about find and replace for xlsx file:

  • How can I replace text(placeholder) by html content?

  • How can I replace text(placeholder) by Image content?

Please see my template in detail.

Thank you very much!

@nguyenhungkloon,

I requested your desired file but you did not provide it. Anyways, you may find your desired string/text in the worksheet and replace with HTML contents. Please note, you cannot replace a part of string in the cell and replace it with HTML contents. Aspose.Cells provides Cell.HtmlString attribute which is used to set cell’s value as HTML string but the whole cell’s value will be replaced by the parsed HTML.
See the document on how to find strings in the worksheet for your reference:

(once you find the cell containing your desired string, you may then use Cell.HtmlString attribute to pass the HTML string to the cell, see the document on how to use HtmlString attribute.).
Also, see the document on how to insert image into the cells for your reference:

You may save the image as base64 when you save the Excel spreadsheet as HTML file format, see the sample code segment for your reference:
e.g
Sample code:

..........
HtmlSaveOptions opts = new HtmlSaveOptions();
            opts.ExportImagesAsBase64 = true;
            opts.ExportActiveWorksheetOnly = true;

            //Save the workbook in HTML format with above HtmlSaveOptions
            workbook.Save("e:\\test2\\out1.html", opts);
.......

If you want something else, as requested earlier, please provide a sample file with your desired images inserted as base64 with details on how to do it via MS Excel manually, we will check it soon.

Thank you for your support.
I can using cell.HtmlString to set html content for cell. But some many case the file is corrupted when I set style.

@nguyenhungkloon,

Please note, Aspose.Cells follows MS Excel standards and specifications when rendering HTML files. Aspose.Cells support to create/render MS Excel oriented HTML files. So, when you specify html string, this should be supported in MS Excel or MS Excel should parse it otherwise the style won’t be applied. For your information, not all HTML tags or common HTML are supported. Aspose.Cells can parse those HTML tags which are understood/parsed by MS Excel. To evaluate how or which HTML tags are supported. You may open a new file into MS Excel and specify some formattings (set cell’s background color and font color, etc.) and now save the file to “Web Page”. Now open the HTML into notepad and check how HTML tags are generated against your specified formattings.

We recommend you to kindly do use Aspose.Cells APIs to set your desired formattings (instead of using HTML strings), see the documents for your reference:

etc.

Hope, this helps a bit.

Thank you very much!

@nguyenhungkloon,

You are welcome.