1

I am not able to upload the xlsx file using the below code. why ? Do I need to make any modifications? Please suggest.

An error has occured while importin data Please make sure input csv file is correct.

ImportDataFromCSVController

public class ImportDataFromCSVController {
    private static final String errorMesg = 'An error has occured while importin data Please make sure input csv file is correct';

    public Blob csvFileBody {get; set;}
    public string csvAsString {get;set;}
    public String[] csvFileLines {get;set;}
    public List<account> acclist {get;set;}

    public ImportDataFromCSVController(){
        csvFileLines = new String[]{};
        acclist = New List<Account>(); 
    }

    public void importCSVFile(){
        try{
            csvAsString = csvFileBody.toString();
            csvFileLines = csvAsString.split('\n'); 

            for(Integer i=1; i < csvFileLines.size(); i++){
                    Account acct = new Account();
                    string[] csvRecordData = csvFileLines[i].split(',');
                    acct.name = csvRecordData[0] ;             
                    acct.accountnumber = csvRecordData[1];
                    acct.Type = csvRecordData[2];
                    acct.AccountSource = csvRecordData[3];   
                    acct.Industry = csvRecordData[4];                                                                             
                    acclist.add(acct); 
                }
            }catch (Exception e){
                ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,errorMesg);
                ApexPages.addMessage(errorMessage);
            } 
        }
    }

and ImportDataFromCSVControllerVFPage

<apex:page controller="ImportDataFromCSVController">
    <apex:form>
        <apex:messages />
        <apex:pageBlock>
            <apex:pageBlockSection>
                <apex:inputFile value="{!csvFileBody}" fileName="{!csvAsString}" />
                <apex:commandButton value="Import Account" action="{!importCSVFile}" />
            </apex:pageBlockSection>
        </apex:pageBlock>

        <apex:pageBlock>
            <apex:pageBlockTable value="{!acclist}" var="acc">
                <apex:column value="{!acc.Name}" />
                <apex:column value="{!acc.AccountNumber}" />
                <apex:column value="{!acc.Type}" />
                <apex:column value="{!acc.Accountsource}" />
                <apex:column value="{!acc.Industry}" />
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

enter image description here

  • 2
    You can not upload an XLS file when your code is expecting a CSV format. – Ashwani May 16 '17 at 9:38
  • 1
    Open the xlsx file, hit save as, and choose .csv. – martin May 16 '17 at 9:39
  • What changes in code do I need to make in order to accepts the xlsx or xls format ? – user4567570 May 16 '17 at 12:16
  • @user4567570 Since it looks like you're trying to extract values from your uploaded file, the changes you'd need to make would require an enormous amount of work. You'd need to build something (specifically, a parser) that understands the xls and xlsx file formats which, I can assure you, is no simple task. The amount of time you would sink into this wouldn't be worth the end result. CSV files really are the way to go here. – Derek F May 16 '17 at 13:03
  • 1
    If you're dead-set on accepting xlsx files, the Office Open XML File Formats page contains over 6000 pages of information, and Microsoft's extensions to the Office Open XML File Formats is another 297 pages on top of that. – Derek F May 16 '17 at 13:05

Your Answer

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

Browse other questions tagged or ask your own question.