Spring MVC生成Excel格式數據

以下示例演示如何使用Spring Web MVC框架生成PDF格式的文件。首先使用Eclipse IDE,並按照以下步驟使用Spring Web Framework開發基於動態表單的Web應用程序:

  1. 創建一個名稱爲 GeneratePdf 的動態WEB項目。
  2. com.yiibai.springmvc 包下創建三個Java類:UserPDFViewPDFController
  3. 從maven存儲庫頁面下載Apache iText庫:Apache iText。 把它放在CLASSPATH中。
  4. 最後一步是創建所有源和配置文件的內容並運行應用程序,詳細如下所述。

完整的項目文件目錄結構如下所示 -

Spring

PDFController.java 的代碼如下所示 -

package com.yiibai.springmvc;
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 PDFController extends AbstractController {

   @Override
   protected ModelAndView handleRequestInternal(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      //user data
      Map<String,String> userData = new HashMap<String,String>();
      userData.put("100", "Xiao.Lu");
      userData.put("102", "User 102");
      userData.put("301", "User 301");
      userData.put("400", "User 400");
      return new ModelAndView("UserSummary","userData",userData);
   }
}

UserPDFView.java 的代碼如下所示 -

package com.yiibai.springmvc;
import java.util.Map;

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

import org.springframework.web.servlet.view.document.AbstractPdfView;

import com.lowagie.text.Document;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;

public class UserPDFView extends AbstractPdfView {

   protected void buildPdfDocument(Map<String, Object> model, Document document,
      PdfWriter pdfWriter, HttpServletRequest request, HttpServletResponse response)
      throws Exception {
      Map<String,String> userData = (Map<String,String>) model.get("userData");

      Table table = new Table(2);
      table.addCell("No.");
      table.addCell("User Name");

      for (Map.Entry<String, String> entry : userData.entrySet()) {
         table.addCell(entry.getKey());
         table.addCell(entry.getValue());
      }
      document.add(table);
   }
}

GeneratePdf-servlet.xml 配置如下所示 -

<beans xmlns="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">
   <bean
      class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

   <bean class="com.yiibai.springmvc.PDFController" />

   <bean class="org.springframework.web.servlet.view.XmlViewResolver">
      <property name="location">
         <value>/WEB-INF/views.xml</value>
      </property>
   </bean>
</beans>

views.xml 文件中的配置如下所示 -

<beans xmlns="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">

   <bean id="UserSummary" class="com.yiibai.springmvc.UserPDFView"></bean>
</beans>

在上面的代碼中,創建了一個PDFControllerUserPDFView類。iText庫是用來處理PDF文件格式,並將數據轉換爲PDF文檔。

完成創建源和配置文件後,發佈應用程序到Tomcat服務器。

現在啓動Tomcat服務器,當訪問URL => http://localhost:8080/GeneratePdf/pdf , 如果Spring Web應用程序沒有問題,應該看到以下結果:

Spring