Spring mvc-Integration (integration)-Generate Excel sample (reprint practice)

Source: Internet
Author: User
Tags tomcat server tutorialspoint

The following content is translated from: https://www.tutorialspoint.com/springmvc/springmvc_excel.htm

Description: The sample is based on spring MVC 4.1.6.

The following example shows how to build Excel using the Spring WEB MVC framework. First, let's use the Eclipse IDE and follow these steps to develop a dynamic form-based Web application using the Spring Web framework:

Step Description
1 Create a project named TestWeb, under a package com.tutorialspoint, as described in the Spring Mvc-hello World Example section.
2 Create a Java class Userexcelview,excelcontroller under Com.tutorialspoint package.
3 Download Apache POI library Apache poi from the Maven repository page. Put it in your classpath.
4 The final step is to create the contents of all the source and configuration files and export the application as described below.

Excelcontroller.java

 PackageCom.tutorialspoint;ImportJava.util.HashMap;ImportJava.util.Map;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.AbstractController; Public classExcelcontrollerextendsAbstractcontroller {@OverrideprotectedModelandview handlerequestinternal (httpservletrequest request, httpservletresponse response)throwsException {//User DataMap<string,string> UserData =NewHashmap<string,string>(); Userdata.put ("1", "Mahesh"); Userdata.put ("2", "Suresh"); Userdata.put ("3", "Ramesh"); Userdata.put ("4", "Naresh"); return NewModelandview ("Usersummary", "UserData", UserData); }}

Userexcelview.java

 PackageCom.tutorialspoint;ImportJava.util.Map;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.apache.poi.hssf.usermodel.HSSFRow;ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;ImportOrg.springframework.web.servlet.view.document.AbstractExcelView; Public classUserexcelviewextendsAbstractexcelview {@Overrideprotected voidBuildexceldocument (map<string, object>model, Hssfworkbook workbook, httpservletrequest request, httpservletresponse response)throwsException {Map<String,String> UserData = (map<string,string>) model.get ("UserData"); //Create a WordsheetHssfsheet sheet = workbook.createsheet ("User report"); Hssfrow Header= Sheet.createrow (0); Header.createcell (0). Setcellvalue ("Roll No")); Header.createcell (1). Setcellvalue ("Name")); intRowNum = 1;  for(Map.entry<string, string>Entry:userData.entrySet ()) {            //Create the row dataHssfrow row = Sheet.createrow (rownum++); Row.createcell (0). Setcellvalue (Entry.getkey ()); Row.createcell (1). Setcellvalue (Entry.getvalue ()); }      }}

Testweb-servlet.xml

<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0. XSD Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.0.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.0.xsd ">   <Beanclass= "Org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />   <Beanclass= "Com.tutorialspoint.ExcelController" />   <Beanclass= "Org.springframework.web.servlet.view.XmlViewResolver">      < Propertyname= "Location">         <value>/web-inf/views.xml</value>      </ Property>   </Bean></Beans>

Views.xml

<Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:context= "Http://www.springframework.org/schema/context"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-3.0. XSD Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.0.xsd ">   <BeanID= "Usersummary"class= "Com.tutorialspoint.UserExcelView"></Bean></Beans>

Here we create a excelcontroller and a excelview. The Apache POI Library processes the Microsoft Office file format and transforms the data into an Excel document.

After you finish creating the source files and profiles, export the application. Right-click the application and use the Export > WAR file option and save your testweb.war file in the Tomcat WebApps folder.

Now start your Tomcat server and make sure you can access other pages from the WebApps folder using a standard browser. Now try the URL http://localhost:8080/TestWeb/excel, you should see the following results.

Maven Example:

Https://github.com/easonjim/5_java_example/tree/master/springmvc/tutorialspoint/test30

Spring mvc-Integration (integration)-Generate Excel sample (reprint practice)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.