I'm working on an app, I want to generate a CSV file, it has many columns with normal text, one of the columns has HTML text

I want to insert HTML text without formatting but with tags, when I open the file in CSV it distorts the formatting due to line breaks and other HTML tags. Below is my code

FileWriter fw = new FileWriter(filename);

// the below is normal string and it's fine
fw.append(selectedProduct.getHandle());
fw.append(",");
// below is html string it doesn't come in single cell
fw.append(selectedProduct.getHTMLBody());
fw.append(",");

I want to show HTML In a single cell like in CSV like below

HTML In Single Cell

Any help would be grateful.

1 Answers

2
Krystian G On Best Solutions

If you want to get rid of line breaks remove them from the HTML:

fw.append(selectedProduct.getHTMLBody().replaceAll("\\r\\n|\\r|\\n", " "));

Note that your cells are comma separated. The consequence of that is that any comma in your HTML contents will be recognized as cell boundary. To indicate that contents should be single cell try surrounding it with quotes:

fw.append("\"" + selectedProduct.getHTMLBody().replaceAll("\\r\\n|\\r|\\n", " ") + "\"");