留言板
Excel file download in Liferay 6
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):
But the problem I am facing is, it's creating a blank excel file. Can anyone please help me on this.
Reference URL
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
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
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
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.
I used the following. And it worked
view.jsp
Action Class:
ExcelWriter.java
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<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<=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<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>