+ Start a Discussion
Marcelo AgostinhoMarcelo Agostinho 

How can i export XLS using Apex Class?

Hello Guys... im here again to ask about exporting XLS.


For example i want to run a method that is associated to a commandButtom...


when i click in this commandButtom... this method is called and they will return the datas ready to export to a XLS file...


Can i do something like it?


write the values in text file as comma separated and save it locally with .csv exetnsion.


either you can use and call javascript function:"

<script language="javascript">
function WriteToFile()
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("C:\\Test.txt", true);
<body onLoad="WriteToFile()">

public PageReference export(){ String recStr = 'FirstName,'; recStr += 'Lastname' ; recStr += '\n' ; recStr +='RObin,'; recStr += 'Dicosta' ; recStr += '\n' ; Blob recBlob= Blob.valueOf(recStr); Attachment att= new attachment(); att.Name = 'MYExport ' + system.now() + '.csv'; att.Body =recBlob; insert att; PageReference exportPage = new ApexPages.StandardController(att).view(); exportPage.setRedirect(true); return exportPage; }