![]() | ![]() | ![]() | ![]() | ![]() |
This sample illustrates how to add an action to the pop-up menu of the column headers of a TableView in order to directly export the contents of the entire table to Microsoft Excel. This sample creates a TableViewComposite, which is a TransformationBeanTM that is composed of other TransformationBeans that include the following subcomponents: MenuBar, NavigationBar, TableView, Title and Footer.
Here are the steps for creating a simple TableView:
The code shown on the Full Code tab of this sample is JSP/servlet code that creates a TableModel and ActionProvider and then creates a TableViewComposite tag with the TableModel and ActionProvider. The ActionProvider includes the Direct Export to Excel command.
The sample is based on the Model 2 Servlet template that is available in SAS AppDev Studio.
For simplicity, this sample uses its own static data model for the TableView. In most real-world use cases, users will connect to live data and not create their own data model. For more information about how to connect the TableView to live data, see Sample 25997.
Customizations
This sample uses the available export commands to export the entire TableView contents to Microsoft Excel. The contents of the TableView is directly exported to Microsoft Excel without displaying the Export Selector.
There's a specific export command for each of the supported export output types:
This sample shows how to use all of these export commands.
Export to Tab Separated Values is shown in the servlet code. Make sure the BaseCommand in the servlet is an ExportToTabSeparatedCommand:
BaseCommand command = new ExportToTabSeparatedCommand();
|
To use comma-separated values, change the BaseCommand in the servlet file to an ExportToCSVCommand:
BaseCommand command = new ExportToCSVCommand();
|
To export to XML, change the BaseCommand in the servlet file to an ExportToExcelXMLCommand:
BaseCommand command = new ExportToExcelXMLCommand();
|
To export to HTML, perform these tasks:
BaseCommand command = new ExportToHTMLCommand();
|
actionProvider.executeCommand(request, response, response.getWriter());
// to correctly export non english characters
|
To also export your HTML styles, add the following:
import com.sas.dataselectors.export.HTMLExportCommandsAttributeNames;
import com.sas.dataselectors.export.HTMLExportCSSInfoInterface;
import com.sas.dataselectors.export.HTMLExportCSSInfo;
import com.sas.entities.AttributeSetException;
import info.UrlInfo;
|
customAction.setActionType("directExportToExcelAction");
HTMLExportCSSInfoInterface[] cssInfo = {new HTMLExportCSSInfo()};
String absoluteCssURL = "";
String cssURL = cssInfo[0].getCSSURL();
// add . in front of the default cssURL got from the HTMLExportCSSInfo object
if ( cssURL.startsWith("/") )
cssURL = ((new StringBuffer(".")).append(cssURL)).toString();
if ( ! UrlInfo.isAbsoluteURL(cssURL) )
// transforms the relative URL ./styles/sasComponents.css
// into an absolute one http://localhost:8082/TableView/./styles/sasComponents.css
absoluteCssURL = UrlInfo.getAbsoluteURL(cssURL, request);
else
absoluteCssURL = cssURL;
// sets the absolute URL of the .css file
cssInfo[0].setCSSURL(absoluteCssURL);
try
|
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Tip: For help with building a Web application project and testing a Web application, see SAS Note 32218.
<%@ taglib uri="http://www.sas.com/taglib/sas" prefix="sas" %>
<%@ page pageEncoding="UTF-8"%>
<html>
<head>
<link href="styles/sasComponents.css" rel="STYLESHEET" type="text/css">
</head>
<body>
<sas:TableViewComposite ref="tableView" scope="session" render="true">
<sas:RelationalMenuBar />
</sas:TableViewComposite>
</body>
</html>
|
package servlets;
public class Model2ExampleControllerServlet.java ...sas_actionProvider.setControllerURL(request.getContextPath()+ "/Model2Example");
RequestDispatcher rd = getServletContext().getRequestDispatcher("/Model2ExampleViewer.jsp");
package servlets;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import com.sas.actionprovider.ActionList;
import com.sas.actionprovider.ActionOrderList;
import com.sas.actionprovider.Area;
import com.sas.actionprovider.BaseAction;
import com.sas.actionprovider.HttpAction;
import com.sas.actionprovider.HttpActionProvider;
import com.sas.actionprovider.support.ActionProviderSupportTypes;
import com.sas.actionprovider.support.tableview.HttpTableViewSupport;
import com.sas.commands.BaseCommand;
import com.sas.dataselectors.export.HTMLExportCommandsAttributeNames;
import com.sas.entities.AttributeDescriptorInterface;
import com.sas.entities.AttributeSetException;
import com.sas.servlet.commands.jdbc.export.ExportToTabSeparatedCommand;
import com.sas.servlet.tbeans.tableview.html.TableView;
import com.sas.servlet.tbeans.tableview.html.TableViewComposite;
import com.sas.servlet.tbeans.tableview.html.TableViewCompositeKeysInterface;
import com.sas.swing.util.Action;
public class Model2ExampleControllerServlet extends
javax.servlet.http.HttpServlet {
// Declare a default version ID since parent class implements
// java.io.Serializable
private static final long serialVersionUID = 1L;
/*
* doPost() Respond to the Post message.
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Note: Calling doGet to provide same behavior to POST and GET HTTP
// methods.
doGet(request, response);
}
/*
* doGet() Respond to the Get message.
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Note: Add User DO_GET code here
HttpSession session = request.getSession();
// Setup the ActionProvider
HttpActionProvider actionProvider = null;
synchronized (session) {
if (session != null) {
actionProvider = (HttpActionProvider) session
.getAttribute("actionProvider");
}
// if ActionProvider is null, create one and put it on the session
if (actionProvider == null) {
actionProvider = new HttpActionProvider();
actionProvider.setLocale(request.getLocale());
actionProvider.setControllerURL(request.getContextPath()
+ "/Model2Example");
actionProvider.setName("actionProvider");
// store object in its scope
if (session != null)
session.setAttribute("actionProvider", actionProvider);
}
// else execute the ActionProvider command
else {
actionProvider.executeCommand(request, response, response
.getWriter());
}
}
TableViewComposite tableView = null;
synchronized (session) {
if (session != null) {
tableView = (TableViewComposite) session
.getAttribute("tableView");
}
if (null == tableView) {
tableView = new TableViewComposite();
String columnNames[] = { "Name", "Age", "Sex", "Hobby" };
String data1[] = { "Bob", "42", "M", "Sailing" };
String data2[] = { "Mary", "33", "F", "Gardening" };
String data3[] = { "Jane", "27", "F", "Shopping" };
String data4[] = { "Tom", "65", "M", "Reading" };
String data5[] = { "Dave", "23", "M", "Skiing" };
Object data[][] = new Object[5][4];
data[0] = data1;
data[1] = data2;
data[2] = data3;
data[3] = data4;
data[4] = data5;
TableModel defaultTableModel = new DefaultTableModel(data,
columnNames);
tableView.setModel(defaultTableModel);
// set the actionProvider on the table
tableView.setActionProvider(actionProvider);
session.setAttribute("tableView", tableView);
}
}
/* Add the action to the pop menu of the tableview column headers */
HttpAction actionCreated = (HttpAction) session
.getAttribute("sas_customAction_justCreated");
// creates a new custom action and put it in the session
if (actionCreated == null) {
BaseCommand command = new ExportToTabSeparatedCommand();
HttpAction customAction = new HttpAction(command);
// associates a unique type to the action
customAction.setActionType("directExportToExcelAction");
// sets the name of the action displayed in the menu
customAction.putValue(Action.NAME, "Direct Export to Excel");
// the action is a custom one
customAction.setCustom(true);
/* Define a new action attribute, "model", */
/* that corresponds to the command�????�???�??�?�¢??s "model" attribute. */
/* Must mark the attribute as visible=false */
/* because it is not possible to pass its value as a String */
/*
* parameter on the request. Instead it will be stored on the
* HttpActionProvider
*/
/* and set on command just prior to execution */
try {
customAction.setAttribute("model", tableView.getModel());
AttributeDescriptorInterface adi = customAction
.getAttributeDescriptor("model");
adi.setVisible(false);
// tell the action provider support class that this value is
// already set.
adi.setModifiable(false);
adi.setSupplementalProperty(BaseAction.DYNAMIC, Boolean.FALSE);
// puts the action in the session
session.setAttribute("sas_directExportToExcelAction",
customAction);
// puts a flag in the session indicating a new action has been
// created,
// so that the JSP will register the action to the action
// provider only once
session.setAttribute("sas_customAction_justCreated", "");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
if (actionProvider != null) {
/* Add the action only once */
if (session.getAttribute("sas_customAction_justCreated") != null) {
session.removeAttribute("sas_customAction_justCreated");
HttpAction customAction = (HttpAction) session
.getAttribute("sas_directExportToExcelAction");
if (customAction != null) {
/* Register the tableview as the only viewer of this action */
if (tableView != null) {
TableView viewer = (TableView) tableView
.getComponent(TableViewCompositeKeysInterface.TABLEVIEW_TABLEDATA);
if (viewer != null) {
/* Register the viewer to the custom action */
try {
customAction
.setAttribute(
HTMLExportCommandsAttributeNames.VIEW_COMPONENT,
viewer);
} catch (AttributeSetException e) {
e.printStackTrace();
}
AttributeDescriptorInterface adi = customAction
.getAttributeDescriptor(HTMLExportCommandsAttributeNames.VIEW_COMPONENT);
adi.setVisible(false);
/*
* Register the action to the action provider and
* specify the area of the viewer that the action is
* valid.
*/
java.util.Vector viewers = new java.util.Vector(1);
viewers.add(viewer);
/*
* setAction() on COLUMN_HEADER_AREA would display
* the action in the column header together with the
* column name
*/
actionProvider
.setAction(
customAction,
viewers,
new Area(
HttpTableViewSupport.COLUMN_HEADER_AREA));
/*
* We want the action be displayed in the pop menu
* of a column header, not in the column header
* itself
*/
ActionOrderList alist = actionProvider
.getActionOrderList(
ActionProviderSupportTypes.TABLEVIEW_SUPPORT,
viewer,
HttpTableViewSupport.COLUMN_HEADER_AREA);
if (alist != null) {
/*
* Remove the action (position 1) from the
* column header
*/
alist.remove(1);
/*
* Add the action to the bottom of the popup
* menu of the column header
*/
ActionOrderList editlist = (ActionOrderList) (alist
.get(0));
if (editlist != null) {
editlist.add(ActionList.SEPARATOR);
editlist.add("directExportToExcelAction");
}
}
}
}
}
}
}
RequestDispatcher rd = getServletContext().getRequestDispatcher(
"/Model2ExampleViewer.jsp");
rd.forward(request, response);
}
}
|
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.






| Type: | Sample |
| Date Modified: | 2008-06-30 16:43:51 |
| Date Created: | 2006-01-06 16:32:31 |
| Product Family | Product | Host | Product Release | SAS Release | ||
| Starting | Ending | Starting | Ending | |||
| SAS System | SAS AppDev Studio | Microsoft Windows 2000 Server | 3.2 | 9.1 TS1M3 SP4 | ||
| Microsoft Windows 2000 Datacenter Server | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows 2000 Advanced Server | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft® Windows® for x64 | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows 2000 Professional | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows Server 2003 Datacenter Edition | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows NT Workstation | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows Server 2003 Enterprise Edition | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows Server 2003 Standard Edition | 3.2 | 9.1 TS1M3 SP4 | ||||
| Microsoft Windows XP Professional | 3.2 | 9.1 TS1M3 SP4 | ||||
| Windows Vista | 3.2 | 9.1 TS1M3 SP4 | ||||




