溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

OAF將查詢結(jié)果導(dǎo)出為EXCEL格式文件

發(fā)布時(shí)間:2020-08-10 23:03:59 來源:ITPUB博客 閱讀:148 作者:kawontony 欄目:關(guān)系型數(shù)據(jù)庫

PG:創(chuàng)建一個(gè)類型為button的按鈕,設(shè)置按鈕的Action Type為firePartialAction和Event為exportexcel(按鈕類型不用 submitButton 的原因是 submitButton類型的按鈕,在導(dǎo)出后,再往頁面添加行或者保存時(shí)會(huì)提示報(bào)瀏覽器后退的異常 )。


CO:processFormRequest代碼:

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

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

      try {

             //獲取DataObject

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

            //根據(jù)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");

            //設(shè)置文件的格式 字符集

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

                                    charset); //gb2312

            //設(shè)置文件大小

            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();

            //將字符數(shù)組寫進(jìn)輸出流

            toClient.write(abyte0);

            //強(qiáng)制刷新(頁面彈出下載框)

            toClient.flush();

            //關(guān)閉輸出流

            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(); //取當(dāng)前提取的記錄集的記錄數(shù)


        if (rowcount == 0)

            throw new OAException("沒有需要導(dǎo)出的數(shù)據(jù)", OAException.ERROR);


        RowSetIterator deleteIter = 

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

        deleteIter.setRangeStart(0); //設(shè)置循環(huán)起點(diǎn),相當(dāng)于移動(dòng)指針到第一條記錄

        deleteIter.setRangeSize(rowcount); //設(shè)置循環(huán)次數(shù)


        //第一步,創(chuàng)建一個(gè)webbook,對(duì)應(yīng)一個(gè)Excel文件

        HSSFWorkbook wb = new HSSFWorkbook();

        //第二步,在webbook中添加一個(gè)sheet,對(duì)應(yīng)Excel文件中的sheet

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

        //第三步,在sheet中添加表頭第0行,注意老版本poi對(duì)應(yīng)Excel的行數(shù)列數(shù)有限制short

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

        //第四步,創(chuàng)建單元格,并設(shè)置值表頭 設(shè)置表頭居中

        CellStyle style = wb.createCellStyle();

        style.setAlignment(CellStyle.ALIGN_CENTER); //創(chuàng)建一個(gè)劇中格式


        //CSV

        StringBuffer buffer = 

            new StringBuffer("庫存組織名稱,物料編碼,物料描述,供應(yīng)商編碼,供應(yīng)商名稱,供應(yīng)商地點(diǎn),狀態(tài),是否禁用,結(jié)算方式\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("供應(yīng)商編碼");

        cell.setCellStyle(style);

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

        cell.setCellValue("供應(yīng)商名稱");

        cell.setCellStyle(style);

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

        cell.setCellValue("供應(yīng)商地點(diǎn)");

        cell.setCellStyle(style);

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

        cell.setCellValue("狀態(tài)");

        cell.setCellStyle(style);

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

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

        cell.setCellStyle(style);

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

        cell.setCellValue("結(jié)算方式");

        cell.setCellStyle(style);


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

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

            hRow = (CuxAslVORowImpl)deleteIter.getRowAtRangeIndex(i); //取得當(dāng)前記錄


            //第五步,創(chuàng)建單元格,并設(shè)置值

            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");

        }


        //第六步,將文件轉(zhuǎn)換成byte數(shù)組

        try {

           

            //這里有兩種導(dǎo)出格式,已屏蔽的是CSV,未屏蔽的是EXCEL

            //Excel

            //文件只能轉(zhuǎn)成流,通過byte流轉(zhuǎn)成二進(jìn)制數(shù)組(流無法序列化做成參數(shù)傳出)

            ByteArrayOutputStream os = new ByteArrayOutputStream();

            //文件寫入流

            wb.write(os);

            //流轉(zhuǎn)數(shù)組

            abyte0 = os.toByteArray();

            //關(guān)閉流

            os.close();


            //            //CSV

            //            //字符直接轉(zhuǎn)byte數(shù)組

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

        } catch (Exception e) {

            e.printStackTrace();

        }

        deleteIter.closeRowSetIterator();


        return abyte0;


    } //end export()




向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI