Thursday, 23 November 2017

Upload data and store in database

CSV files can be used to do bulk import. 

Open CSV:
It is a very simple csv (comma-separated values) parser library for Java. It was developed because all of current csv parsers I've come across don't have commercial-friendly licenses.  


Download Open CSV jar file from OpenCSV

Add jar to your portlet

Just write file tag in your jsp page
  
<aui:form action="<%=BulkImportURL.toString()%>" method="post" name="fm" enctype="multipart/form-data">
<aui:input name="QuestionAnswerAttach" id="QuestionAnswerAttach" type="file" label="Upload your Question Answer key"></aui:input>
<aui:button type="submit" value="Upload Questions"/>
</aui:form>


Then in your action class

Inside your method write

    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);
        java.io.File document = uploadPortletRequest.getFile("QuestionAnswerAttach");
        CSVReader readers = new CSVReader(new FileReader(document));
        List<String[]> myEntries = readers.readAll();
for(int i=0;i<myEntries.size();i++){
String[] s = myEntries.get(i);
System.out.println(s[0]); 
 
}

And you can store the value in database.

TEST

I am Java Developer. I have 6 year Experiance in this field and like to post in blogging. So keep sharing and like my post

1 comments:

  1. hello ma,am
    i have tried to implement this code but i am not apply to resolve the dependency.

    ReplyDelete

 

Copyright @ 2017 Liferay Article.