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
salesusetosalesuseto 

Read and Write CSV file using Apex code

Hello Everyone,

I am very new to Salesforce.com, I need to read and write a CSV file using Apex code.

Functionality wants to implement : Read some records using query and write into a excel file and
read a CSV file and insert records into database.
this should be using apex code.

Please help me :)

Thanks
SuperfellSuperfell
Given that you need to ship the csv to/from a client desktop to use excel, is there any reason why you can't just use the Apex Data Loader, or the Excel Connector?
salesusetosalesuseto
Hello Simon,

Thanks for reply.

I want to develop a application which remove duplicates with selection control and write into CSV file which will store on client
desktop. Mainly using a Apex code.

If any idea about it, please reply.

Thanks


SankarSankar

Hi,

 

I am also held at the same point. Hope by now you could have solved the issue. Can you help me?

 

Thanks

anu08anu08

Hi All,

 

I am also struck with same senariao , can u any one help me 

 

here  I want When I click  a button  ,I want 2 enter bulk of data in a Excell sheet or CSV  and

After click  Submit button it will store which ever data enter in Excell, the same into SF

 

Vasu

KRSKRS

Hi,

 

  Did u manage to implement this ? If so could u pls share the code,would be really helpful.

TonnisthaTonnistha

Hi,

 

If any body is having the apex code bolck kindly share it...

 

 

Thnaks

Tonnistha

cantchanandcantchanand

 

<apex:form >
<apex:inputFile value="{!contentFile}" filename="{!nameFile}" />
<apex:commandButton action="{!ReadFile}" value="Read File" id="theButton"/>
</apex:form>

 

<apex:page controller="Test_ReadExcel_Cls">

<apex:form >

  <apex:inputFile value="{!contentFile}" filename="{!nameFile}" />

  <apex:commandButton action="{!ReadFile}" value="Read File" id="theButton"/>

 </apex:form>

</apex:page>

 

/////Test_ReadExcel_Cls

 

 

public class Test_ReadExcel_Cls 

{

public string nameFile{get;set;}

public Blob contentFile{get;set;}

public Test_ReadExcel_Cls()

{

}

public Pagereference ReadFile()

{

nameFile=contentFile.toString();

return null;

}

}

 

 

Try this 

cantchanandcantchanand

<apex:page controller="Test_ReadExcel_Cls">

<apex:form > 

  <apex:inputFile value="{!contentFile}" filename="{!nameFile}" /> 

  <apex:commandButton action="{!ReadFile}" value="Read File" id="theButton"/> 

 </apex:form>

</apex:page>

 

/////Test_ReadExcel_Cls

 

 

public class Test_ReadExcel_Cls 

{

 public string nameFile{get;set;}

 public Blob contentFile{get;set;}

 public Test_ReadExcel_Cls()

 {

 

 }

public Pagereference ReadFile()

{

nameFile=contentFile.toString();

return null;

}

}

Keith654Keith654

Here is a post on the subject of parsing CSV text in Apex that might save you a bit of time if you are implementing such a parser:

 

Parsing CSV while staying within the “total number of executed script statements” governor limit

 

Keith

ArunaAruna

Hi

 

i have same problem.

 

but some thing different i,e i need to read header column of excel file  and those fileds i have to display in vf page

 

and those filed i shuld be  map with the salesforce object  after mapping insert the excel data into object

 

can u please share me sample code how to create excel data and insert into salesforce object

 

Thank you

 

Chamil MadusankaChamil Madusanka

Hi,

 

Try this code,,


http://www.forcetree.com/2010/08/read-and-insert-records-from-csv-file.html

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

Chamil's Blog

Amit Singh1989Amit Singh1989

Hi cantchanand,

 

I am using the same code and that is working fine,

 

but i want to display namefile's data in a visualforce page (for pdf).

How to do this,

 

 

Thank you

Amit Singh

AnkurShukla.ax1238AnkurShukla.ax1238

@Amit Singh1989 create the regular visual force page and user the render as PDF option in Apex:Page tag.

 

e.g.

 

<apex:page standardController="Custom_Contacts__c" extensions="CustomContactsController" renderAs="PDF">

 

Thanks,

Ankur

PrajarikamPrajarikam

Hi cantchanand,

 

                           I have the same kind of requirement to read a CSV file  and by clicking Readfile button I want ajax loading functionality

when i click on Read file button i just want ajax loading functionality Ex :  procesing ....

I'm not able to get that requirement , can anyone help me out.

Regards,

Praveen.

 

URVASHIURVASHI

For Ajax you can use <apex:actionsupport> component.please refer to component reference for futher examples.

You can also use <apex:actionregion>.

Please let me know the scenario in which u wana use it.

I can help u futher.

SwayampravaSwayamprava
Hi I am trying to construct a csv file inside  the batch apex. It should be in .csv format how to do it

Divesh BafnaDivesh Bafna
Hi,
Can anyone help me in providing a link to a document or a sample code which explains on how to read csv files stored on s3 bucket from salesforce?
Thanks!
Terafina AdminTerafina Admin

If any body is having the apex code bolck kindly share it. Wanted to implement it in test suite to load data from a json file.