<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    jfy3d(劍事)BLOG

      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      37 隨筆 :: 0 文章 :: 363 評(píng)論 :: 0 Trackbacks

    WebWork中除了默認(rèn)支持的幾中視圖外還可以自己來(lái)定義需要的視圖,如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;
    ??? }
    }

    視圖做完之后做如下配置運(yùn)行測(cè)試

    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, "廠商產(chǎn)品", (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);//這里是設(shè)置編碼保證中文正常顯示
    ??????????? 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("產(chǎn)品名");
    ??????????? cell2.setCellValue("重量");
    ??????????? cell3.setCellValue("星級(jí)");
    ??????????? cell4.setCellValue("parama");
    ??????????? cell5.setCellValue("paramb");
    ??????????? cell6.setCellValue("paramc");
    ??????????? cell7.setCellValue("paramd");
    ??????????? cell8.setCellValue("狀態(tài)");
    ??????????? 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 劍事 閱讀(5691) 評(píng)論(5)  編輯  收藏 所屬分類: webwork

    評(píng)論

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

    這個(gè)打印出來(lái)是null

    前臺(tái)頁(yè)面報(bào)錯(cuò):
    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)


    后臺(tái)tomcat報(bào)錯(cuò):
    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)  回復(fù)  更多評(píng)論
      

    # 怎么動(dòng)態(tài)設(shè)置下載后的文件名??? 2006-04-18 15:14 hello
    文件名在action 中動(dòng)態(tài)生成傳到excelresult里面怎么寫(xiě)??
    我不想在<result name="success" type="excel">
    <param name="filename">productparam>
    </result>
    中寫(xiě)死.  回復(fù)  更多評(píng)論
      

    # re: WebWork中自定義result視圖使用POI生成Excel 2006-04-18 21:22 劍事
    tomcat 5 webwork 2.17版本運(yùn)行正常

    動(dòng)態(tài)指定文件名可以再ACTION中
    private String filename;
    public String getFilename() {
    return filename;
    }
    然后result中

    if(filename==null)
    filename = (String)invocation.getStack().findValue("filename");  回復(fù)  更多評(píng)論
      

    # re: WebWork中自定義result視圖使用POI生成Excel 2006-04-19 10:16 asdfd
    配置好了.謝謝!  回復(fù)  更多評(píng)論
      

    # 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>

    控制臺(tái)輸出
    嚴(yán)重: Could not find action or result
    No result defined for action……  回復(fù)  更多評(píng)論
      

    主站蜘蛛池模板: 国产性生大片免费观看性| 永久免费不卡在线观看黄网站| 国产又黄又爽又刺激的免费网址| 菠萝菠萝蜜在线免费视频| 亚洲精品成人无码中文毛片不卡| 色播精品免费小视频| 美女又黄又免费的视频| 久久亚洲精品成人综合| 免费观看男人免费桶女人视频| 一级做a爰片性色毛片免费网站| 亚洲欧洲国产精品你懂的| 色视频色露露永久免费观看| 9久热这里只有精品免费| 亚洲免费网站在线观看| 亚洲成a人片在线观看日本麻豆| 无码专区AAAAAA免费视频| 亚洲女子高潮不断爆白浆| 亚洲欧洲无码AV电影在线观看| 91免费国产自产地址入| 日本黄页网址在线看免费不卡| 亚洲美女视频免费| www国产亚洲精品久久久| 2020因为爱你带字幕免费观看全集| 久久久久亚洲国产AV麻豆| 久久国产精品亚洲一区二区| 日本最新免费不卡二区在线| 免费国产成人18在线观看| 亚洲AV成人无码网天堂| 亚洲天天在线日亚洲洲精| 亚洲国模精品一区| 四虎国产精品免费久久| 国产免费一区二区视频| 国产亚洲日韩在线a不卡| 亚洲欧洲日产国码二区首页| 中文字幕亚洲一区二区va在线| 在线观看视频免费国语| 无码专区AAAAAA免费视频| 国产99视频精品免费视频76| 亚洲精品乱码久久久久久V| 亚洲综合视频在线观看| 日本亚洲成高清一区二区三区|