2

Is there a way to make a string in apex class bold?

I am generating a CSV file as email attachment in a apex class and sending it. Now the header of the CSV file is in a string format:

string header = text1, text2,text3

I want to make it bold. Can it be done?

PS: I am not using a Visualforce Page.

2
  • 2
    Take an Excel sheet, have some bold values and some non bold values, save it as a .csv then open that .csv in a text editor. You'll see that nothing gets preserved. The format stores raw data only. Nov 8 '16 at 16:59
  • Understood...is there a way I can accomplish this in an excel file then? I will generate this in apex.
    – KLab
    Nov 8 '16 at 17:01
6

That's not how CSV files work. Comma Separated Values means that the file contains tabular data values in plain text. Values only. No formatting.

Note that the below solution is the only way to format cells, but my personal opinion is that it would be massively over-engineering the task at hand.

If you really want the data to be formatted, you must generate an excel spreadsheet. To do this, you will need to create a Visualforce Page and make a callout using the PageReference.getContent() method. Your page would look something like:

<apex:page controller="MyXlsController" contentType="application/vnd.ms-excel#MyDataExport.xls" cache="true">
    <apex:pageBlock>
        <apex:pageBlockTable value="{!rows}" var="row">
            <apex:column value="{!row[0]}" headerValue="Column 1" />
            <apex:column value="{!row[1]}" headerValue="Column 2" />
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Then in your Apex, you should be able to do something like:

PageReference xls = Page.MyXls;
Blob spreadsheet = xls.getContent();
// above line counts as a callout

// perform any further manipulation
2
  • I understand. Is there a way to accomplish this in excel then? I will change the content type to xls. I just need the header of the table to be bold. Thank you :)
    – KLab
    Nov 8 '16 at 17:03
  • @KaustubhLabhe I added the one way you can technically get this result, but I think you're better off just opening it up and modifying it by yourself manually.
    – Adrian Larson
    Nov 8 '16 at 17:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.