Wednesday, February 3, 2010

how to use JExcel with spring 3 on google app engine

 

First, i tried org.apache.poi. It works in local but throw exception when i upload it to google app engine. Then i have to to JExcel, it works well till now.

step 1: setup your spring environment for your google app engine, please refer my previous article for this.

step 2: download jexcelapi, what i used is jexcelapi_2_6_12. Then copy jxl.jar into war/WEB-INF/lib/ and add it to build path.

step 3: configure web.xml to add spring configuration as below

    <servlet>
<servlet-name>web-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>web-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>



step 4: create web-dispatcher-servlet.xml which is required by spring looks like below:



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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"
>

<context:component-scan base-package="com.zpylxapp.controller.html" />

<bean id="viewResolver"
class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename" value="views" />
</bean>
</beans>



step 5: create views.properties file under your “src” directory and add following configuration



xl.(class)=com.zpylxapp.excel.HomePage



step 6: create com.zpylxapp.excel.HomePage view class



package com.zpylxapp.excel;

import java.util.List;
import java.util.Map;

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

import jxl.*;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.springframework.web.servlet.view.document.AbstractJExcelView;
public class HomePage extends AbstractJExcelView {

protected void buildExcelDocument(Map model, WritableWorkbook wb,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
WritableSheet sheet = wb.createSheet("Spring", 0);
sheet.addCell(new Label(0, 0, "Spring-Excel jexcel"));
List words = (List) model.get("wordList");
for (int i = 0; i < words.size(); i++) {
sheet.addCell(new Label(2 + i, 0, (String) words.get(i)));
}
}
}

step 7: create your controller use “return new ModelAndView("xl", map);” as return

@Controller
@RequestMapping("/inventory")
public class InventoryController {

@RequestMapping(value = "/{item}", method = RequestMethod.GET)
public ModelAndView getxls(Inventory item) {

Map map = new HashMap();
List wordList = new ArrayList();
wordList.add("hello");
wordList.add("world");
map.put("wordList", wordList);
return new ModelAndView("xl", map);
}
}



step 8: done! try it in your google app engine.

No comments:

Post a Comment