abap 用excel將alv上展示的數據導出(基本知識)


abap 用excel將alv上展示的數據導出(基本知識)

轉自:http://blog.sina.com.cn/s/blog_98dd7e1101013crl.html

form user_command using r_ucomm like sy-ucomm
                        rs_selfield 
type  slis_selfield .
  
case  r_ucomm.
    
when 'EX'.alv菜單欄上的按鈕)
      
perform excel_data.
  
endcase.
endform.  

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

form excel_data .
  
perform def_excel.
  
perform sho_excel.
endform.                    " EXECL_DATA
*--------------------------------------------------------------------
form def_excel .

(以下的代碼是必須寫的,比較固定,用的時候直接寫就行)
  
define fill_cell.
    
call method of v_excel  'CELLS' v_cell "單元格位置
       
exporting
         #1 
&1
         #2 
&2.
    
set property of v_cell 'value' &3.
    
call method of v_cell 'FONT'  v_font.
    
set property of v_font 'Bold' &4.
    
set property of v_font 'size' &5.
  
end-of-definition.

  
create object v_excel 'Excel.Application'.
  
call method of
      v_excel
      
'Workbooks' v_book.
  
call method of
      v_book
      
'ADD'.
*給第一行填充要寫的東西,一下的第一個‘1’表示第一行,第二個‘1’表示第一列。
  fill_cell 
1  1  '生產情況形象進度統計表1 20.
  
set property of v_cell 'HorizontalAlignment' -4108.這句是居中)
  
set property of v_font 'UNDERLINE' 2.(下划線)

 

以下代碼主要作用是定義第二行各列和第三行各列、第四行各列:

  fill_cell 2 1   ''   1 10.
  fill_cell 
2 2   ''   1 10.
  fill_cell 
2 3   ''   1 10.
  fill_cell 
2 4   ''   1 10.
  fill_cell 
2 5   ''   1 10.
  fill_cell 
2 6   ''   1 10.
  fill_cell 
2 7   '統計時間'   1 10.
  fill_cell 
2 8   ''   1 10.
  fill_cell 
2 9   ''   1 10.
  fill_cell 
2 10  ''  1 10.
*  fill_cell 3 10  ''   1 10.
  fill_cell 
2 11 ''   1 10.
  fill_cell 
2 12  ''   1 10.
  fill_cell 
2 13  ''   1 10.
  fill_cell 
2 14  ''   1 10.


  fill_cell 
3 1   ''   1 10.
  fill_cell 
3 2   ''   1 10.
  fill_cell 
3 3   ''   1 10.
  fill_cell 
3 4   ''   1 10.
  fill_cell 
3 5   ''   1 10.
  fill_cell 
3 6   ''   1 10.
  fill_cell 
3 7   ''   1 10.
  fill_cell 
3 8   ''   1 10.
  fill_cell 
3 9   ''   1 10.
  fill_cell 
3 10  '編號'  1 10.
*  fill_cell 3 10  ''   1 10.
  fill_cell 
3 11 ex_itab-num1   1 10.
  fill_cell 
3 12  ''   1 10.
  fill_cell 
3 13  ''   1 10.
  fill_cell 
3 14  ''   1 10.
*
  fill_cell 
4 1   '序號'          1 10.
  
set property of v_cell 'HorizontalAlignment' -4108.
  fill_cell 
4 2   '工廠'          1 10.
  
set property of v_cell 'HorizontalAlignment' -4108.
  fill_cell 
4 3   '生產訂單'      1 10.
  
set property of v_cell 'HorizontalAlignment' -4108.
  fill_cell 
4 4   '訂單描述'      1 10.
  
set property of v_cell 'HorizontalAlignment' -4108.
  fill_cell 
4 5   '計划下達時間'      1 10.
  
set property of v_cell 'HorizontalAlignment' -4108.
  fill_cell 
4 6   '實際下達時間'      1 10.
  
set property of v_cell 'HorizontalAlignment' -4108.
  fill_cell 
4 7   '計划完工時間'          1 10.
  
set property of v_cell 'HorizontalAlignment' -4108.
  fill_cell 
4 8   '實際完工時間'          1 10.
  
set property of v_cell 'HorizontalAlignment' -4108.
  fill_cell 
4 9   '完工工時'        1 10.
  
set property of v_cell 'HorizontalAlignment' -4108.
  fill_cell 
4 10   '總工時'          1 10.
  
set property of v_cell 'HorizontalAlignment' -4108.
  fill_cell 
4 11  '完成進度'          1 10.
  
set property of v_cell 'HorizontalAlignment' -4108.
  fill_cell 
4 12  '計划資金'          1 10.
  
set property of v_cell 'HorizontalAlignment' -4108.
  fill_cell 
4 13  '進度資金'          1 10.
  
set property of v_cell 'HorizontalAlignment' -4108.
  fill_cell 
4 14  '備注'              1 10.
  
set property of v_cell 'HorizontalAlignment' -4108.

*************先把第一行的所有列都合並****************************
  
call method of
      v_excel
      
'Range' v_range
    
exporting
      #1      
'A1'這里的意思是從A1N1這個范圍合並
      #2      
'N1'.
  
call method of
      v_range
      
'Select'.
  
set property of v_range 'MergeCells' 1.
**************************一行一列合並
  
call method of
      v_excel
      
'COLUMNS' v_column
    
exporting
      #1        
'E'."列編號
  
set property of v_column 'ColumnWidth' 10.
  
set property of v_column 'NumberFormat' '@'.
endform.                    " DEF_EXCEL

*&-------------------------------------------------------------------*--------------------------------------------------------------------
form sho_excel .
  
field-symbols :  type any ,
                  
type any.
  
data: lv_txt(50type c.

  
data: row_i type i,     定義行和列
        col_i type i.

*** 從內表循環數據,按順序填到單元表中

  
loop at  alv_itab.(將alv的數據傳到為excel定義的內表中)
    
move alv_itab-num     to ex_itab-num.
    
move alv_itab-werks   to ex_itab-werks.
    
move alv_itab-aufnr   to ex_itab-aufnr.
    
move alv_itab-maktx   to ex_itab-maktx.
    
move alv_itab-ftrms   to ex_itab-ftrms.
    
move alv_itab-ftrmi   to ex_itab-ftrmi.
    
move alv_itab-gltrs   to ex_itab-gltrs.
    
move alv_itab-idat2   to ex_itab-idat2.
    
move alv_itab-ism01   to ex_itab-ism01.
    
move alv_itab-vgw01   to ex_itab-vgw01.
    
move alv_itab-rate    to ex_itab-rate.
    
move alv_itab-jhzj    to ex_itab-jhzj.
    
move alv_itab-jdzj    to ex_itab-jdzj.
    
move alv_itab-beizhu  to ex_itab-beizhu.
    
move alv_itab-zijin1  to ex_itab-zijin1.
    
move alv_itab-zijin2  to ex_itab-zijin2.
    
move alv_itab-num1    to ex_itab-num1.
    
append ex_itab.
  
endloop.


*  loop at ex_itab assigning .表示是分配一個工作區>
    row_i 
sy-tabix + 4.(意思是從第五行開始添加循環傳進去的數據)
    
do 13 times.(循環13列,切記,不是行,是列)
      col_i 
sy-index .
      
assign component sy-index of structure   to .
      lv_txt 
.
      fill_cell row_i col_i lv_txt 
0 10.
    
enddo.

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

這段代碼的意思大致就是保持格式的意思吧~~~~我也不是特別理解,但是對它的重要性深有體會啊啊啊啊啊!!!!!!!
   call method of
            v_excel
            'COLUMNS' v_column
          exporting
            #1        12.(12指的是12列,意思就是第12列的數據按char型導出,長短什么的都不發生變化,若是想要輸出的字段是資金型的,那這段代碼就必不可少了,O(∩_∩)O~)
        set property of v_column 'ColumnWidth' 10.
        set property of v_column 'NumberFormat' '@'.
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
    ENDDO.
 

  
endloop.


  fill_cell  
3 11 ex_itab-num1 1 10.(這里的ex_itab-num1excel內表字段
  fill_cell  2 8  sy-datum  1 8.
 

 row_i row_i + 1.

  fill_cell row_i 11 '合計:' 1 10 .
  fill_cell row_i 
12  ex_itab-zijin1 1 10.
  fill_cell row_i 
13  ex_itab-zijin2 1 10.
  row_i 
row_i + 1.
  fill_cell row_i  
'審核:' 1 10.
  fill_cell row_i 
11 '制編人:' 1 10.


  
data: post  type string,
         col  
type c value 'N',(‘N’表示excel上最后一列的號)
         row  
type string.
  row_i 
row_i 2.(減去二的意思是:最后兩行不用划線)
  move row_i   to row.
  
concatenate 'N' row into post.

  
call method of
      v_excel
      
'Range' v_range
    
exporting
      #1      
'A4'"A4表示從A4開始畫表格線
      #2      post.
  
call method of
      v_range
      
'BORDERS' v_border.

  
set property of v_border 'LineStyle' '1'.
  
set property of v_border 'WEIGHT' '2 '.
  
set property of v_border 'ColorIndex' '1'.
  
free object v_border.
  
free object v_cell.

  
call method of
      v_excel
      
'Columns' v_column.
  
call method of
      v_column
      
'Autofit'.

*** 設置excel為可見
  
set property of  v_excel 'Visible' 1.

endform.                    "sho_excel

 

展示結果如下:



若是在表頭部分有合並單元格的,那么代碼如下。

****一行一列和二行一列合並
  
call method of
      v_excel
      
'Range' v_range
    
exporting
      #1      
'A1'(意思是A1A2合並了)
      #2      
'A2'.
  
call method of
      v_range
      
'Select'.
  
set property of v_range 'MergeCells' 1.
*****
  
call method of
      v_excel
      
'Range' v_range
    
exporting
      #1      
'B1'
      #2      
'B2'.
  
call method of
      v_range
      
'Select'.
  
set property of v_range 'MergeCells' 1.
****
  
call method of
      v_excel
      
'Range' v_range
    
exporting
      #1      
'C1'
      #2      
'C2'.
  
call method of
      v_range
      
'Select'.
  
set property of v_range 'MergeCells' 1.
***
  
call method of
      v_excel
      
'Range' v_range
    
exporting
      #1      
'D1'
      #2      
'D2'.
  
call method of
      v_range
      
'Select'.
  
set property of v_range 'MergeCells' 1.
****
  
call method of
      v_excel
      
'Range' v_range
    
exporting
      #1      
'E1'
      #2      
'E2'.
  
call method of
      v_range
      
'Select'.
  
set property of v_range 'MergeCells' 1.
***
  
call method of
      v_excel
      
'Range' v_range
    
exporting
      #1      
'H1'
      #2      
'H2'.
  
call method of
      v_range
      
'Select'.
  
set property of v_range 'MergeCells' 1.
***
  
call method of
      v_excel
      
'Range' v_range
    
exporting
      #1      
'I1'
      #2      
'I2'.
  
call method of
      v_range
      
'Select'.
  
set property of v_range 'MergeCells' 1.
***
  
call method of
      v_excel
      
'Range' v_range
    
exporting
      #1      
'F1'(意思是F1G1合並了)
      #2      
'G1'.
  
set property of v_cell 'HorizontalAlignment' -4108.
  
call method of
      v_range
      
'Select'.
  
set property of v_range 'MergeCells' 1.
  
call method of
      v_excel
      
'Range' v_range
    
exporting
      #1      
'J1'(意思是J1K1合並了)
      #2      
'K1'.
  
set property of v_cell 'HorizontalAlignment' -4108.
  
call method of
      v_range
      
'Select'.
  
set property of v_range 'MergeCells' 1.
結果展示如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM