Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AbstractExcelView is deprecated in Spring-based application

I want implement exporting data to Excel file in my Spring-Boot-based application.

When I intend use AbstractExcelView, but I know that AbstractExcelView is deprecated. What is the alternate solution for export Excel file?

like image 517
Viththal Joshi Avatar asked Jun 23 '16 06:06

Viththal Joshi


2 Answers

As of Spring 4.2, instead of AbstractExcelView you can try AbstractXlsView and its AbstractXlsxView and AbstractXlsxStreamingView variants

One of the example is given below

    import com.example.Course;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.springframework.web.servlet.view.document.AbstractXlsView;
    import org.apache.poi.ss.usermodel.Workbook;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.text.DateFormat;
    import java.util.List;
    import java.util.Map;

    public class XlsView extends AbstractXlsView {

        private static final DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.SHORT);

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

            // change the file name
            response.setHeader("Content-Disposition", "attachment; filename=\"my-xls-file.xls\"");

            @SuppressWarnings("unchecked")
            List<Course> courses = (List<Course>) model.get("courses");

            // create excel xls sheet
            Sheet sheet = workbook.createSheet("Spring MVC AbstractXlsView");

            // create header row
            Row header = sheet.createRow(0);
            header.createCell(0).setCellValue("ID");
            header.createCell(1).setCellValue("Name");
            header.createCell(2).setCellValue("Date");

            // Create data cells
            int rowCount = 1;
            for (Course course : courses){
                Row courseRow = sheet.createRow(rowCount++);
                courseRow.createCell(0).setCellValue(course.getId());
                courseRow.createCell(1).setCellValue(course.getName());
                courseRow.createCell(2).setCellValue(DATE_FORMAT.format(course.getDate()));
            }
        }
    }

Another example

import com.example.Course;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.servlet.view.document.AbstractXlsxStreamingView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.util.List;
import java.util.Map;

public class XlsxStreamingView extends AbstractXlsxStreamingView {

    private static final DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.SHORT);

    @Override
    protected void buildExcelDocument(Map<String, Object> model,
                                      Workbook workbook, HttpServletRequest request,
                                      HttpServletResponse response) throws Exception {
        // change the file name
        response.setHeader("Content-Disposition", "attachment; filename=\"my-xlsxStreaming-file.xlsx\"");

        @SuppressWarnings("unchecked")
        List<Course> courses = (List<Course>) model.get("courses");

        // create excel xls sheet
        Sheet sheet = workbook.createSheet("Spring MVC AbstractXlsxStreamingView");

        // create header row
        Row header = sheet.createRow(0);
        header.createCell(0).setCellValue("ID");
        header.createCell(1).setCellValue("Name");
        header.createCell(2).setCellValue("Date");

        // Create data cells
        int rowCount = 1;
        for (Course course : courses){
            Row courseRow = sheet.createRow(rowCount++);
            courseRow.createCell(0).setCellValue(course.getId());
            courseRow.createCell(1).setCellValue(course.getName());
            courseRow.createCell(2).setCellValue(DATE_FORMAT.format(course.getDate()));
        }
    }
}
like image 180
Mudassar Avatar answered Sep 26 '22 00:09

Mudassar


I would like to complete this example, to get it running from scratch:

Add the folowing to your Spring MVC Project.

File: src/views.properties

excelView2.(class)=YourPackageName.XlsxStreamingView


File: WEB-INF/your_Spring_Config.xml
add an 'ResourceBundleViewResolver' with reference to 'views.properties'

<bean id="viewResolver1" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
 <property name="order" value="1" />
 <property name="basename" value="views" />
</bean>
<bean id="viewResolver2"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="order" value="2" />
 <property name="prefix" value="/WEB-INF/view/" />
 <property name="suffix" value=".jsp" />
</bean>

File: src/YourPackageName/Course.java

public class Course {
 int Id;
 String Name;
 Date Date;
 .. getter and setter
}

File: src/YourPackageName/MyController.java

package YourPackageName.MyController;
@Controller
@RequestMapping("/example")
public class CustomerController {
...
    @GetMapping("downloadExcel")
    public ModelAndView downloadExcel() {
        System.out.println("inside downloadExcel");
        // create some sample data

        List<Course> listCourses = new ArrayList<Course>();
        listCourses.add(new Course(1, "Polarfrosch100", new Date()));
        listCourses.add(new Course(2, "Polarfrosch101", new Date()));
        listCourses.add(new Course(3, "Polarfrosch102", new Date()));

        // return a view which will be resolved by an excel view resolver
        return new ModelAndView("excelView2", "courses", listCourses);
    }
}

File: WEB-INF/view/your.jsp

...
<a href="${pageContext.request.contextPath}/example/downloadExcel">Download Excel Document</a>
like image 40
Polarfrosch100 Avatar answered Sep 26 '22 00:09

Polarfrosch100