org.springframework.web.servlet.view.document
abstract public class: AbstractJExcelView [javadoc |
source]
java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.view.AbstractView
org.springframework.web.servlet.view.document.AbstractJExcelView
All Implemented Interfaces:
View, BeanNameAware, ServletContextAware, ApplicationContextAware
Convenient superclass for Excel document views.
This class uses the JExcelAPI instead of POI. More
information on JExcelAPI can be found on their website.
Properties:
- url (optional): The url of an existing Excel document to pick as a
starting point. It is done without localization part nor the .xls extension.
The file will be searched with locations in the following order:
- [url]_[language]_[country].xls
- [url]_[language].xls
- [url].xls
For working with the workbook in the subclass, see Java Excel API site
As an example, you can try this snippet:
protected void buildExcelDocument(Map model, WritableWorkbook workbook, HttpServletRequest request,
HttpServletResponse response) {
if (workbook.getNumberOfSheets() == 0) {
workbook.createSheet("Spring", 0);
}
WritableSheet sheet = workbook.getSheet("Spring");
Label label = new Label(0, 0, "This is a nice label");
sheet.addCell(label);
}
The use of this view is close to the AbstractExcelView class,
just using the JExcel API instead of the Apache POI API.
| Methods from org.springframework.web.servlet.view.AbstractView: |
|---|
|
addStaticAttribute, createRequestContext, exposeModelAsRequestAttributes, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getStaticAttributes, prepareResponse, render, renderMergedOutputModel, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setRequestContextAttribute, toString |
| Method from org.springframework.web.servlet.view.document.AbstractJExcelView Detail: |
abstract protected void buildExcelDocument(Map model,
WritableWorkbook workbook,
HttpServletRequest request,
HttpServletResponse response) throws Exception
Subclasses must implement this method to create an Excel Workbook
document, given the model. |
protected boolean generatesDownloadContent() {
return true;
}
|
protected Workbook getTemplateSource(String url,
HttpServletRequest request) throws Exception {
LocalizedResourceHelper helper = new LocalizedResourceHelper(getApplicationContext());
Locale userLocale = RequestContextUtils.getLocale(request);
Resource inputFile = helper.findLocalizedResource(url, EXTENSION, userLocale);
// Create the Excel document from the source.
if (logger.isDebugEnabled()) {
logger.debug("Loading Excel workbook from " + inputFile);
}
return Workbook.getWorkbook(inputFile.getInputStream());
}
Create the workbook from an existing XLS document. |
protected final void renderMergedOutputModel(Map model,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
// Set the content type and get the output stream.
response.setContentType(getContentType());
OutputStream out = response.getOutputStream();
WritableWorkbook workbook;
if (this.url != null) {
Workbook template = getTemplateSource(this.url, request);
workbook = Workbook.createWorkbook(out, template);
}
else {
logger.debug("Creating Excel Workbook from scratch");
workbook = Workbook.createWorkbook(out);
}
buildExcelDocument(model, workbook, request, response);
// Should we set the content length here?
// response.setContentLength(workbook.getBytes().length);
workbook.write();
out.flush();
workbook.close();
}
Renders the Excel view, given the specified model. |
public void setUrl(String url) {
this.url = url;
}
Set the URL of the Excel workbook source, without localization part nor extension. |