Spring Abstractexcelview supports generating Excel files from templates. Specify the path to the template by setting the URL property of the view

Source: Internet
Author: User

Note:
1. The template needs to be placed in the Web-inf directory
2. When specifying a template path, you do not need to add an extension, and spring automatically adds the. xls to the URL property.
3. Set the ApplicationContext of the view before you specify the URL

1. Controller configuration Control-context.xml

1 <BeanID= "Beannameviewresolver"
2 class= "Org.springframework.web.servlet.view.BeanNameViewResolver" />
3
4<BeanID= "Viewcontroller"class= "Viewcontroller">
5 < Propertyname= "Datamodel">
6 <refBean= "Model-datamodel"/>
7 </ Property>
8 </Bean>
9
Ten <BeanID= "UrlMapping"
One class= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
A < Propertyname= "Mappings">
- <Props>
-    <propKey= "/view/*.report">Viewcontroller</prop>
the </Props>
- </ Property>
- </Bean>


2. Excelview.java of Excel View sub-class
implements the Buildexceldocument method in the Org.springframework.web.servlet.view.document.AbstractExcelView class.

1  Public classExcelviewextendsAbstractexcelview {
2
3 protected voidBuildexceldocument (map<string, object> model,
4Hssfworkbook WB, HttpServletRequest request, HttpServletResponse Reqponse)
5 throwsException {
6
7 //...
8Object[] Datas = (object[]) model.get ("DaTa ");
9 //...
Ten
OneHssfsheet sheet;
A //get the sheet 1 in template
-Sheet = wb.getsheetat (0);
- //Handle this sheet
the //...
-}
-}


3. Controller Viewcontroller Viewcontroller.java

1  Public classViewcontrollerextendsMultiactioncontroller{
2
3 PrivateDatamodel Datamodel;
4
5 Public voidSetdatamodel (Datamodel Datamodel) {
6 This. Datamodel = Datamodel;
7}
8
9 PublicModelandview exportToExcel (httpservletrequest request, httpservletresponse response)throwsException {
Ten
OneMap<string,object> model=NewHashmap<string,object> ();
A
-Object[] datas;
-
theDatas = DataModel.getDatas.toArray ();
-
-Model.put ("DaTa ", datas);
-
+Excelview Excelview =NewExcelview ();
-
+ Excelview.setapplicationcontext ( This. Getwebapplicationcontext ());
AExcelview.seturl ("/web-inf/template");
at
- return NewModelandview (Excelview,model);
-}
-}


4. Web. XML

1 <servlet>
2 <Servlet-name>Viewcontroller</Servlet-name>
3 <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>
4 <Init-param>
5 <Param-name>Contextconfiglocation</Param-name>
6 <Param-value>/web-inf/config/control-context.xml</Param-value>
7 </Init-param>
8 <Load-on-startup>1</Load-on-startup>
9</servlet>
Ten<servlet-mapping>
One <Servlet-name>Viewcontroller</Servlet-name>
A <Url-pattern>*.report</Url-pattern>
-</servlet-mapping>


5. Running
Http://hostname/view/exportToExcel.report

Spring Abstractexcelview supports generating Excel files from templates. Specify the path to the template by setting the URL property of the view

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.