Download Excel file functionality in Hybris e-commerce | How to make a excel download in Hybris multi-suite

Download Excel file functionality in Hybris e-commerce | How to make a excel download in Hybris multi-suite

Friends there was a requirnment in Hybris project to create a http link to export data in excel format
implementation
First create a controller and from your controller follow below step

ExcelExportController.java
/**
 *
 */
package com.occ.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.occ.facade.export.ExportFacade;

@Controller
public class ExportController
{

@Autowired
private ExportFacade dlExportFacade;

@RequestMapping(value = "/v1/export", method = RequestMethod.GET)
protected ModelAndView export(final HttpServletRequest request, final HttpServletResponse response) throws Exception
{

final List<Map<String, String>> deal = dlExportFacade.getExportData();
return new ModelAndView("export", "Data", deal);


}

@RequestMapping(value = "/v1/Export", method = RequestMethod.GET)
protected ModelAndView Export(final HttpServletRequest request, final HttpServletResponse response) throws Exception
{

final String output = ServletRequestUtils.getStringParameter(request, "output");
final List<Map<String, String>> deal = dlExportFacade.getExportData();
if (output == null || "".equals(output))
{
//return normal view
// ModelAndView("ExcelExport", "Data", revenueData);
return new ModelAndView("ExcelExport", "Data", deal);

}
else if ("EXCEL".equals(output.toUpperCase()))
{
//return excel view
return new ModelAndView("ExcelExport", "Data", deal);

}
else
{
//return normal view
return new ModelAndView("ExcelExport", "Data", deal);

}
}
}


Facade Layer
DefaultExportFacade.java

/**
 *
 */
package com.occ.facade.export.impl;

import java.util.List;
import java.util.Map;

import com.occ.facade.export.ExportFacade;
import com.occ.service.ExportService;

public class DefaultExportFacade implements ExportFacade
{

/**
* @return the ExportService
*/
public ExportService getExportService()
{
return ExportService;
}

/**
* @param ExportService
*           the ExportService to set
*/
public void setExportService(final ExportService ExportService)
{
this.ExportService = ExportService;
}

public ExportService ExportService;

/*
* (non-Javadoc)
*
* @see com.occ.facade.export.ExportFacade#getExportData()
*/
@Override
public List<Map<String, String>> getExportData()
{

return ExportService.getExportData();
}
}

Service Layer 
ExportService.java
/**
 *
 */
package com.occ.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.occ.dao.ExportDao;


public class ExportService
{
@Autowired
private ExportDao dlExportDao;

public List<Map<String, String>> getExportData()
{
return dlExportDao.getExportData();
}
}

Excel view

/**
 *
 */
package com.occ.view;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;


public class ExportView extends AbstractExcelView
{

@Override
protected void buildExcelDocument(final Map model, final HSSFWorkbook workbook, final HttpServletRequest request,
final HttpServletResponse response) throws Exception
{

final List<Map<String, String>> revenueData1 = (List<Map<String, String>>) model.get("Data");

//create a wordsheet
final HSSFSheet sheet = workbook.createSheet(" Export");

final HSSFRow header = sheet.createRow(0);




int rowNum = 1;
for (int i = 0; i < revenueData1.size(); i++)
{
final HSSFRow row = sheet.createRow(rowNum++);
final Map<String, String> data = revenueData1.get(i);


}
}
}

Bean Configuration
 <bean class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location">
<value>WEB-INF/spring-excel-views.xml</value>
</property>
<property name="order" value="0" />
</bean>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix">
           <value>/WEB-INF/jsp/</value>
         </property>
        <property name="suffix">
           <value>.jsp</value>
        </property>
        <property name="order" value="1" />
    </bean>


spring-excel-views.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 <bean id="ExcelRevenueSummary" class="com.occ.controller.ExcelRevenueReportView"> </bean>
 <bean id="ExcelExport" class="com.occ.view.DealerExportView"> </bean>
</beans>

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.