Solved

Export in excel format

Posted on 2009-07-14
1
1,289 Views
Last Modified: 2013-11-11
Hi Experts,

                  I have a page that displays a chart and a table. I want to the table to shown in excel format on click of a link.
                   Below is my controller code. Both the table and Chart are rendered on the fly from request parameters.
                  model.put("detailData", detailRes);

                 the above variable detailData is the one one which contains the formatted  HTML Table. I want to show this variable in excel format.
   
package com.att.analytics.ui;
 
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
 
import javax.servlet.http.HttpServletRequest;
 
import net.sf.json.JSONObject;
 
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
import com.si.myworld.business.Chart;
import com.si.myworld.dao.ChartData;
 
 
@Controller
@RequestMapping("/chartDetail.html")
public class DetailChartController {
 
	private static final Logger log = Logger.getLogger(DetailChartController.class);
	
	@RequestMapping(method=RequestMethod.GET)
   public String getChart(HttpServletRequest request, ModelMap model) {
		String chartType = request.getParameter("chartType");
		String chartGroup = request.getParameter("chartGroup");
		String chartSubgroup = request.getParameter("chartSubgroup");
		String timeline = request.getParameter("timeline");
		String liabilityCode = request.getParameter("liabilityCode");
				
//		log.debug("Chart Type: "+chartType);
//		log.debug("Chart Group: "+chartGroup);
//		log.debug("Chart Subgroup: "+chartSubgroup);
//		log.debug("Timeline: "+timeline);
 
// build your report here
// using the code from the jsp
		
		System.out.println("Picked from request attribute: " + chartType);
		System.out.println("Picked from request attribute: " + chartGroup);
		System.out.println("Picked from request attribute: " + chartSubgroup);
		System.out.println("Picked from request attribute: " + timeline);
		System.out.println("Picked from request attribute: " + liabilityCode);
		
		//added on 7/2/09
		int id = ServletRequestUtils.getIntParameter(request, "chartId", 0);
		String startDt = request.getParameter("startDt");
		String endDt = request.getParameter("endDt");
		
		Chart crt = new Chart();
		crt.setChartGroup(chartGroup);
		crt.setChartSubgroup(chartSubgroup);
		crt.setTimeline(timeline);
		crt.setChartType(chartType);
		crt.setLiabilityCode(liabilityCode);
		
		//added by pp 7/2/09
		crt.setChartId(id);
		crt.setStartDt(startDt);
		crt.setEndDt(endDt);
		
		System.out.println("Values from Chart object: " + crt.getChartType() );
		System.out.println("Values from Chart object: " + crt.getChartGroup() );
		System.out.println("Values from Chart object: " + crt.getChartSubgroup() );
		System.out.println("Values from Chart object: " + crt.getTimeline());
		System.out.println("Values from Chart object: " + crt.getLiabilityCode());
		
		ChartData cd = new ChartData();
	    String chartXml = cd.getChartXml(crt);
		
		System.out.println("DetailChartController>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> chart XML" + chartXml);
		
		//added 7/2/09
		String chartDatasource = cd.fetchChartDatasourceName(crt.getChartSubgroup());		
		
		model.put("chartType", chartType);
		model.put("chartGroup", chartGroup);
		model.put("chartSubgroup", chartSubgroup);
		model.put("timeline", timeline);	
		model.put("liablityCode", liabilityCode);
		model.put("detailForm", crt);
			
		//pp on 7/2/09
		model.put("chartId", id);
		model.put("startDt", startDt);
		model.put("endDt", endDt);
		model.put("chartDatasource", chartDatasource);
 
		//detail data
		//Vector detailData = cd.getChartDetailData(crt); 
		//List detailData = cd.getChartDetailDataList(crt);
		//String chartDetailHtml = cd.getChartDetailHtml(cd.getChartDetailData(crt));
		//model.put("chartDetailHtml", chartDetailHtml);
		
		String detailRes = cd.getChartDetailDataNew(crt);
		model.put("detailData", detailRes);
		
		//breadcrumb
		String breadcrumb = crt.getChartGroup() + ">>" 	+ crt.getChartSubgroup();
		model.put("breadcrumb", breadcrumb);
		
    	return "TestDetail";
    }
 
	
	@RequestMapping(method=RequestMethod.POST)
	public String setChartPosition(HttpServletRequest request) {				
		return("redirect:/index.html");
	}
	
		
	@ModelAttribute("chartGroups")
	public Collection<String> populateGroups() {
		return populateSubgroups().keySet();
	}
 
	@ModelAttribute("chartSubgroups")
	public JSONObject getSubgroups() {
		return JSONObject.fromObject(populateSubgroups());
	}
 
	public Map<String, List<String>> populateSubgroups() {
		
		Map<String, List<String>> groups = new HashMap<String, List<String>>();
		groups.put("Payment", Arrays.asList("AUTOPAY", "TXT2PAY"));
		groups.put("Registration", Arrays.asList("REGISTRATION"));
		groups.put("UpgradeChanges", Arrays.asList(
		"UPGRADE_CHANGE"));
		
		return groups;
	}
 
	@ModelAttribute("chartTypes")
	public Collection<String> populateTypes() {
		return Arrays.asList("bar", "line");
	}
 
	@ModelAttribute("timelines")
	public Collection<String> populateTime() {
		return Arrays.asList("Daily", "Weekly", "Monthly", "Quarterly",
				"Annual");
	}
	
	@ModelAttribute("liabilityCodes")
	public Collection<String> populateCustomers() {
		return Arrays.asList("IRU", "CRU");
	}
	
	
}

Open in new window

0
Comment
Question by:aman0711
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 24854853
create another method that returns a different (excel) view

        @RequestMapping(value="/excel.html", method=RequestMethod.GET)
   public String getChart(HttpServletRequest request, ModelMap model) {
                String chartType = request.getParameter("chartType");
                String chartGroup = request.getParameter("chartGroup");
                String chartSubgroup = request.getParameter("chartSubgroup");
                String timeline = request.getParameter("timeline");
                String liabilityCode = request.getParameter("liabilityCode");
                               
//              log.debug("Chart Type: "+chartType);
//              log.debug("Chart Group: "+chartGroup);
//              log.debug("Chart Subgroup: "+chartSubgroup);
//              log.debug("Timeline: "+timeline);
 
// build your report here
// using the code from the jsp
               
                System.out.println("Picked from request attribute: " + chartType);
                System.out.println("Picked from request attribute: " + chartGroup);
                System.out.println("Picked from request attribute: " + chartSubgroup);
                System.out.println("Picked from request attribute: " + timeline);
                System.out.println("Picked from request attribute: " + liabilityCode);
               
                //added on 7/2/09
                int id = ServletRequestUtils.getIntParameter(request, "chartId", 0);
                String startDt = request.getParameter("startDt");
                String endDt = request.getParameter("endDt");
               
                Chart crt = new Chart();
                crt.setChartGroup(chartGroup);
                crt.setChartSubgroup(chartSubgroup);
                crt.setTimeline(timeline);
                crt.setChartType(chartType);
                crt.setLiabilityCode(liabilityCode);
               
                //added by pp 7/2/09
                crt.setChartId(id);
                crt.setStartDt(startDt);
                crt.setEndDt(endDt);
               
                System.out.println("Values from Chart object: " + crt.getChartType() );
                System.out.println("Values from Chart object: " + crt.getChartGroup() );
                System.out.println("Values from Chart object: " + crt.getChartSubgroup() );
                System.out.println("Values from Chart object: " + crt.getTimeline());
                System.out.println("Values from Chart object: " + crt.getLiabilityCode());
               
                ChartData cd = new ChartData();
            String chartXml = cd.getChartXml(crt);
               
                System.out.println("DetailChartController>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> chart XML" + chartXml);
                                 
                String detailRes = cd.getChartDetailDataNew(crt);
                model.put("detailData", detailRes);
               
               
        return "excel";
    }
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error in @AspectJ Based AOP with Spring 2 24
java mysql insert application 14 47
spring maven example issues 3 44
Java Inheritance super keyword use 8 38
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question