1

i tried using AbstractXlsxView of spring but it is only prompting to download application/octet-stream file type instead of an excel file.

ExcelBuilder.java

public class ExcelBuilder extends AbstractXlsxView {

    @Override
    protected void buildExcelDocument(Map<String, Object> model,
            Workbook workbook, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        Sheet latePaymentsSheet = workbook
                .createSheet("Late Payments");
        setLatePaymentHeader(latePaymentsSheet);

        @SuppressWarnings("unchecked")
        Map<String, Object> map = (Map<String, Object>) model.get("map");
        @SuppressWarnings("unchecked")
        List<Map<String, Object>> latePaymentsList = (List<Map<String, Object>>) map
                .get("latePayments");

        setLatePaymentsRow(latePaymentsSheet, latePaymentsList);
    }

Controller.java

@RequestMapping(value = "/export", method = RequestMethod.GET)
public ModelAndView exportToExcel() {

    Map<String,Object> map = Dao.getData()

 return new ModelAndView("ExcelBuilder", "map", map);
}

Excel-view.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.xsd">

    <bean id="ExcelBuilder" class="com.test.utility.ExcelBuilder">
    </bean>
</beans>
pat3ck029
  • 263
  • 1
  • 7
  • 19

2 Answers2

2

You can change your @RequestMapping and add the produces attribute.

@RequestMapping(value = "/export", method = RequestMethod.GET, produces="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")

See here for other Mime types.

Community
  • 1
  • 1
Renato Back
  • 706
  • 1
  • 10
  • 21
0

You can set file type in response as given below:

public ModelAndView exportPaymentReport(HttpServletResponse response) {
     response.setContentType( "application/ms-excel" );
     response.setHeader( "Content-disposition", "attachment;filename=myfile.xls" );
}
gudok
  • 4,029
  • 2
  • 20
  • 30
Rishabh
  • 1
  • 2