[SalesForce] How to include the contents of a static resource file in controller class

I have a controller class with the following method implementation:

    public String buildTemplate(Task task) {

    Id taskId = task.Id;

    List<Task_History__c> listTaskHistory = [SELECT Field1, Id, Field2, Username__c, LastModifiedDate FROM Task_History__c WHERE Task_ID__c = :taskId];
    String pageHTMLContent = '<HTML><HEAD><STYLE>{! $Resource.Stylesheet }</STYLE></HEAD><BODY><TABLE class="css_class">';
    pageHTMLContent += '<TR><TH>Comment1</TH><TH>Comment2/TH><TH>Last Modified By</TH><TH>Last Modified Date</TH>';

    for(Task_History__c taskHistory :listTaskHistory) {
        String oldValue = taskHistory.Old_Value__c;
        String newValue = taskActivityHistory.New_Value__c;
        String userName = taskActivityHistory.Username__c;
        DateTime lastModifiedByDate = taskActivityHistory.LastModifiedDate;
        pageHTMLContent += '<TR>';
        pageHTMLContent += '<TD>' + oldValue + '</TD><TD>' + newValue + '</TD><TD>' + userName + '</TD><TD>' + lastModifiedByDate + '</TD>';
        pageHTMLContent += '</TR>';
    }

    pageHTMLContent += '</TABLE></BODY></HTML>';

    return pageHTMLContent;
}

I'd like to know how to include the contents of a static resource file in controller class?

In the Static Resource I have a file called "Stylesheet" with content:

table.css_class , .css_class th, .css_class td { border: 1px solid black; padding: 10px; } table.css_class th { text-align: center; }

Please advise how to include the contents of a static resource file in controller class?

Best Answer

you can query the body of a Static Resource using a query like that:

StaticResource stylesheetStaticResource = [
            SELECT Body
            FROM StaticResource
            WHERE Name = 'Stylesheet'
            ];

There are other questions similars like Read text file as a static resource and display in a VF page?