ALV OO Grid Show , DownLoad, CSV ,Excel...

<!--StartFragment -->

*----------------------------------------------------------------------
*ProgramID/Name:ZW_ALV_GRID_STDDateWritten:20100630
*Author'sName:Lilo.ZhuLastUpdate:
*ProgramTitle:
*ProjectName:XXXX
*Version:1.0
*----------------------------------------------------------------------
*Description:(NewProgramForaStandardOOALVGridFormat,Dialog)
*----------------------------------------------------------------------
*ChangeHistory
*----------------------------------------------------------------------
*Date|Programmer|Corr.#|Description
*20100630|Lilo.Zhu||NewCreate
*|||
*|||
*-----------------------------------------------------------------------
REPORTZW_ALV_GRID_STD.

*------TABLES------
TABLES:EKKO.

*------TYPEDEFINITION------
TYPES:BEGINOFTP_TAB.
INCLUDESTRUCTUREEKKO.
TYPES:ROWCOLOR(4)TYPEC.
TYPES:CELLCOLORTYPELVC_T_SCOL.
TYPES:EXPOTYPEC.
TYPES:ENDOFTP_TAB.

*------SELECTION-SCREEN------
SELECTION-SCREEN:BEGINOFBLOCKB00WITHFRAMETITLETEXT-B00.
SELECT-OPTIONS:S_EBELNFOREKKO-EBELN.
SELECT-OPTIONS:S_BUKRSFOREKKO-BUKRS.
SELECTION-SCREEN:ENDOFBLOCKB00.

SELECTION-SCREEN:BEGINOFBLOCKB01WITHFRAMETITLETEXT-B01.
PARAMETERSP_ALVRADIOBUTTONGROUPG1DEFAULT'X'USER-COMMANDC1.
PARAMETERSP_DWNRADIOBUTTONGROUPG1.
*->ServerPathandFileName
PARAMETERS:P_SVPATHLIKERLGRAP-FILENAMEMODIFIDPTHMEMORYIDPAT,
P_SVFILELIKERLGRAP-FILENAMENO-DISPLAY.
SELECTION-SCREEN:ENDOFBLOCKB01.

DATA:G_SVFILELIKERLGRAP-FILENAME,
G_PATHTYPECHAR600.

*------CLASSDEFINITIONDEFERRED------
CLASSLCL_LAYOUTDEFINITIONDEFERRED.
CLASSLCL_FIELDCATDEFINITIONDEFERRED.
CLASSLCL_DATADEFINITIONDEFERRED.

*------>GLOBALDATADEFINITIONSFORALV
*------ALVGRIDINSTANCEREFERENCE------
DATAREF_ALVTYPEREFTOCL_GUI_ALV_GRID.
*------CUSTOMERCONTAINERINSTANCEREFERENCE------
DATAREF_CONTYPEREFTOCL_GUI_CUSTOM_CONTAINER.
*------DATAFORSCREENCOMMAND------
DATA:SAVE_OKLIKESY-UCOMM,
OK_CODELIKESY-UCOMM.
DATA:VARTYPEDISVARIANT.

*------CLASSDATADEFINITION------
DATAREF_LAYOUTTYPEREFTOLCL_LAYOUT.
DATAREF_FIELDCATTYPEREFTOLCL_FIELDCAT.
DATAREF_DATATYPEREFTOLCL_DATA.

*------>CLASSDEFINITION------
*------CLASSLCL_LAYOUTDEFINITION------
CLASSLCL_LAYOUTDEFINITION.
PUBLICSECTION.
DATA:S_LAYOUTTYPELVC_S_LAYO.
METHODS:PREPARE_LAYOUT.
ENDCLASS.

*------CLASSLCL_FIELDCATDEFINITION------
CLASSLCL_FIELDCATDEFINITION.
PUBLICSECTION.
DATA:IT_FIELDCATTYPELVC_T_FCAT.
METHODS:PREPARE_FIELDCAT.
PRIVATESECTION.
DATA:S_FIELDCATTYPELVC_S_FCAT.
ENDCLASS.

*------CLASSLCL_DATADEFINITION------
CLASSLCL_DATADEFINITION.
PUBLICSECTION.
DATA:IT_EKKOTYPETABLEOFTP_TAB.
METHODS:PREPARE_DATA.
PRIVATESECTION.
DATA:RS_DATATYPEREFTOTP_TAB.
ENDCLASS.

*------>CLASSIMPLEMENTATION------
*------CLASSLCL_LAYOUTIMPLEMENTATION------
CLASSLCL_LAYOUTIMPLEMENTATION.
METHODPREPARE_LAYOUT.
*->FisrstColumnExceptionFlag
*S_LAYOUT-EXCP_FNAME='EXPO'.
S_LAYOUT-CTAB_FNAME='CELLCOLOR'.
S_LAYOUT-INFO_FNAME='ROWCOLOR'.
ENDMETHOD.
ENDCLASS.

*------CLASSLCL_FIELDCATIMPLEMENTATION------
CLASSLCL_FIELDCATIMPLEMENTATION.
METHODPREPARE_FIELDCAT.
**---UserMaintainedFieldCat---
*DATA:LS_FCATTYPELVC_S_FCAT.
*LS_FCAT-FIELDNAME='EBELN'.
*LS_FCAT-INTTYPE='C'.
*LS_FCAT-OUTPUTLEN='10'.
*LS_FCAT-COLTEXT='Pur.Doc'.
*LS_FCAT-SELTEXT='Purchase.Doc'.
*APPENDLS_FCATTOIT_FIELDCAT.

**---AutomaticMaintainedFieldCat
*CALLFUNCTION'LVC_FIELDCATALOG_MERGE'
*EXPORTING
*I_STRUCTURE_NAME='EKKO'
*CHANGING
*CT_FIELDCAT=IT_FIELDCAT
*EXCEPTIONS
*INCONSISTENT_INTERFACE=1
*PROGRAM_ERROR=2
*OTHERS=3.
*
*IFSY-SUBRC<>0.
**--->USERCHECK-POINT.
*ENDIF.
*
**---UP+SemiautomaticMaintainedFieldCat---
*DATA:LS_FCATTYPELVC_S_FCAT.
*LOOPATIT_FIELDCATINTOLS_FCAT.
*CASELS_FCAT-FIELDNAME.
*WHEN'EBELN'.
**->FieldLenght
*LS_FCAT-OUTPUTLEN='10'.
*LS_FCAT-COLTEXT='PUR.Doc'.
*MODIFYIT_FIELDCATFROMLS_FCAT.
*ENDCASE.
*ENDLOOP.

*---MaintainedUseMicroDefineFieldCatlog---
data:LS_FIELDCATTYPELVC_S_FCAT."ALVDataFieldCatalog
"ls_fieldcatatypeslis_fieldcat_alv."ALVDisplayFieldCatalog

*Macro:_APPEND_FIELDCAT
*&1-FieldName
*&2-ReferenceTable
*&3-ReferenceTableField(whenFieldNameisdifferent)
*&4-ShortFieldLabel
*&5-MediumFieldLabel
*&6-LongFieldLabel
*&7-TextFieldName
*&8-ReferenceFieldwithQuantityUnit
*&9-Hotspot
DEFINEF1.
clear:LS_FIELDCAT."LS_FIELDCAT.
LS_FIELDCAT-FIELDNAME=&1.
LS_FIELDCAT-REF_TABLE=&2.
LS_FIELDCAT-REF_FIELD=&3.
LS_FIELDCAT-SCRTEXT_S=&4.
LS_FIELDCAT-SCRTEXT_M=&5.
LS_FIELDCAT-SCRTEXT_L=&6.
*LS_FIELDCAT-TXT_FIELD=&7.
*LS_FIELDCAT-QFIELDNAME=&8.
*LS_FIELDCAT-HOTSPOT=&9.

APPENDLS_FIELDCATTOIT_FIELDCAT.
END-OF-DEFINITION.

F1'EBELN''EKKO''EBELN''Pur.Doc#''Pur.DocNumer''PurchaseDocumentNumer'.
F1'BUKRS''EKKO''BUKRS''Pur.XXX''Pur.XXX''PurchaseXXX'.

ENDMETHOD.
ENDCLASS.

*------CLASSLCL_DATAIMPLEMENTATION------
CLASSLCL_DATAIMPLEMENTATION.
METHODPREPARE_DATA.
*---GETDATA---
SELECTA~EBELNA~BUKRS
FROMEKKOASA
INTOCORRESPONDINGFIELDSOFTABLEIT_EKKO
WHEREA~EBELNINS_EBELN
ANDA~BUKRSINS_BUKRS.

*---SETROWCOLOR---
READTABLEIT_EKKOREFERENCEINTORS_DATAINDEX2.
RS_DATA->ROWCOLOR='C110'.

*---SETCELLCOLOR---
DATA:RS_CELLTYPELVC_S_SCOL.
READTABLEIT_EKKOREFERENCEINTORS_DATAINDEX4.
RS_CELL-FNAME='BUKRS'.
RS_CELL-COLOR-COL='6'.
RS_CELL-COLOR-INT='1'.
APPENDRS_CELLTORS_DATA->CELLCOLOR.
ENDMETHOD.
ENDCLASS.

*------>ATSELECTIONSCREEN------
ATSELECTION-SCREENOUTPUT.
PERFORMMODIFY_SCREEN.

*------>START-OF-SELECTION------
START-OF-SELECTION.
IFP_ALV='X'.
*---CALLALVGIRDSCREEN.
CALLSCREEN9000.
ELSE.
PERFORMDOWNLOAD_TO_SERVER.
ENDIF.

*------FORMMODIFY_SCREEN------
FORMMODIFY_SCREEN.
LOOPATSCREEN.
IFP_ALV='X'.
IFSCREEN-GROUP1='PTH'.
SCREEN-ACTIVE='0'.
SCREEN-INVISIBLE='1'.
MODIFYSCREEN.
ENDIF.
ENDIF.
ENDLOOP.
ENDFORM.

*------>ModuleINIT_SCREENOUTPUT------
MODULEINIT_SCREENOUTPUT.
SETPF-STATUS'STAT'.
PERFORMFRM_DISPLAY_ALV.
ENDMODULE.

*------>ModuleUSER_COMMANDOUTPUT-----
MODULEUSER_COMMANDINPUT.
SAVE_OK=OK_CODE.
CASESAVE_OK.
WHEN'BACK'OR'EXIT'OR'CANC'.
LEAVEPROGRAM.
ENDCASE.
ENDMODULE.

*------>FormFRM_DISPLAY_ALV------
FORMFRM_DISPLAY_ALV.
*->LocalDataDefinition
DATA:VARTYPEDISVARIANT."Variant
IFREF_ALVISINITIAL.
*---InstanceCustomerContainer->CL_GUI_CUSTOMER_CONTAINER
CREATEOBJECTREF_CON
EXPORTING
CONTAINER_NAME='CON_ALV'.

*---CreateALVGrid---
CREATEOBJECTREF_ALV
EXPORTING
I_PARENT=REF_CON.

*---CreateFieldCatlogForALV---
CREATEOBJECTREF_FIELDCAT.
CALLMETHODREF_FIELDCAT->PREPARE_FIELDCAT.

*---SetLayoutProperty---
CREATEOBJECTREF_LAYOUT.
CALLMETHODREF_LAYOUT->PREPARE_LAYOUT.

*---GetData---
CREATEOBJECTREF_DATA.
CALLMETHODREF_DATA->PREPARE_DATA.

*---DispalyData---
CLEARVAR.
CALLMETHODREF_ALV->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_VARIANT=VAR
I_SAVE=SPACE
I_DEFAULT='X'
IS_LAYOUT=REF_LAYOUT->S_LAYOUT
CHANGING
IT_OUTTAB=REF_DATA->IT_EKKO
IT_FIELDCATALOG=REF_FIELDCAT->IT_FIELDCAT
EXCEPTIONS
INVALID_PARAMETER_COMBINATION=1
PROGRAM_ERROR=2
TOO_MANY_LINES=3
OTHERS=4.
ELSE.
CALLMETHODREF_ALV->REFRESH_TABLE_DISPLAY.
ENDIF.
ENDFORM.

*------>FORMDOWNLOAD_TO_SERVER------
FORMDOWNLOAD_TO_SERVER.
FIELD-SYMBOLS:<FS>.
DATA:L_FIELD(40)TYPEC.
DATA:L_STRING(450)TYPEC.

*---GetData---
CREATEOBJECTREF_DATA.
CALLMETHODREF_DATA->PREPARE_DATA.

DATA:IT_TAB1TYPESTANDARDTABLEOFTP_TABWITHHEADERLINE.

IT_TAB1[]=REF_DATA->IT_EKKO.

TRY.
CONCATENATE'Report_'SY-DATUM'_'
SY-UZEIT+0(2)SY-UZEIT+2(2)'.csv'INTOP_SVFILE.

DATA:BEGINOFIT_OUTOCCURS0,
PUR_NO(30),"PUR.DOCNumber
ENDOFIT_OUT.

*DATA:IT_OUTTYPESTANDARDTABLEOFTP_OUTWITHHEADERLINE.
LOOPATIT_TAB1.
*MOVE-CORRESPONDINGIT_TAB1TOIT_OUT.
IT_OUT-PUR_NO=IT_TAB1-EBELN.
APPENDIT_OUT.
CLEAR:IT_OUT.
ENDLOOP.

CONCATENATEP_SVPATHP_SVFILEINTOG_SVFILE.
OPENDATASETG_SVFILEFOROUTPUTINTEXTMODEENCODINGDEFAULT.
IFSY-SUBRC=0.
CLEARL_STRING.
CONCATENATE'PUR_NUMBER'
'BUKRS'
INTOL_STRING
SEPARATEDBY','.

TRANSFERL_STRINGTOG_SVFILE.

LOOPATIT_OUT.
CLEARL_STRING.
DO.
ASSIGNCOMPONENTSY-INDEXOFSTRUCTUREIT_OUTTO<FS>.
IFSY-SUBRCNE0.
EXIT.
ENDIF.
WRITE<FS>TOL_FIELD.
REPLACEALLOCCURRENCESOF''''INL_FIELDWITH''''''.
SHIFTL_FIELDLEFTDELETINGLEADINGSPACE.
IFSY-INDEX=1.
*---EXCEL---
*L_STRING=L_FIELD.
*---CSV---
*-e.g1->"1"fileincsv.
L_STRING=L_FIELD.
ELSE.
*---EXCEL---
*CONCATENATEL_STRINGL_FIELDINTOL_STRING
*SEPARATEDBY
*CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
*---CSV---
CONCATENATEL_STRINGL_FIELDINTOL_STRING
SEPARATEDBY','.

ENDIF.
ENDDO.
TRANSFERL_STRINGTOG_SVFILE.
ENDLOOP.
CLOSEDATASETG_SVFILE.

WRITE:/'Exportissuccessful.'.
WRITE:/'Temporaryfilecreatedonserver:',G_SVFILE.
ELSE.
MESSAGEE999(b1)WITH:'Exportisfailed!'.
ENDIF.
CATCHCX_ROOT.
WRITE:/'Temporaryfilewritingerroronserver.'.
MESSAGEE999(b1)WITH:'Temporaryfilewritingerroronserver!'.
ENDTRY.
ENDFORM.

你可能感兴趣的:(C++,c,Excel,C#,OO)