function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Hemant Sharma 46Hemant Sharma 46 

Read password protected excel file in the visualforce page

Hi,

Actually, I have a page where i can upload the excel file. I am reading the data from excel.
When I upload a simple excel file, code works as per expected.

But When i upload the passoword protected excel file, it throws the exception. It is not able to read the excel sheet data.

Controller
public class ContactImportController {
public String emailList {get; set;}
    public String recordData {get; set;}
    public Boolean recordDataFound {get; set;}
    public Integer recordDataTotal {get; set;}
    public Map<String,Integer> recordDataMap {get; set;}

    public void filterContact(){
        system.debug('emailList');
        system.debug(emailList);
        recordDataFound = false;
        if(emailList != null ){
            Set<String> emailSet = new Set<String>(emailList.split(','));
            system.debug(emailSet);
            Map<String,Integer> emailMap = new Map<String,Integer>();
            recordDataTotal = 0;
            for (AggregateResult ar : [SELECT Email emailId, Count(id) ContactCount FROM Contact WHERE Email =:emailSet GROUP BY Email]){
                system.debug(ar);
                recordDataTotal += (Integer) ar.get('ContactCount');
                emailMap.put((String) ar.get('emailId'),(Integer) ar.get('ContactCount'));
            }
            if(emailMap.size() != 0){
                recordDataFound = true;
                recordDataMap = emailMap;
                recordData = 'Success';
            }else{
                recordData ='No Contact List Found';
            }
        }else{
            recordData ='No Email List Found';
        }
    }

}


VF Page
<apex:page id="df" sidebar="false" controller="ContactImportController" showHeader="false" standardStylesheets="true" lightningStylesheets="true">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/jszip.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.js"></script>
    <style>
    h2{
        padding: 2px;
        margin: 0px;
    }
    </style>
    <div style="text-align: center;margin: 80px;">
        <form enctype="multipart/form-data">
            <h1> Select File for test the Contacts</h1>
            <input id="upload" type="file" accept=".xlsx" name="files[]" classs="slds-button slds-button_brand" style="margin: 20px;" />
        </form>
        <apex:form id="dfp">
            <apex:actionStatus startText="Processing...." id="status"></apex:actionStatus>
            <apex:actionFunction action="{!filterContact}" name="dfpFilterData" id="dfpFilterData" reRender="dfp" status="status" oncomplete="showData()">
                <apex:param name="firstParam" assignTo="{!emailList}" value="" />
            </apex:actionFunction>
            <apex:inputHidden value="{!recordData}" id="recordDataID"/>
            <apex:outputPanel rendered="{!recordDataFound}">
               <h2> <b>Total : {!recordDataTotal} </b> </h2>  
                <apex:repeat value="{!recordDataMap}" var="emailcon">
                    <h2>  Email: {!emailcon} ({!recordDataMap[emailcon]})</h2> 
                </apex:repeat>
            </apex:outputPanel>
            <script>
                var ExcelToJSON = function() {
            
                    this.parseExcel = function(file) {
                        var reader = new FileReader();
                
                        reader.onload = function(e) {
                            try{
                                var data = e.target.result;
                                var workbook = XLSX.readFile(data, {
                                    type: 'binary'                                   
                                });
                                workbook.SheetNames.forEach(function(sheetName) {
                                    // Here is your object
                                    var XL_row_object = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
                                    var json_object = JSON.stringify(XL_row_object);
                                    console.log(JSON.parse(json_object));
                                    if(json_object.length != 0){
                                        var emailList = '';
                                        JSON.parse(json_object).forEach( row => {
                                            if(row['E-mail']){
                                                emailList += row['E-mail']+',';
                                            }
                                        });
                                        emailList = emailList.slice(0, -1);
                                        console.log('emailList >>>');
                                        console.log(emailList);
                                        if(emailList.trim().length != 0){
                                            dfpFilterData(emailList);
                                        }else{
                                            alert("No Email Found");
                                        }
                                    }else{
                                        alert("No Data Found");
                                    }
                                })
                            }catch(e){
                                alert('Please select Excel File');
                                console.log(e);
                                document.getElementById('upload').value= [];
                            }
                        };
                
                        reader.onerror = function(ex) {
                        console.log(ex);
                        };
                
                        reader.readAsBinaryString(file);
                    };
                };
                
                function handleFileSelect(evt) {
                    try{
                        var files = evt.target.files; // FileList object
                        var xl2json = new ExcelToJSON();
                        xl2json.parseExcel(files[0]);
                    }catch(e){
                        alert('Please select Excel File');
                        console.log(e);
                    }
                }

                function showData(){
                    var val = document.getElementById('df:dfp:recordDataID').value;
                    console.log('val>>>>>>'+val);
                    if(val == 'No Contact List Found'){
                        alert('No Contact List Found');
                    }else if(val == 'No Email List Found'){
                        alert('No Email List Found');
                    }else{
                        alert(val);
                    }
                }
                document.getElementById('upload').addEventListener('change', handleFileSelect, false);
            </script>
        </apex:form>
    </div>
</apex:page>


​​​​​​​
Could anyone help me on this?

Thank You!!
emma davidemma david
I have same issue in my sheet check from this link (https://hunttire.com/best-scopes-for-scar-17/). Kindly, help me.