一:创建一个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 "); //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() + " "); //使用字符 进行换行,不然第二行以后的数据会全显示在第一行最后一个单元格之中 } //第六步,将文件转换成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格式文件