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


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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:xsi="" xmlns:p=""

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

<bean id="viewResolver"
<property name="basename" value="views" />

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


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

public class InventoryController {

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

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

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

Monday, February 1, 2010

Iphone NSDate and NSDateComponents

1. use components:fromDate:toDate:options: to determine the temporal difference between two dates in units other than seconds (which you could calculate with the NSDate method timeIntervalSinceDate

NSDate *startDate = ...; 
NSDate *endDate = ...;

NSCalendar *gregorian = [[NSCalendar alloc]

NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

NSDateComponents *components = [gregorian components:unitFlags
toDate:endDate options:0];
NSInteger months = [components month];
NSInteger days = [components day];

2. use the dateByAddingComponents:toDate:options: method to add components of a date (such as hours or months) to an existing date

NSDate *today = [[NSDate alloc] init]; 
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setHour:1];
[offsetComponents setMinutes:30];
// Calculate when, according to Tom Lehrer, World War III will end
NSDate *endOfWorldWar3 = [gregorian dateByAddingComponents:comps toDate:today options:0];