通過POI.jar導出數據生成xls、xlsx、csv等格式文件


一:創建一個Button

 

二:引入POI的jar包

三:在CO的processFormRequest中添加如下代碼(導入的包中,有些是做其他功能用的,只做數據導出的話,不用將下列的包全部導入)

import com.sun.java.util.collections.HashMap;
import java.io.OutputStream;
import java.io.Serializable;
import javax.servlet.http.HttpServletResponse;
import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.framework.OAApplicationModule;
import oracle.apps.fnd.framework.OAException;
import oracle.apps.fnd.framework.webui.OAControllerImpl;
import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.OAWebBeanConstants;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;
import oracle.cabo.ui.data.DataObject;
 
public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)
{
    super.processFormRequest(pageContext, webBean);
    OAApplicationModule am = pageContext.getApplicationModule(webBean);       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);
        }
    }
}

四:在CO中添加下列方法

    public void download(OAPageContext pageContext, HttpServletResponse response, byte[] abyte0) {
        String fileName = "EmpInfoData"; //最終導出數據生成的文件名字
        try {
            //String charset = pageContext.getProfile("ICX_CLIENT_IANA_ENCODING");
            //設置文件的格式 字符集
            response.setContentType("application/vnd.ms-excel;charset=gb2312;"); //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();
        }
    }

五:在AMImpl中加入下列方法(導入的包中,有些是做其他功能用的,只做數據導出的話,不用將下列的包全部導入)

 
         

import java.io.ByteArrayOutputStream;
import zz.oracle.apps.cux.adtable.server.EmployeeVOImpl;
import zz.oracle.apps.cux.adtable.server.EmployeeVORowImpl;
import oracle.apps.fnd.common.MessageToken;
import oracle.apps.fnd.framework.OAException;
import oracle.apps.fnd.framework.OAViewObject;
import oracle.apps.fnd.framework.server.OAApplicationModuleImpl;
import oracle.apps.fnd.framework.server.OADBTransaction;
import oracle.apps.fnd.framework.server.OAViewObjectImpl;
import oracle.jbo.Row;
import oracle.jbo.RowSetIterator;
import oracle.jbo.domain.Number;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import zz.oracle.apps.cux.poplist.server.PositionsVOImpl;

  public byte[] export() {
        byte abyte0[] = null;
        EmployeeVOImpl vo = getEmployeeVO1();
        EmployeeVORowImpl 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("test_sheet1");
            //第三步,在sheet中添加表頭第0行,注意老版本poi對應Excel的行數列數有限制short
            HSSFRow row = sheet.createRow((int)0);
            //第四步,創建單元格,並設置值表頭 設置表頭居中
            CellStyle style = wb.createCellStyle();
            style.setAlignment(CellStyle.ALIGN_CENTER); //創建一個劇中格式

            //CSV:CSV的定義是逗號分隔符,有時也稱為字符分隔符,因為分隔字符也可以不是逗號,CSV是一個可變長度的由字符分開的字符串,故使用StringBuffer
            StringBuffer buffer = new StringBuffer("姓,名,Full_Name,郵箱,主管姓名,職務,薪資,Start_Date,End_Date\r\n");

            //Excel
            HSSFCell cell = row.createCell(0);
            cell.setCellValue("姓");
            cell.setCellStyle(style);
            
            cell = row.createCell(1);
            cell.setCellValue("名");
            cell.setCellStyle(style);
            
            cell = row.createCell(2);
            cell.setCellValue("Full_Name");
            cell.setCellStyle(style);
            
            cell = row.createCell(3);
            cell.setCellValue("郵箱");
            cell.setCellStyle(style);
            
            cell = row.createCell(4);
            cell.setCellValue("主管姓名");
            cell.setCellStyle(style);
            
            cell = row.createCell(5);
            cell.setCellValue("職務");
            cell.setCellStyle(style);
            
            cell = row.createCell(6);
            cell.setCellValue("薪資");
            cell.setCellStyle(style);
            
            cell = row.createCell(7);
            cell.setCellValue("Start_Date");
            cell.setCellStyle(style);
            
            cell = row.createCell(8);
            cell.setCellValue("End_Date");
            cell.setCellStyle(style);

            for (int i = 0; i < rowcount; i++) {
                row = sheet.createRow((int)i + 1);
                hRow = (EmployeeVORowImpl)deleteIter.getRowAtRangeIndex(i); //取得當前記錄

                //第五步,創建單元格,並設置值
                row.createCell(0).setCellValue(hRow.getFirstName());
                row.createCell(1).setCellValue(hRow.getLastName());
                row.createCell(2).setCellValue(hRow.getFullName());
                row.createCell(3).setCellValue(hRow.getEmailAddress());
                row.createCell(4).setCellValue(hRow.getManagerName());
                row.createCell(5).setCellValue(hRow.getPositionCode());
                row.createCell(6).setCellValue(hRow.getSalary()==null?"":hRow.getSalary().doubleValue()+"");
                row.createCell(7).setCellValue(hRow.getStartDate()==null?"":hRow.getStartDate().dateValue()+"");
                row.createCell(8).setCellValue(hRow.getEndDate()==null?"":hRow.getEndDate().dateValue()+"");

                buffer.append(hRow.getFirstName() + "," + hRow.getLastName() + "," + 
                              hRow.getFullName() + "," + hRow.getEmailAddress() + "," + 
                              hRow.getManagerName() + "," + hRow.getPositionCode() + "," + 
                              hRow.getSalary() + "," + hRow.getStartDate() + "," + 
                              hRow.getEndDate() + "\r\n"); //使用字符\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;
        } 

六:上面演示的是導出CSV和xls格式文件,xlsx文件只需要將HSSF改為XSSF,並導入相關包即可。

第二種導出數據是用OAF的exportbutton類型,具體可以看我另一篇文章:https://www.cnblogs.com/AI-xiaocai/p/11731171.html

第三種導出數據是用JXL.jar生成xls格式文件,具體可以看我另一篇文章:https://www.cnblogs.com/AI-xiaocai/p/11732531.html

參考資料:

1、https://blog.csdn.net/szwangdf/article/details/39053859:[JAVA]POI各Jar包的作用

2、https://www.cnblogs.com/AI-xiaocai/p/11732531.html:通過JXL.jar導出數據生成xls格式文件

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM