ABAP는 EXCEL 파일을 자동으로 생성하고 간단한 형식으로 처리합니다.

9085 단어 Excel
*&---------------------------------------------------------------------*

*& Report  ZDEMO_EXCEL13

*&

*&---------------------------------------------------------------------*

*& Example by: Liaojunbo.

*& 

*&   , Excel 

*&---------------------------------------------------------------------*



REPORT  zharpo_excel1.



DATA: lo_excel                TYPE REF TO zcl_excel,

      lo_excel_writer         TYPE REF TO zif_excel_writer,

      lo_worksheet            TYPE REF TO zcl_excel_worksheet,

      lv_style_bold_border_guid TYPE zexcel_cell_style,

      lo_style_bold_border TYPE REF TO zcl_excel_style,

      lo_border_dark TYPE REF TO zcl_excel_style_border.



DATA: lv_file                 TYPE xstring,

      lv_bytecount            TYPE i,

      lt_file_tab             TYPE solix_tab.



DATA: lv_full_path      TYPE string,

      lv_workdir        TYPE string,

      lv_file_separator TYPE c.



CONSTANTS: lv_default_file_name TYPE string VALUE 'MergedCells.xlsx'.



PARAMETERS: p_path TYPE zexcel_export_dir.



AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path.

  lv_workdir = p_path.

  cl_gui_frontend_services=>directory_browse( EXPORTING initial_folder  = lv_workdir

                                              CHANGING  selected_folder = lv_workdir ).

  p_path = lv_workdir.



INITIALIZATION.

  cl_gui_frontend_services=>get_sapgui_workdir( CHANGING sapworkdir = lv_workdir ).

  cl_gui_cfw=>flush( ).

  p_path = lv_workdir.



START-OF-SELECTION.



  IF p_path IS INITIAL.

    p_path = lv_workdir.

  ENDIF.

  cl_gui_frontend_services=>get_file_separator( CHANGING file_separator = lv_file_separator ).

  CONCATENATE p_path lv_file_separator lv_default_file_name INTO lv_full_path.



  CREATE OBJECT lo_excel.



  " Get active sheet

  lo_worksheet = lo_excel->get_active_worksheet( ).

  lo_worksheet->set_title( 'sheet1' ).



  CREATE OBJECT lo_border_dark.

  lo_border_dark->border_color-rgb = zcl_excel_style_color=>c_black.

  lo_border_dark->border_style = zcl_excel_style_border=>c_border_thin.



  lo_style_bold_border = lo_excel->add_new_style( ).

  lo_style_bold_border->font->bold = abap_true.

  lo_style_bold_border->font->italic = abap_false.

  lo_style_bold_border->font->color-rgb = zcl_excel_style_color=>c_black.

  lo_style_bold_border->alignment->horizontal = zcl_excel_style_alignment=>c_horizontal_center.

  lo_style_bold_border->borders->allborders = lo_border_dark.

  lv_style_bold_border_guid = lo_style_bold_border->get_guid( ).



  lo_worksheet->set_cell( ip_row = 2 ip_column = 'A' ip_value = 'Test' ).



  lo_worksheet->set_cell( ip_row = 2 ip_column = 'B' ip_value = 'Banana' ip_style = lv_style_bold_border_guid ).

  lo_worksheet->set_cell( ip_row = 2 ip_column = 'C' ip_value = '' ip_style = lv_style_bold_border_guid ).

  lo_worksheet->set_cell( ip_row = 2 ip_column = 'D' ip_value = '' ip_style = lv_style_bold_border_guid ).

  lo_worksheet->set_cell( ip_row = 2 ip_column = 'E' ip_value = '' ip_style = lv_style_bold_border_guid ).

  lo_worksheet->set_cell( ip_row = 2 ip_column = 'F' ip_value = '' ip_style = lv_style_bold_border_guid ).

  lo_worksheet->set_cell( ip_row = 2 ip_column = 'G' ip_value = '' ip_style = lv_style_bold_border_guid ).

  lo_worksheet->set_cell( ip_row = 4 ip_column = 'B' ip_value = 'Apple' ip_style = lv_style_bold_border_guid ).

  lo_worksheet->set_cell( ip_row = 4 ip_column = 'C' ip_value = '' ip_style = lv_style_bold_border_guid ).

  lo_worksheet->set_cell( ip_row = 4 ip_column = 'D' ip_value = '' ip_style = lv_style_bold_border_guid ).

  lo_worksheet->set_cell( ip_row = 4 ip_column = 'E' ip_value = '' ip_style = lv_style_bold_border_guid ).

  lo_worksheet->set_cell( ip_row = 4 ip_column = 'F' ip_value = '' ip_style = lv_style_bold_border_guid ).

  lo_worksheet->set_cell( ip_row = 4 ip_column = 'G' ip_value = '' ip_style = lv_style_bold_border_guid ).



  lo_worksheet->set_merge( ip_row = 4 ip_column_start = 'B' ip_column_end = 'G' ).

  lo_worksheet->set_merge( ip_row = 6 ip_column_start = 'B' ip_column_end = 'G' ).



  " Test also if merge works when oher merged chells are empty

  lo_worksheet->set_cell( ip_row = 6 ip_column = 'B' ip_value = 'Tomato' ).

  lo_worksheet->set_merge( ip_row = 6 ip_column_start = 'B' ip_column_end = 'G' ).



  " Test the patch provided by Victor Alekhin to merge cells in one column

  lo_worksheet->set_cell(  ip_row = 8 ip_column       = 'B' ip_value = 'Merge cells also over multiple rows by Victor Alekhin' ).

  lo_worksheet->set_merge( ip_row = 8 ip_column_start = 'B' ip_column_end = 'G' ip_row_to = 10 ).



  CREATE OBJECT lo_excel_writer TYPE zcl_excel_writer_2007.

  lv_file = lo_excel_writer->write_file( lo_excel ).



  " Convert to binary

  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'

    EXPORTING

      buffer        = lv_file

    IMPORTING

      output_length = lv_bytecount

    TABLES

      binary_tab    = lt_file_tab.

*  " This method is only available on AS ABAP > 6.40

*  lt_file_tab = cl_bcs_convert=>xstring_to_solix( iv_xstring  = lv_file ).

*  lv_bytecount = xstrlen( lv_file ).



  " Save the file

  cl_gui_frontend_services=>gui_download( EXPORTING bin_filesize = lv_bytecount

                                                    filename     = lv_full_path

                                                    filetype     = 'BIN'

                                           CHANGING data_tab     = lt_file_tab ).

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

좋은 웹페이지 즐겨찾기