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
kutts18kutts18 

Read User Uploaded Excel File

I have a requirement where through VF page, user will upload an Excel file.

Controller has to read this excel file and display the values in the same page.

 

Now my question would be

 

1. Is it possible to read the excel content in controller class?

2. If it is possible, can you explain me with some sample code?

 

Best Answer chosen by Admin (Salesforce Developers) 
kutts18kutts18

I have a good news for all of you guys,

If you are in desparate need of reading excel,

then you have two options

 

1. Use Excel Connector (avialable in SFA tools)

2. Use ActiveX in VF page.

 

I have used the second option, and below is my code...

 

<apex:page showHeader="false">

<apex:messages />


<script type="text/vbscript">

Sub readBtn_OnClick

Dim fileUrl
Dim rowIndex
Dim colIndex
Dim oldContent
Dim newContent
Dim cellContent

fileUrl = document.form.userFile.value
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(fileUrl)

rowIndex = 1
colIndex = 1
Do Until objExcel.Cells(rowIndex ,colIndex).Value = ""
oldContent = document.form.excelContent.value
newContent = oldContent & objExcel.Cells(rowIndex , colIndex).Value & vbCr & vbLf
document.form.excelContent.value = newContent
rowIndex = rowIndex + 1
Loop
objExcel.Workbooks(1).Close
objExcel.Quit
End Sub
</script>

<form name="form">
<input type="file" name="userFile"/>
<input type="button" value="Read" name="readBtn"/>
<p><textarea rows="10" cols="44" name="excelContent"></textarea></p>
</form>

</apex:page>

 For more reference

http://www.activexperts.com/activmonitor/windowsmanagement/scripts/msoffice/excel/

 

 

Remember that the ActiveX will be unsigned, so user

will prompted wether to execute this  ActiveX or not.

But hey you can get the job done in client side itself

cause there is no way you can do it in server side.

 

Also only IE will support ActiveX, no other browser.

All Answers

kutts18kutts18

I have gone through other community posts and lots of documents

and i have derived the conclusion that

 

Its impossible to read data from an user uploaded excel file using the current

version of Apex.

 

Would it be wrong if i conclude like this? 

 

 

 

 

 

dchasmandchasman

That is correct - there are 3rd party libraries that will read the proprietary Excel binary file format but to date none of them have been wired into Apex Code and there are no plans to do so at this time AFAIK.

 

Have you considered using apex callouts or apex's built in HTTP client (lets you call your own web service/http server using from apex code - something you can definitely do from you controller) and handling the processing in your own server and then processing the results in apex code?

kutts18kutts18

I have a good news for all of you guys,

If you are in desparate need of reading excel,

then you have two options

 

1. Use Excel Connector (avialable in SFA tools)

2. Use ActiveX in VF page.

 

I have used the second option, and below is my code...

 

<apex:page showHeader="false">

<apex:messages />


<script type="text/vbscript">

Sub readBtn_OnClick

Dim fileUrl
Dim rowIndex
Dim colIndex
Dim oldContent
Dim newContent
Dim cellContent

fileUrl = document.form.userFile.value
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(fileUrl)

rowIndex = 1
colIndex = 1
Do Until objExcel.Cells(rowIndex ,colIndex).Value = ""
oldContent = document.form.excelContent.value
newContent = oldContent & objExcel.Cells(rowIndex , colIndex).Value & vbCr & vbLf
document.form.excelContent.value = newContent
rowIndex = rowIndex + 1
Loop
objExcel.Workbooks(1).Close
objExcel.Quit
End Sub
</script>

<form name="form">
<input type="file" name="userFile"/>
<input type="button" value="Read" name="readBtn"/>
<p><textarea rows="10" cols="44" name="excelContent"></textarea></p>
</form>

</apex:page>

 For more reference

http://www.activexperts.com/activmonitor/windowsmanagement/scripts/msoffice/excel/

 

 

Remember that the ActiveX will be unsigned, so user

will prompted wether to execute this  ActiveX or not.

But hey you can get the job done in client side itself

cause there is no way you can do it in server side.

 

Also only IE will support ActiveX, no other browser.

This was selected as the best answer
rcravenrcraven
Make sure that unsigned ActiveX controls are not blocked by your browswer:

In Internet Explorer. Go to, Tools -> Internet Options
Click on the 'Security' tab

Then seek the following:

'Download unsigned ActiveX controls'
- Set to 'Prompt'

Initialize and script ActiveX controls not marked as safe
- Set to 'Prompt'
GiangdtGiangdt

Hi All,

 

I tried to run this VF page but with no luck. I get this error message  "ActiveX component can't create object: 'Excel.Application'".

 

I already allowed my IE initiates and runs unsigned activex as follow.
 
'Download unsigned ActiveX controls'
- Set to 'Prompt'

Initialize and script ActiveX controls not marked as safe
- Set to 'Prompt'
 

Any ideas, please? 

 

Thanks,

Giang

 

NeetikaNeetika

Hi All,

 

This code is helped me...but my requirement to display the excel one column content in picklist..can any one help me on this?

SharathChandraSharathChandra

Not able to use vbscript in visual force page please help

 

When i do it in HTML page its working please help.

 

SharathChandraSharathChandra

Not able to read excel in visual force page please help i have done as the solution provided

bunnyrabbitbunnyrabbit

This can be done using Javascript also which is easy to integrate with apex code

Rameshvar Dayal 25Rameshvar Dayal 25
Hi All,
we have an excel file with multiple sheets, We have saved it in static resource now we want to read it from static resource and then we want to parse it using apex to store the sheet records in the custom object fields.
I required the solution immediately.
Thanks in advance.