Reading data from excel sheet using liferay/Java
Consider the situation where i need to read the data from EXCEL sheet.
So , If you have an input of type "FILE" where you need to upload the Excel sheet and Submit button to submit the file as below code.
In JSP page
Create action URL and file type to submit the file as show the below
PortletURL actionURL = renderResponse.createActionURL();
actionURL.setParameter("_spage", "/portlet_action/excel/excel");
<form method ="post" action = "${actionURL}" type"multipart/encrpt><input type ="file" name"filename"/><input type ="submit"/>
</form>
In Class i am reading the Excel sheet as follows as ,
In Class
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart) { //Checking the Type of file
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
log.error(e);
}
StringBuilder errorBuilder = new StringBuilder();
Iterator itr = items.iterator();
while (itr.hasNext())
{
FileItem item = (FileItem) itr.next(); //Since form is of type encrypt we should follow this procedure
if(!item.isFormField()) {
String itemName = item.getName();
String fieldName = item.getFieldName();
if(fieldName.equalsIgnoreCase("file")) {
File file = new File(itemName);
try {
item.write(file);
InputStream ios = new FileInputStream(file);
//Here the actual coding starts
HSSFWorkbook workbook = new HSSFWorkbook(ios);
HSSFSheet sheet = workbook.getSheetAt(0); // Getting the first Sheet
Iterator rows = sheet.rowIterator(); // Getting the rows
int rowCount = 0;
while (rows.hasNext()) { //Iterating the Rows
HSSFRow row = (HSSFRow) rows.next(); // Getting the First Row
for(int i=0;i<15;i++){ //Iterating the First rows of 15 cells (Here i am assuming that i have filled only 15 cells for row
HSSFCell cell = (HSSFCell) row.getCell(i); //Getting the first Cell
if(cell!=null){
val = cell.getRichStringCellValue().toString(); //Getting first cell value
System.out.println("Value is "+ val);
}
}
// Import Statements i have used in this class is :
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
for first 5 imports
https://drive.google.com/file/d/0B1c-cxUeQg5Oa0o3czdsSlY4dVU/edit?usp=sharing
for last 4 imports
https://drive.google.com/file/d/0B1c-cxUeQg5OY2p3SHU3aXVULTA/edit?usp=sharing
// Import Statements i have used in this class is :
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
for first 5 imports
https://drive.google.com/file/d/0B1c-cxUeQg5Oa0o3czdsSlY4dVU/edit?usp=sharing
for last 4 imports
https://drive.google.com/file/d/0B1c-cxUeQg5OY2p3SHU3aXVULTA/edit?usp=sharing
I want the jar file to be included?
ReplyDeleteI have provided links to download the jar files.
Deletehttps://www.liferay.com/community/forums/-/message_boards/message/47798332
ReplyDeleteThere is definitely a great deal to find out about this issue. I really like all of the points you've made. Official statement: How To Lock Files.
ReplyDelete