通过JXL.jar导出数据生成xls格式文件

一:创建一个Button

 

二:引入JXL的jar包

三:在CO的processFormRequest中添加如下代码(导入的包中,有些是做其他功能用的,只做数据导出的话,不用将下列的包全部导入)

import com.sun.java.util.collections.HashMap;
import java.io.OutputStream;
import java.io.Serializable;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.Alignment;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import oracle.apps.fnd.common.MessageToken;
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.OADecimalValidater;
import oracle.apps.fnd.framework.webui.OADialogPage;
import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.OAWebBeanConstants;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;
import oracle.apps.fnd.framework.webui.beans.message.OAMessageLovInputBean;
import oracle.apps.fnd.framework.webui.beans.message.OAMessageTextInputBean;
import oracle.cabo.style.CSSStyle;
import oracle.cabo.ui.data.DataObject;
import oracle.cabo.ui.validate.Formatter;
import zz.oracle.apps.cux.adtable.server.EmployeeAMImpl;
import zz.oracle.apps.cux.adtable.server.EmployeeVOImpl;
import zz.oracle.apps.cux.adtable.server.EmployeeVORowImpl;

public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)
{
  super.processFormRequest(pageContext, webBean);
  OAApplicationModule am = pageContext.getApplicationModule(webBean);
  EmployeeAMImpl am1 = (EmployeeAMImpl)pageContext.getApplicationModule(webBean);
  EmployeeVOImpl empvo = am1.getEmployeeVO1();
  if("exportdatajxl".equals(pageContext.getParameter(EVENT_PARAM))){
    empexportexcel(pageContext,empvo);
  }
}

四:在CO中添加下列方法

    public void empexportexcel(OAPageContext pageContext,EmployeeVOImpl empvo) {
        HttpServletResponse response = (HttpServletResponse) pageContext.getRenderingContext().getServletResponse();
        response.setCharacterEncoding("gb2312");
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        String filenames = "EmpDataInfo_jxl.xls";
    
        String[] expLabels = new String[] {"姓","名","Full_Name","邮箱","主管姓名","职务","薪资","Start_Date","End_Date"};
        String[] expFields = new String[] {"FirstName","LastName","FullName","EmailAddress","ManagerName","PositionCode","Salary","StartDate","EndDate"};
        Class[] colClasses = new Class[] {null,null,null,null,null,null,null,null,null};
    
        int inId = empvo.getRangeStart();
        EmployeeVORowImpl currentRow = (EmployeeVORowImpl) empvo.getCurrentRow();
        if (empvo == null) {
            return;
        }
        try{
            OutputStream fileos = response.getOutputStream();
            response.addHeader("Content-Disposition",(new StringBuilder()).append("attachment;filename=\"").append(filenames).append("\"").toString());
            WorkbookSettings workbookSettings = new WorkbookSettings();
            workbookSettings.setEncoding("ISO-8859-1");  //乱码处理
            WritableWorkbook workbook = Workbook.createWorkbook(fileos, workbookSettings);
            WritableSheet sheet = workbook.createSheet("test_sheet1",0);
    
            // 定义格式 字体 下划线 斜体 粗体 颜色
            WritableFont wf_h = new WritableFont(WritableFont.ARIAL, 20, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLUE_GREY);
            WritableCellFormat wcf_h = new WritableCellFormat(wf_h); // 单元格定义
            wcf_h.setAlignment(Alignment.LEFT); // 设置对齐方式
            wcf_h.setBorder(jxl.format.Border.NONE,BorderLineStyle.NONE);
    
            // 定义格式 字体 下划线 斜体 粗体 颜色
            WritableFont wf_t = new WritableFont(WritableFont.ARIAL, 11, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.WHITE);
            WritableCellFormat wcf_t = new WritableCellFormat(wf_t); // 单元格定义
            wcf_t.setBackground(Colour.BLUE_GREY); // 设置单元格的背景颜色
            wcf_t.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
            wcf_t.setVerticalAlignment(VerticalAlignment.TOP); // 设置对齐方式
            wcf_t.setBorder(jxl.format.Border.ALL,BorderLineStyle.MEDIUM,Colour.GREY_25_PERCENT);
    
            // 定义格式 字体 下划线 斜体 粗体 颜色
            WritableFont wf_cs = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            WritableCellFormat wcf_cs = new WritableCellFormat(wf_cs); // 单元格定义
            wcf_cs.setAlignment(Alignment.LEFT); // 设置对齐方式
            wcf_cs.setVerticalAlignment(VerticalAlignment.TOP);
            wcf_cs.setWrap(true);
            wcf_cs.setBorder(jxl.format.Border.ALL,BorderLineStyle.MEDIUM,Colour.GREY_25_PERCENT);
    
            // 定义格式 字体 下划线 斜体 粗体 颜色
            WritableFont wf_cn = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            WritableCellFormat wcf_cn = new WritableCellFormat(wf_cn); // 单元格定义
            wcf_cn.setAlignment(Alignment.RIGHT); // 设置对齐方式
            wcf_cn.setVerticalAlignment(VerticalAlignment.TOP);
            wcf_cn.setBorder(jxl.format.Border.ALL,BorderLineStyle.MEDIUM,Colour.GREY_25_PERCENT);
    
            Label expLabel = null;
    
            // 头
            expLabel = new Label(0, 0, "Employee Info Data", wcf_h);
             sheet.addCell(expLabel);
            sheet.mergeCells(0,0,2,0);
            sheet.setRowView(0, 500);
    
            // 标题
            for (int i = 0, j=0; i < expLabels.length; i++, j++)
            {
                expLabel = new Label(j, 1, expLabels[i], wcf_t);
                sheet.addCell(expLabel);
            }
            sheet.setRowView(1, 400);
    
            // 内容
            EmployeeVORowImpl rowfield;
            rowfield = (EmployeeVORowImpl) empvo.first();
            if (rowfield != null)
            {
                for (int i = 0; i < expFields.length; i++)  //获取每个字段的数据类型String/Date/Number
                {
                    colClasses[i] = rowfield.getStructureDef().lookupAttributeDef(expFields[i]).getJavaType();
                }
            }
            empvo.previous();
    
            while (empvo.hasNext())
            {
                rowfield = (EmployeeVORowImpl) empvo.next();
                int rowcount = empvo.getCurrentRowIndex() + 2;
    
                for (int i = 0, j=0; i < expFields.length; i++, j++)
                {
                    if (rowfield.getAttribute(expFields[i]) != null)
                    {
                        if (colClasses[i].equals(Class.forName("oracle.jbo.domain.Date")))
                        {
                            expLabel = new Label(j, rowcount, rowfield.getAttribute(expFields[i]).toString(), wcf_cs);  //Date类型格式
                        }
                        else if (colClasses[i].equals(Class.forName("oracle.jbo.domain.Number")))
                        {
                            expLabel = new Label(j, rowcount, rowfield.getAttribute(expFields[i]).toString(), wcf_cn);  //Number类型格式
                        }
                        else if (colClasses[i].equals(Class.forName("java.lang.String")))
                        {
                            expLabel = new Label(j, rowcount, rowfield.getAttribute(expFields[i]).toString(), wcf_cs);  //String类型格式
                        }
                    }
                    else
                    {
                        expLabel = new Label(j, rowcount, "", wcf_cs);
                    }
                    sheet.addCell(expLabel);
                }
            }
            sheet.setColumnView(0, 10);   //设定栏位FirstName长度
            sheet.setColumnView(1, 10);   //设定栏位LastName长度
            sheet.setColumnView(2, 20);   //设定栏位FullName长度
            sheet.setColumnView(3, 25);   //设定栏位EmailAddress长度
            sheet.setColumnView(4, 20);   //设定栏位ManagerName长度
            sheet.setColumnView(5, 15);   //设定栏位PositionCode长度
            sheet.setColumnView(6, 10);   //设定栏位Salary长度
            sheet.setColumnView(7, 15);   //设定栏位StartDate长度
            sheet.setColumnView(8, 15);   //设定栏位EndDate长度
    
            //关闭服务
            sheet.getSettings().setShowGridLines(false);
            workbook.write();
            workbook.close();
            fileos.close();
            response.flushBuffer();
        }catch(Exception ex){
            empvo.setRangeStart(inId);
            empvo.setCurrentRow(currentRow);
            throw new OAException(ex.getMessage());
        }
    }

五:运行导出生成xls文件

第二种导出数据是用OAF的exportbutton类型,具体可以看我另一篇文章:https://www.cnblogs.com/AI-xiaocai/p/11731171.html

第三种导出数据是用JXL.jar生成xls格式文件,具体可以看我另一篇文章:https://www.cnblogs.com/AI-xiaocai/p/11732531.html

猜你喜欢

转载自www.cnblogs.com/AI-xiaocai/p/11732531.html