You need to sign in to do that
Don't have an account?
Csv file values displayed in vf page
Hi all,
For displaying csv file data in to vf apge ,for this i can write the controller and page as below
controller
public class uploadCSVcontroller
{
public string name{set;get;}
public Blob contentFile { get; set; }
public String nameFile { get; set; }
public Integer rowCount { get; set; }
public Integer colCount { get; set; }
public List<List<String>> getResults()
{
List<List<String>> parsedCSV = new List<List<String>>();
rowCount = 0;
colCount = 0;
if (contentFile != null)
{
String fileString = contentFile.toString();
parsedCSV = parseCSV(fileString, False);
rowCount = parsedCSV.size();
for (List<String> row : parsedCSV)
{
name=row[2];
system.debug('??????????????????'+name);
for(string name:row)
if (row.size() > colCount)
{
colCount = row.size();
}
}
}
return parsedCSV;
}
/*public Pagereference CreatePDF()
{
pagereference pr = new pagereference('/apex/FinalReport1');
pr.setredirect(true);
return pr;
}*/
public static List<List<String>> parseCSV(String contents,Boolean skipHeaders)
{
List<List<String>> allFields = new List<List<String>>();
contents = contents.replaceAll(',"""',',"DBLQT').replaceall('""",','DBLQT",');
contents = contents.replaceAll('""','DBLQT');
List<String> lines = new List<String>();
try
{
lines = contents.split('\r'); // using carriage return accomodates windows, unix, and mac files
}
catch (System.ListException e)
{
System.debug('Limits exceeded?' + e.getMessage());
}
Integer num = 0;
for(String line: lines)
{
if (line.replaceAll(',','').trim().length() == 0) break;
List<String> fields = line.split(',');
List<String> cleanFields = new List<String>();
String compositeField;
Boolean makeCompositeField = false;
for(String field: fields)
{
if (field.startsWith('"') && field.endsWith('"'))
{
cleanFields.add(field.replaceAll('DBLQT','"'));
}
else if (field.startsWith('"'))
{
makeCompositeField = true;
compositeField = field;
}
else if (field.endsWith('"'))
{
compositeField += ',' + field;
cleanFields.add(compositeField.replaceAll('DBLQT','"'));
makeCompositeField = false;
}
else if (makeCompositeField)
{
compositeField += ',' + field;
}
else
{
cleanFields.add(field.replaceAll('DBLQT','"'));
}
}
allFields.add(cleanFields);
}
if (skipHeaders) allFields.remove(0);
return allFields;
}
}
page as
<apex:page controller="uploadCSVcontroller">
<apex:form >
<apex:pageMessages id="pm"/>
<apex:inputFile value="{!contentFile}" filename="{!nameFile}"/>
<apex:commandButton value="Display" id="theButton"/>
<apex:pageBlock >
<apex:outputPanel id="results">
<p>nameFile: {!nameFile}</p>
<p>rowCount: {!rowCount}</p>
<p>colCount: {!colCount}</p>
<table title="CSV Output" border="0" width="100%">
<apex:repeat value="{!results}" var="row">
<tr>{!name}</tr>--------> for this dispalyed last value .But i want to display all the values in csv file.
<tr>
<apex:repeat value="{!row}" var="cell"> For example:i have values in csv file as
<td> {!cell} </td>
</apex:repeat> name city address
</tr>
</apex:repeat> aaa aa aaa
</table> bbb bb bbb
</apex:outputPanel> ccc cc ccc
</apex:pageBlock> ddd dd ddd
</apex:form> In vf page displayed csv file as it is.It is ok .But i want to get the value in <tr>{!name}</tr>------>
</apex:page> In this tag displayed the value as ddd ddd ddd ddd like.
But i want to display in the following form
aaa
bbb
ccc
ddd
Any one can u help me plz.
Thanks in advance.
Hi test269,
Use this Class:
public string name{set;get;}
public Blob contentFile { get; set; }
public String nameFile { get; set; }
public Integer rowCount { get; set; }
public Integer colCount { get; set; }
public List<List<String>> getResults(){
List<List<String>> parsedCSV = new List<List<String>>();
rowCount = 0;
colCount = 0;
if (contentFile != null)
{
String fileString = contentFile.toString();
parsedCSV = parseCSV(fileString, False);
rowCount = parsedCSV.size();
for (List<String> row : parsedCSV)
{
name=row[2];
system.debug('??????????????????'+name);
for(string name:row)
if (row.size() > colCount)
{
colCount = row.size();
}
}
}
return parsedCSV;
}
public static List<List<String>> parseCSV(String contents,Boolean skipHeaders){
List<List<String>> allFields = new List<List<String>>();
contents = contents.replaceAll(',"""',',"DBLQT').replaceall('""",','DBLQT",');
contents = contents.replaceAll('""','DBLQT');
List<String> lines = new List<String>();
try
{
lines = contents.split('\r'); // using carriage return accomodates windows, unix, and mac files
}
catch (System.ListException e)
{
System.debug('Limits exceeded?' + e.getMessage());
}
Integer num = 0;
for(String line: lines)
{
if (line.replaceAll(',','').trim().length() == 0) break;
List<String> fields = line.split(',');
List<String> cleanFields = new List<String>();
String compositeField;
Boolean makeCompositeField = false;
for(String field: fields)
{
if (field.startsWith('"') && field.endsWith('"'))
{
cleanFields.add(field.replaceAll('DBLQT','"'));
}
else if (field.startsWith('"'))
{
makeCompositeField = true;
compositeField = field;
}
else if (field.endsWith('"'))
{
compositeField += ',' + field;
cleanFields.add(compositeField.replaceAll('DBLQT','"'));
makeCompositeField = false;
}
else if (makeCompositeField)
{
compositeField += ',' + field;
}
else
{
cleanFields.add(field.replaceAll('DBLQT','"'));
}
}
allFields.add(cleanFields);
}
if (skipHeaders) allFields.remove(0);
return allFields;
}
}
And Page
--------------------
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.
Thanks
Thanks for ur reply.In the above code i want to call {!name} only in vf Page.
some this i want to assign like this;
name=row[0];
city=row[1];
Any one can u please help me this.
Thanks in advance.