I have this scenario that html text are being stor...
# cfml-general
s
I have this scenario that html text are being stored in the database. I need to download an excel with that column data. when I download it, it is showing the html text in the excel. I've tried so many things, but not getting proper output in the excel file ( for example hyperlinks are showing as anchor tags).
Any ideas?
e
you are saying you want the raw HTML code displayed as text in the excel cell, but its being rendered by excel as html?
Copy code
e.g.
<a href="testlink">my link</a>

is being shown as a hyperlink?
s
<a href="testlink">my link</a> - This is in db. It should be rendered as hyperlink in the excel.
e
ah, i had it backwards, so excel is showing the raw html instead of rendering it. how are you exporting to excel?
s
I am using SpreadSheet functions to create and write an xls file and using headers to download it. The text in DB like this:
'A power hub <a href="<https://www.amazon.com/>">Visit</a>'
While writing to the excel, it is rendering like the same html tags instead of hyperlinks. I've tried the below (also tried manually updating the tags with chr, more and more. I am just pulling my hair out for this)
Copy code
<cfset excelData = 'TEXTJOIN("", TRUE, "A power hub", HYPERLINK("<https://www.amazon.com/>", "Visit"))'>

<cfset spreadsheetSetCellFormula(sheet, "#excelData#", 1, 1)>
e
i don't have an ACF instance to play with, but did you try a super simple test case like:
Copy code
<cfset spreadsheetObj = SpreadsheetNew("My Sheet")>

<!-- Add a hyperlink --->
<cfset SpreadsheetSetCellValue(spreadsheetObj, '=HYPERLINK("<http://www.example.com>", "Example Website")', 1, 1)>

<!-- Write the spreadsheet to a file -->
<cfspreadsheet action="write" filename="example.xlsx" name="spreadsheetObj">
s
yeah tried that already, it will work only for hyperlinks, if we combine any plain text with hyperlink it wont work
e
ah, i see - ya what i'm reading says
Copy code
Excel does not natively support partial hyperlinks within a single cell. Each cell can only contain a single hyperlink that applies to the entire content of the cell.

If you need to format only part of the text in a cell as a hyperlink, you would need to use Rich Text Formatting in Excel, which is not directly supported by ColdFusion's <cfspreadsheet> or Spreadsheet functions
s
yup dead end 😞
e
it does say you can save HTML code with an .xls extension and excel should interpret and render the hyperlink case, but users will get that excel warning whenever they open the file
only other approach i see mentioned is to use Apache POI library
s
yeah but that will be talked as an another "error" from the end user side if they see that warning. yeah need to check POI, seems no other simpler ways without using libraries.
e
ok, good luck, here is the example POI i saw for ref
Copy code
// In Java, use Apache POI's XSSFRichTextString to create partial hyperlinks
XSSFRichTextString richText = new XSSFRichTextString("This is a test website");
richText.applyFont(10, 17, hyperlinkFont); // Apply hyperlink font styling to "website"
cell.setCellValue(richText);
s
Thanks