📜  Spring MVC-生成Excel示例

📅  最后修改于: 2020-11-11 06:36:29             🧑  作者: Mango


以下示例显示了如何使用Spring Web MVC Framework生成Excel。首先,让我们拥有一个运行良好的Eclipse IDE,并遵循以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序。

Step Description
1 Create a project with a name TestWeb under a package com.tutorialspoint as explained in the Spring MVC – Hello World chapter.
2 Create Java classes UserExcelView and ExcelController under the com.tutorialspoint package.
3 Download the Apache POI library Apache POI from the maven repository page. Put it in your CLASSPATH.
4 The final step is to create the content of the source and configuration files and export the application as explained below.

ExcelController.java

package com.tutorialspoint;

import java.util.HashMap;
import java.util.Map;

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class ExcelController extends AbstractController {

   @Override
   protected ModelAndView handleRequestInternal(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      //user data
      Map userData = new HashMap();
      userData.put("1", "Mahesh");
      userData.put("2", "Suresh");
      userData.put("3", "Ramesh");
      userData.put("4", "Naresh");
      return new ModelAndView("UserSummary","userData",userData);
   }
}

UserExcelView.java

package com.tutorialspoint;

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 UserExcelView extends AbstractExcelView {

   @Override
   protected void buildExcelDocument(Map model,
      HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response)
      throws Exception {
      Map userData = (Map) model.get("userData");
      //create a wordsheet
      HSSFSheet sheet = workbook.createSheet("User Report");

      HSSFRow header = sheet.createRow(0);
      header.createCell(0).setCellValue("Roll No");
      header.createCell(1).setCellValue("Name");

      int rowNum = 1;
      for (Map.Entry entry : userData.entrySet()) {
         //create the row data
         HSSFRow row = sheet.createRow(rowNum++);
         row.createCell(0).setCellValue(entry.getKey());
         row.createCell(1).setCellValue(entry.getValue());
      }   
   }
}

TestWeb-servlet.xml


   
   

   

   
      
         /WEB-INF/views.xml
      
   

views.xml



   

在这里,我们创建了一个ExcelController和一个ExcelView。 Apache POI库处理Microsoft Office文件格式,并将数据转换为Excel文档。

完成创建源文件和配置文件后,导出应用程序。右键单击您的应用程序,使用“导出”→“ WAR文件”选项,并将TestWeb.war文件保存在Tomcat的webapps文件夹中。

现在,启动Tomcat服务器,并确保您能够使用标准浏览器从webapps文件夹访问其他网页。尝试URL- http:// localhost:8080 / TestWeb / excel ,我们将看到以下屏幕。

Spring Excel生成