jfy3d(剑事)BLOG

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  37 随笔 :: 0 文章 :: 363 评论 :: 0 Trackbacks

WebWork中除了默认支持的几中视图外还可以自己来定义需要的视图,如JFreeChart,Excel等

这里生成Excel用的是POI的API
WebWork中定义ResultType视图类型只需要继承Result接口
代码如下

package com.customer.resulttype;

import com.opensymphony.xwork.Result;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.webwork.ServletActionContext;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;

public class ExcelResult implements Result{
    private HSSFWorkbook workbook;
    private String filename;
    private String contenttype;
    public void execute(ActionInvocation invocation) throws Exception {
        if(contenttype==null)
            contenttype = "application/ms-excel";
        if (workbook==null)
            workbook = (HSSFWorkbook) invocation.getStack().findValue("workbook");
      

        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType(contenttype);
        response.setHeader("Content-Disposition","attachment;Filename="+filename+".xls");
        OutputStream os = response.getOutputStream();
        workbook.write(os);
        os.flush();
        os.close();
    }

    public void setWorkbook(HSSFWorkbook workbook) {
        this.workbook = workbook;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public void setContenttype(String contenttype) {
        this.contenttype = contenttype;
    }
}

视图做完之后做如下配置运行测试

package com.customer.action;

import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.webwork.ServletActionContext;
import com.dboperate.ResultGather;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.Map;

public class ExportExcelAction extends ActionSupport {
    private HSSFWorkbook workbook;

    public String execute() throws Exception {
        return SUCCESS;
    }

    public String product() throws Exception {
        try {
            workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet();

            workbook.setSheetName(0, "厂商产品", (short) 1);
            HSSFRow row = sheet.createRow((short) 0);

            HSSFCell cell0 = row.createCell((short) 0);
            HSSFCell cell1 = row.createCell((short) 1);
            HSSFCell cell2 = row.createCell((short) 2);
            HSSFCell cell3 = row.createCell((short) 3);
            HSSFCell cell4 = row.createCell((short) 4);
            HSSFCell cell5 = row.createCell((short) 5);
            HSSFCell cell6 = row.createCell((short) 6);
            HSSFCell cell7 = row.createCell((short) 7);
            HSSFCell cell8 = row.createCell((short) 8);
            HSSFCell cell9 = row.createCell((short) 9);

            cell0.setEncoding(HSSFCell.ENCODING_UTF_16);//这里是设置编码保证中文正常显示
            cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell4.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell5.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell6.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell7.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell8.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell9.setEncoding(HSSFCell.ENCODING_UTF_16);

            cell0.setCellValue("厂商名");
            cell1.setCellValue("产品名");
            cell2.setCellValue("重量");
            cell3.setCellValue("星级");
            cell4.setCellValue("parama");
            cell5.setCellValue("paramb");
            cell6.setCellValue("paramc");
            cell7.setCellValue("paramd");
            cell8.setCellValue("状态");
            cell9.setCellValue("备注");

        } catch (Exception e) {
        }
        return SUCCESS;
    }

    public HSSFWorkbook getWorkbook() {
        return workbook;
    }


}

Xwork.xml中配置加入

        <result-type default="true" name="freemarker"
            <result-type name="excel" class="com.customer.resulttype.ExcelResult"/>
        </result-types>

 <action name="exportExcel" class="com.customer.action.ExportExcelAction">

            <result name="success" type="excel">
                <param name="filename">productparam>
               
            </result>

        </action>

posted on 2006-04-11 12:16 剑事 阅读(5681) 评论(5)  编辑  收藏 所属分类: webwork

评论

# re: WebWork中自定义result视图使用POI生成Excel 2006-04-18 14:55 lynx
System.out.println(invocation.getStack().findValue("workbook"));

这个打印出来是null

前台页面报错:
java.lang.IllegalStateException: getOutputStream() has already been called for this response
org.apache.coyote.tomcat5.CoyoteResponse.getWriter(CoyoteResponse.java:599)
org.apache.coyote.tomcat5.CoyoteResponseFacade.getWriter(CoyoteResponseFacade.java:163)
com.opensymphony.webwork.views.freemarker.FreemarkerServlet.process(FreemarkerServlet.java:229)


后台tomcat报错:
04-18 14:51:38.828 ERROR [ExceptionInterceptor.java:38] Caught exception while invoking action: compare.ExportToExcelToolAction@1f1c748
java.lang.NullPointerException
at compare.ExcelResult.execute(ExcelResult.java:38)
at com.opensymphony.xwork.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:258)
at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:182)
at gxlu.webapps.umv.core.webwork.interceptor.AuditInterceptor.intercept(AuditInterceptor.java:46)  回复  更多评论
  

# 怎么动态设置下载后的文件名??? 2006-04-18 15:14 hello
文件名在action 中动态生成传到excelresult里面怎么写??
我不想在<result name="success" type="excel">
<param name="filename">productparam>
</result>
中写死.  回复  更多评论
  

# re: WebWork中自定义result视图使用POI生成Excel 2006-04-18 21:22 剑事
tomcat 5 webwork 2.17版本运行正常

动态指定文件名可以再ACTION中
private String filename;
public String getFilename() {
return filename;
}
然后result中

if(filename==null)
filename = (String)invocation.getStack().findValue("filename");  回复  更多评论
  

# re: WebWork中自定义result视图使用POI生成Excel 2006-04-19 10:16 asdfd
配置好了.谢谢!  回复  更多评论
  

# re: WebWork中自定义result视图使用POI生成Excel 2009-03-09 11:18 Robert Su
<result-type default="true" name="freemarker"
<result-type name="excel" class="com.customer.resulttype.ExcelResult"/>
</result-types>

控制台输出
严重: Could not find action or result
No result defined for action……  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: