留言板

Excel file download in Liferay 6

thumbnail
Subhasis Roy,修改在10 年前。

Excel file download in Liferay 6

Expert 帖子: 275 加入日期: 12-1-20 最近的帖子
Hi,
I am trying to download the excel file in LiferayPortal 6. I have used the following approach to download the excel.

1. create resourceURL in jsp page
2. Create handler in PortletAction class
3. Right code

In Step3 I wrote the following code (This "exportReturnRequest" method is getting called from PortletAction class):

public static FileOutputStream exportReturnRequest(ResourceRequest resourceRequest,ResourceResponse resourceResponse)	throws IOException {

               HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("Sample");
		Row row = sheet.createRow(0);				
		Cell cell = row.createCell(0);
		cell.setCellValue("TestValue");
		
		FileOutputStream out = null;
		File file = null;	    
		try {
			
			DateFormat dateFormat = new SimpleDateFormat("yy.MM.dd-HH.mm.ss");
			Date date = new Date();
			String fileName = dateFormat.format(date);		
			file = new File("SampleFile.xls");
			out = new FileOutputStream(file);
		
			resourceResponse.setContentType("application/vnd.ms-excel");
			resourceResponse.setProperty("Content-Disposition", "attachment; filename=\""+ file.getName() + "\"");		
		    workbook.write(out);
			
		} catch (FileNotFoundException e) {			
		    e.printStackTrace();
		} catch (IOException e) {			
		    e.printStackTrace();
		}
				
		return out;
}


But the problem I am facing is, it's creating a blank excel file. Can anyone please help me on this.

Reference URL
thumbnail
Manupoti Subrahmanyam,修改在10 年前。

RE: Excel file download in Liferay 6

Junior Member 帖子: 39 加入日期: 13-4-12 最近的帖子
Hi,

1.Please follow the url to download Excel sheet or PDF

https://www.liferay.com/community/wiki/-/wiki/Main/Generate+PDF+File+in+Portlet

2.Use serveResouce() method instead of processAction.

3.Add "poi3.jar" file
Ex:
Select Your project--->Build Path--->Add External Achieves-->select jar file


Regards
Subrahmanyam
thumbnail
Subhasis Roy,修改在10 年前。

RE: Excel file download in Liferay 6

Expert 帖子: 275 加入日期: 12-1-20 最近的帖子
Manupoti Subrahmanyam:
Hi,

1.Please follow the url to download Excel sheet or PDF

https://www.liferay.com/community/wiki/-/wiki/Main/Generate+PDF+File+in+Portlet

2.Use serveResouce() method instead of processAction.

3.Add "poi3.jar" file
Ex:
Select Your project--->Build Path--->Add External Achieves-->select jar file


Regards
Subrahmanyam


Hi Subrahmanyam,

I have used serveResource and faced one problem. I have not wrote processAction. Its creating blank file for me. But in the tomcat "bin" I am getting a properly written file.
I checked the URL you provided. Its seems working fine.

Reference_serveResource

Thanks a lot for your help.
thumbnail
Subhasis Roy,修改在10 年前。

RE: Excel file download in Liferay 6

Expert 帖子: 275 加入日期: 12-1-20 最近的帖子
I used the following. And it worked

view.jsp

<portlet:resourceurl id="exportAction" var="exportURL" />
<a href="<%=exportURL%>"> Export User Data </a>

//Setting list in session
PortletSession sessionSession = renderRequest.getPortletSession();
sessionSession.setAttribute("getUsersList", userList, PortletSession.APPLICATION_SCOPE);




Action Class:


public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse){
		PortletSession pSession = resourceRequest.getPortletSession();
		List<searchobjectbean> claimCodeUsersList = new ArrayList<searchobjectbean>();
		if (pSession!= null)
		{
			claimCodeUsersList = (List<searchobjectbean>) pSession.getAttribute("getUsersList",PortletSession.APPLICATION_SCOPE);				
			logger.debug("claimCodeUsersList in code :: "+claimCodeUsersList);
		}		
		try {
			logger.debug("Calling new exportReturnRequest...." );  
			ExcelWriter.exportReturnRequest(resourceRequest,resourceResponse,(ArrayList<searchobjectbean>)usersList);
		} catch (IOException e) {			
			e.printStackTrace();
		}
	}
</searchobjectbean></searchobjectbean></searchobjectbean></searchobjectbean>


ExcelWriter.java


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.servlet.HttpHeaders;

public class ExcelWriter {

	public static void exportReturnRequest(ResourceRequest resourceRequest,ResourceResponse resourceResponse,ArrayList<searchobjectbean> claimCodeUsersList)
	throws IOException {
		
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("UserData");

		try{
			
			Row headerRow = sheet.createRow(0);
			Cell headerCell = null;
			CellStyle style = workbook.createCellStyle();
			Font font = workbook.createFont();
			font.setBoldweight(Font.BOLDWEIGHT_BOLD);
			style.setFont(font);
			logger.debug("Creating header cells....");
			for(int l = 0;l&lt;7;l++){
				
				headerCell = headerRow.createCell(l);
				headerCell.setCellStyle(style);
				
			    if(l == 0){
			    	headerCell.setCellValue("UserID");
				}else if(l==1){
					headerCell.setCellValue("Email Address");
				}else if(l==2){
					headerCell.setCellValue("Start Date");
				}else if(l==3){
					headerCell.setCellValue("End Date");
				}else if(l==4){
					headerCell.setCellValue("First Name");
				}else if(l==5){
					headerCell.setCellValue("Last Name");
				}
			}
			
		
			for(int i =1; i&lt;=claimCodeUsersList.size();i++){		    
				
				SearchObjectBean objSearchObjectBean = usersList.get(i-1);				
			
				Row row = sheet.createRow(i);
				Cell cell = null;
				logger.debug("Creating data cells for Row Number ["+ (i+1) +"]...");
				for(int j = 0; j&lt;7;j++){
					
					cell = row.createCell(j);
					if(null != objSearchObjectBean .getUserId()){
						if(j == 0){							
							cell.setCellValue(objSearchObjectBean .getUserId().toString());
						}else if(j==1){							
							cell.setCellValue(objSearchObjectBean .getEmailAddress());
						}else if(j==2){							
							cell.setCellValue(objSearchObjectBean .getStartDate());
						}else if(j==3){							
							cell.setCellValue(objSearchObjectBean .getEndDate());
						}else if(j==4){							
							cell.setCellValue(objSearchObjectBean .getFirstName());
						}else if(j==5){							
							cell.setCellValue(objSearchObjectBean .getLastName());
						}else{
							cell.setCellValue("");
						}
					}
				}
				
			
			}
		}
		catch(NullPointerException e){
			 e.printStackTrace();
		}
		
			
		OutputStream outStrm = null;
		try {
			
			resourceResponse.setContentType("application/vnd.ms-excel");
			resourceResponse.addProperty(HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate");
			
            outStrm = resourceResponse.getPortletOutputStream();
            workbook.write(outStrm);
            
			
		} catch (FileNotFoundException e) {			
		    e.printStackTrace();
		} catch (IOException e) {			
		    e.printStackTrace();
		}
		
		finally{
			try {
				logger.debug("Inside finally.....");	
				outStrm.flush();
	            outStrm.close();
			} catch (IOException e) {				
				e.printStackTrace();
			}
		}
	
	}
}</searchobjectbean>
Jesus Gonzalez,修改在8 年前。

RE: Excel file download in Liferay 6

New Member 帖子: 14 加入日期: 16-1-25 最近的帖子
Thanks a lot emoticon