-2

I want to open an Excel file through an Apex dialog box, then browse the information in my file line and display it with visualforce. I show you my code

public class ImportExcel {  
    public String fileName{get; set;}
    public Blob contentFile{get;set;}
    public String[] csvFileLines{get;set;}
    public List<String> acclist{get;set;}
    public String[] data{get;set;}
    public ImportExcel(){
        csvFileLines = new String[]{};
        acclist = New List<String>(); 
    }
public void readFile(){
        try
        {
            fileName=contentFile.toString();
        }catch(Exception e){
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'Please upload a valid CSV document');
        ApexPages.addMessage(myMsg); 
            return ; 
        }
        csvFileLines=fileName.split('\n'); 
        system.debug(csvFileLines);
        for(Integer i=0;i<csvFileLines.size();i++){
            data=csvFileLines[i].split(',');
            acclist.add(data[i]);
        }
    }
}

Once I execute my code, it puts me this error:

13:52:13:012 VARIABLE_ASSIGNMENT [15]|e|"common.apex.runtime.impl.ExecutionException: BLOB is not a valid UTF-8 string"|0x4e4728

  • 1
    Please edit your existing question instead of opening a new one. Please make sure that all relevant information (code, objective, and error details) is in the body of your question. This time, I will close the previous question as a duplicate of this one, but please do familiarize yourself with How to Ask. – David Reed Apr 29 '19 at 12:33
2

This issue is with your file primarily and your code only secondarily. Apex (and Salesforce more broadly) only supports UTF-8 encoded text, or encodings like ASCII that are equivalent to UTF-8 for their codepoint ranges. What this error is telling you is that the file you've uploaded is not valid UTF-8 text, and therefore cannot be converted to an Apex String.

There are few routes around this in Apex. It's really a problem you should fix at the source (the file). The most likely case is that your file isn't really CSV but is a binary format. If it actually is CSV data (not just a file called .csv!), you should use a programmer's editor like Visual Studio Code to examine its contents and may need to re-save it using the UTF-8 encoding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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