AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-excel: How to use csv file in java

Could you please explain the concept with examples

java x 210
excel x 2
Posted On : 2016-08-20 08:25:31.0
profile MOHAMMAD SALEEM BASHA - anyforum.in MOHAMMAD SALEEM BASHA
266150
up-rate
5
down-rate

Answers


I am going to share one example using jsp and servlet, Where we can upload excel(CSV) file in specified directory D:/upload. and we can print the data on console, If we can print the data, we can save in DB as well without any challenge.

index.jsp:
--------------------------------
<html>
<body>
<h1>Select the Excel file to upload</h1>
<form action="uploadExcel" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" value="Upload"/>
</form>
</body>
</html>


UploadExcel.java:
-------------------------------------------------
package in.anyforum;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class UploadExcel extends HttpServlet{

String saveFile="D:/upload/";

protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
File file=null;
try{
if(ServletFileUpload.isMultipartContent(request)){
DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory();
ServletFileUpload servletFileUpload=new ServletFileUpload(diskFileItemFactory);
@SuppressWarnings("unchecked")
List<FileItem> multiparts =servletFileUpload.parseRequest(request);

for(FileItem item : multiparts){
if(!item.isFormField()){
String name = new File(item.getName()).getName();
file=new File("D:/upload/"+File.separator + name);
item.write(file);
}
}
}
//Create Workbook instance holding reference to .xlsx file
org.apache.poi.ss.usermodel.Workbook workbook = WorkbookFactory.create(new FileInputStream(file));

//Get first/desired sheet from the workbook
org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0);

//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();

while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
//Check the cell type and format accordingly
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "t");
break;
}
}
System.out.println("");
}

}
catch (Exception e){
out.print(e);
e.printStackTrace();
}
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
processRequest(request, response);

}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
processRequest(request, response);
}

@Override
public String getServletInfo() {
return "Short description";
}

}

web.xml:
----------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>UploadExcel</servlet-name>
<servlet-class>in.anyforum.UploadExcel</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>UploadExcel</servlet-name>
<url-pattern>/uploadExcel</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


Note:
-------------------------
Use all latest and same version of below jar files:
* Apache-Commons.jar
* commons-io.jar
* xmlbeans.jar
* poi-finals.jar
* poi-ooxml.jar
* poi-ooxml-schemas.jar

Posted On : 2016-08-21 23:27:57
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523185319357
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in