欢迎光临
我们一直在努力

OAF将查询结果导出为EXCEL格式文件

PG:创建一个类型为button的按钮,设置按钮的Action Type为firePartialAction和Event为exportexcel(按钮类型不用

submitButton

的原因是

submitButton类型的按钮,在导出后,再往页面添加行或者保存时会提示报浏览器后退的异常

)。

CO:processFormRequest代码:

if ("exportexcel".equals(pageContext.getParameter(EVENT_PARAM))){

      byte abtye0[] = (byte[])am.invokeMethod("export");

      try {

             //获取DataObject

            DataObject dataObject = pageContext.getNamedDataObject("_SessionParameters");

            //根据DataObject获取response

            HttpServletResponse httpservletresponse  = (HttpServletResponse)dataObject.selectValue(null, "HttpServletResponse");

             download(pageContext, httpservletresponse, abtye0);

      } catch (Exception e) {

               e.printStackTrace();

               throw OAException.wrapperException(e);

         }

}


download方法代码:


    public void download(OAPageContext pageContext, 


                         HttpServletResponse response, byte[] abyte0) {




        String fileName = "export";


        //fileName = pageContext.getMessage("CUX", "CUX_SRM_IMPORT_FILE_NAME", null);


        try {


            String charset = 


                pageContext.getProfile("ICX_CLIENT_IANA_ENCODING");


            //设置文件的格式 字符集


            response.setContentType("application/vnd.ms-excel;charset=" + 


                                    charset); //gb2312


            //设置文件大小


            response.setContentLength(abyte0.length);


            //throw new OAException("abyte0.length:"+abyte0.length,OAException.ERROR);


            //通知浏览器文件的名字


            response.setHeader("Content-Disposition", 


                               "attachment;filename=" + fileName + ".xls");


            //获取输出流


            OutputStream toClient = response.getOutputStream();


            //将字符数组写进输出流


            toClient.write(abyte0);


            //强制刷新(页面弹出下载框)


            toClient.flush();


            //关闭输出流


            toClient.close();


        } catch (Exception ex) {


            ex.printStackTrace();


        }


    } //end download()






AM,export代码:



    public byte[] export() {




        byte abyte0[] = null;


        CuxAslVOImpl vo = getCuxAslVO1();


        CuxAslVORowImpl hRow = null;


        int rowcount = vo.getRowCount(); //取当前提取的记录集的记录数




        if (rowcount == 0)


            throw new OAException("没有需要导出的数据", OAException.ERROR);




        RowSetIterator deleteIter = 


            vo.createRowSetIterator("deleteIter"); //建立记录集的指示符


        deleteIter.setRangeStart(0); //设置循环起点,相当于移动指针到第一条记录


        deleteIter.setRangeSize(rowcount); //设置循环次数




        //第一步,创建一个webbook,对应一个Excel文件


        HSSFWorkbook wb = new HSSFWorkbook();


        //第二步,在webbook中添加一个sheet,对应Excel文件中的sheet


        HSSFSheet sheet = wb.createSheet("export");


        //第三步,在sheet中添加表头第0行,注意老版本poi对应Excel的行数列数有限制short


        HSSFRow row = sheet.createRow((int)0);


        //第四步,创建单元格,并设置值表头 设置表头居中


        CellStyle style = wb.createCellStyle();


        style.setAlignment(CellStyle.ALIGN_CENTER); //创建一个剧中格式




        //CSV


        StringBuffer buffer = 


            new StringBuffer("库存组织名称,物料编码,物料描述,供应商编码,供应商名称,供应商地点,状态,是否禁用,结算方式\r\n");


        //Excel


        HSSFCell cell = row.createCell((short)0);


        cell.setCellValue("库存组织名称");


        cell.setCellStyle(style);


        cell = row.createCell((short)1);


        cell.setCellValue("物料编码");


        cell.setCellStyle(style);


        cell = row.createCell((short)2);


        cell.setCellValue("物料描述");


        cell.setCellStyle(style);


        cell = row.createCell((short)3);


        cell.setCellValue("供应商编码");


        cell.setCellStyle(style);


        cell = row.createCell((short)4);


        cell.setCellValue("供应商名称");


        cell.setCellStyle(style);


        cell = row.createCell((short)5);


        cell.setCellValue("供应商地点");


        cell.setCellStyle(style);


        cell = row.createCell((short)6);


        cell.setCellValue("状态");


        cell.setCellStyle(style);


        cell = row.createCell((short)7);


        cell.setCellValue("是否禁用");


        cell.setCellStyle(style);


        cell = row.createCell((short)8);


        cell.setCellValue("结算方式");


        cell.setCellStyle(style);




        for (int i = 0; i < rowcount; i++) {


            row = sheet.createRow((int)i + 1);


            hRow = (CuxAslVORowImpl)deleteIter.getRowAtRangeIndex(i); //取得当前记录




            //第五步,创建单元格,并设置值


            row.createCell((short)0).setCellValue(hRow.getOrganizationName());


            row.createCell((short)1).setCellValue(hRow.getItemNumber());


            row.createCell((short)2).setCellValue(hRow.getItemDesc());


            row.createCell((short)3).setCellValue(hRow.getVendorNumber());


            row.createCell((short)4).setCellValue(hRow.getVendorName());


            row.createCell((short)5).setCellValue(hRow.getVendorSiteCode());


            row.createCell((short)6).setCellValue(hRow.getAslStatus());


            row.createCell((short)7).setCellValue(hRow.getDisableFlag());


            row.createCell((short)8).setCellValue(hRow.getPoSettlementMethod());




            buffer.append(hRow.getOrganizationName() + "," + 


                          hRow.getItemNumber() + "," + hRow.getItemDesc() + 


                          "," + hRow.getVendorNumber() + "," + 


                          hRow.getVendorName() + "," + 


                          hRow.getVendorSiteCode() + "," + 


                          hRow.getAslStatus() + "," + hRow.getDisableFlag() + 


                          "," + hRow.getPoSettlementMethod() + "\r\n");


        }




        //第六步,将文件转换成byte数组


        try {


           


            //这里有两种导出格式,已屏蔽的是CSV,未屏蔽的是EXCEL


            //Excel


            //文件只能转成流,通过byte流转成二进制数组(流无法序列化做成参数传出)


            ByteArrayOutputStream os = new ByteArrayOutputStream();


            //文件写入流


            wb.write(os);


            //流转数组


            abyte0 = os.toByteArray();


            //关闭流


            os.close();




            //            //CSV


            //            //字符直接转byte数组


            //            abyte0 = buffer.toString().getBytes();


        } catch (Exception e) {


            e.printStackTrace();


        }


        deleteIter.closeRowSetIterator();




        return abyte0;




    } //end export()




赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。