I control the layout and content type through the screen class I built 
called, ExcelScreen

You should have your excel .vm extend the ExcelScreen class and run 
        super.doBuildTemplate( data, context );

You can create a unique file name by setting the outfile parameter in the 
RunData object, or the ExcelScreen class will by default export the file 
as export.xls

You should update the logging class for turbine 2.3, this has been working 
for me in turbine 2.2 apps.

Jeff Painter


---------------------------------------------------------------------------------
package com.kiasoft.modules.screens.csv;

/*
 *  ------------------------------------------------------------------
 *  Kiasoft, Inc.
 *  http://kiasoft.com
 *  ------------------------------------------------------------------
 */

import org.apache.turbine.modules.screens.VelocityScreen;

import org.apache.turbine.util.Log;
import org.apache.turbine.util.RunData;
import org.apache.velocity.context.Context;
import javax.servlet.http.HttpServletResponse;

/**
 *  set the content type header for a csv export screen
 *
 [EMAIL PROTECTED]     painter
 */
public class ExcelScreen extends VelocityScreen {

    // not exactly a CSV, but works for MS Windows clients with excel installed
    // excel will open tables directly as spreadsheet fields... can save as csv easily 
from there
    private static String CONTENT_TYPE = "application/vnd.ms-excel";
    private static String DEFAULT_LAYOUT = "VelocityOnlyLayout";
    private static String DEFAULT_LAYOUT_VM = "/ExcelDataLayout.vm";

    /**
     *  Description of the Method
     *
     [EMAIL PROTECTED]  data     Description of the Parameter
     [EMAIL PROTECTED]  context  Description of the Parameter
     */
    public void doBuildTemplate(RunData data, Context context) {
        try {
            String filename = data.getParameters().getString("outfile");
            if ( filename != null ) {
                filename.trim();
            } else {
                filename = "export.xls";
            }

            data.setLayoutTemplate(DEFAULT_LAYOUT_VM);
            data.setContentType(CONTENT_TYPE);

            // Set response header type
            HttpServletResponse resp = data.getResponse();
            resp.setHeader("Content-Type", CONTENT_TYPE);
            resp.setHeader("Content-Disposition", "inline; filename=" + filename );
        } 
        catch (Exception e) {
            Log.error("ExcelScreen error: " + e.toString());
        }
    }


    /**
     *  We are overriding the Screen.getLayout() method to force the use of our
     *  excel format layout which does not include the head tags built from
     *  VelocityECSLayout
     *
     [EMAIL PROTECTED]  data  Turbine information.
     [EMAIL PROTECTED]       A String with the Layout.
     */
    public String getLayout(RunData data) {
        data.setLayout(DEFAULT_LAYOUT);
        return DEFAULT_LAYOUT;
    }

}
---------------------------------------------------------------------------------




On Sun, 13 Jun 2004, turbine-user-gmx wrote:

> Hi,
> 
> I am trying to generate a excel-report via a template and open it on the
> browser by MS Excel. (I use Turbine 2.3! )But I get the content displayed as
> plain text and therefore Excel do not start.
> 
> my test-code:
> 
> $data.setLayoutTemplate("Excel.vm")
> $data.setContentType( "application/vnd.ms-excel" )
> <table>
>   <tr>
>     <td>text1</td>
>     <td>1</td>
>   </tr>
>   <tr>
>     <td>text2</td>
>     <td>2</td>
>   </tr>
> </table>
> 
> 
> 1. the Layout "Excel.vm" contains "$screen_placeholder" only
> 2. tomcat web.xml-File contains
>     <mime-mapping>
>         <extension>xls</extension>
>         <mime-type>application/vnd.ms-excel</mime-type>
>     </mime-mapping>
>      ...
> 3. TurbineResources.properties contains
>     
> services.MimeTypeService.classname=org.apache.turbine.services.mimetype.TurbineMimeTypeService
>     services.MimeTypeService.mime.types=/WEB-INF/conf/mime.types
>     ...
> 4. mime.types contains
>     application/vnd.ms-excel xls
>     
> Is the template code correct?
> Are the configuration entries correct and complete?
> 
> 
> 
> If I set the ContentType via
> $data.setContentType("application/vnd.ms-excel"),
> $data.getContentType() returns no MIME type string (both in the same template).
> If I use the default MIME types from turbine, then it works.
> 
> 
> Thank you for your time and help.
> 
> Alex
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to