1.SAP端准备好函数:ZEXCELTEST
1.1 若启用RESTFUL的方式,则不用勾选“远程启用模块”,若JAVA通过JCO包调用则需要启用。(restful方式暂时未更新)
函数为测试信息,仅有返回文件的二进制信息,无其他返回参数
SAP端ABAP代码如下:
1 FUNCTION zexceltest. 2 *"---------------------------------------------------------------------- 3 *"*"本地接口: 4 *" EXPORTING 5 *" VALUE(OV_STR) TYPE XSTRING 6 *"---------------------------------------------------------------------- 7 SELECT * INTO TABLE @DATA(lt_bp) FROM but000 UP TO 100 ROWS. 8 DATA:lw_tab_ref TYPE REF TO data. 9 CREATE DATA lw_tab_ref LIKE LINE OF lt_bp. 10 11 DATA:ls_mime TYPE w3mime, 12 lt_mime TYPE TABLE OF w3mime, 13 filesize TYPE string, 14 filesizei TYPE i, 15 lv_xstring TYPE xstring, 16 fileext TYPE char20, 17 ls_key TYPE wwwdatatab. 18 19 ls_key-relid = 'MI'. 20 ls_key-objid = 'XXXXXX'. 21 SELECT * FROM wwwparams INTO TABLE @DATA(lt_wwwparam) 22 WHERE relid = @ls_key-relid 23 AND objid = @ls_key-objid. 24 TRY. 25 LOOP AT lt_wwwparam INTO DATA(ls_wwwparam). 26 CASE ls_wwwparam-name. 27 WHEN 'filesize'. 28 filesizei = filesize = ls_wwwparam-value. 29 CONDENSE filesize NO-GAPS. 30 WHEN 'fileextension'. 31 fileext = ls_wwwparam-value. 32 ENDCASE. 33 ENDLOOP. 34 ********************************************************************** 35 * 获取SMW0的数据 36 CALL FUNCTION 'WWWDATA_IMPORT' 37 EXPORTING 38 key = ls_key 39 TABLES 40 mime = lt_mime 41 EXCEPTIONS 42 wrong_object_type = 1 43 import_error = 2 44 OTHERS = 99. 45 46 CALL FUNCTION 'SCMS_BINARY_TO_XSTRING' 47 EXPORTING 48 input_length = filesizei 49 IMPORTING 50 buffer = lv_xstring 51 TABLES 52 binary_tab = lt_mime 53 EXCEPTIONS 54 failed = 1 55 OTHERS = 2. 56 ********************************************************************** 57 DATA(xlsx_handling) = cl_ehfnd_xlsx=>get_instance( )."xlsx 句柄 58 DATA(xlsx_document) = xlsx_handling->load_doc( lv_xstring )."xlsx 文件 59 DATA(xlsx_sheets) = xlsx_document->get_sheets( )."得到sheets 60 DATA(first_xlsx_sheet) = xlsx_document->get_sheet_by_id( xlsx_sheets[ 1 ]-sheet_id )."得到sheet 61 first_xlsx_sheet->change_sheet_name( 'Sheet_first' )."设置sheet的名称 62 DO 2 TIMES. 63 DATA(lv_row) = sy-index + 3."从第三行起,开始写数据 64 DO 10 TIMES. 65 DATA(lv_column) = sy-index. 66 IF first_xlsx_sheet->has_cell_content( iv_row = lv_row iv_column = lv_column ). 67 ELSE. 68 first_xlsx_sheet->set_cell_content( iv_row = lv_row iv_column = lv_column iv_value = '测试数据!' ). 69 ENDIF. 70 71 ENDDO. 72 ENDDO. 73 74 ov_str = xlsx_document->save( ). 75 CATCH cx_openxml_format INTO DATA(openxml_format_exception). 76 MESSAGE e001(00) RAISING file_export_error 77 WITH 'Error occurs when constructing excel file instance.'." cx_openxml_format. 78 CATCH cx_openxml_not_found INTO DATA(openxml_not_found_exception). 79 MESSAGE e001(00) RAISING file_export_error 80 WITH ' Error occurs when constructing excel file instance.' "CX_OPENXML_NOT_FOUND |. 81 . 82 CATCH cx_openxml_not_allowed INTO DATA(openxml_not_allowed_exception). 83 MESSAGE e001(00) RAISING file_export_error 84 WITH 'Error occurs when constructing excel file instance.'" CX_OPENXML_NOT_ALLOWED |. 85 . 86 ENDTRY. 87 88 ENDFUNCTION.
JAVA端通过JCO包代码如下:
1 import java.io.BufferedInputStream; 2 import java.io.BufferedOutputStream; 3 import java.io.ByteArrayInputStream; 4 import java.io.File; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 8 import com.sap.conn.jco.JCoDestination; 9 import com.sap.conn.jco.JCoFunction; 10 import com.sap.conn.jco.JCoParameterList; 11 12 public class FM_ZEXCELTEST { 13 public static void main(String[] args) { 14 BufferedInputStream bis = null; 15 FileOutputStream fos = null; 16 BufferedOutputStream output = null; 17 String filePath = "C:\\Users\\XXXX\\Desktop\\Test\\测试文件.xlsx"; 18 try { 19 JCoFunction function = null; 20 JCoDestination destination = SAPConn.connect("S4D360"); 21 function = destination.getRepository().getFunction("ZEXCELTEST"); 22 function.execute(destination); 23 JCoParameterList list = function.getExportParameterList(); 24 byte[] contents = (byte[])list.getValue("OV_STR"); 25 26 ByteArrayInputStream byteInputStream = new ByteArrayInputStream(contents); 27 bis = new BufferedInputStream(byteInputStream); 28 File file = new File(filePath); 29 File path = file.getParentFile(); 30 if (!path.exists()) { 31 boolean isCreated = path.mkdirs(); 32 if (!isCreated) {} 33 } 34 fos = new FileOutputStream(file); 35 output = new BufferedOutputStream(fos); 36 byte[] buffer = new byte[1024]; 37 int length = bis.read(buffer); 38 while (length != -1) { 39 output.write(buffer, 0, length); 40 length = bis.read(buffer); 41 } 42 output.flush(); 43 System.out.println("文件下载完毕:"+filePath); 44 } catch (Exception e) { 45 46 } finally { 47 try { 48 bis.close(); 49 fos.close(); 50 output.close(); 51 } catch (IOException e) { 52 System.out.println("异常" + e.toString()); 53 } 54 } 55 } 56 }