SAP逐行寫出Excel檔案

TolyHuang發表於2007-05-16

report ztest_export_excel.
include ole2incl.
data: application type ole2_object,
workbook type ole2_object,
sheet type ole2_object,
cells type ole2_object.

constants: row_max type i value 256.
data index type i.
data: begin of itab occurs 0,
first_name(10),
last_name(10),
address(100),
end of itab.

start-of-selection.

...

[@more@]

"Add data to itab

itab-first_name = '123445'.
itab-last_name = 'tesst'.
itab-address = '中國'.
append itab.
clear itab.


itab-first_name = 'ABCDE'.
itab-last_name = 'tessasdfasdft'.
itab-address = '中國中山'.
append itab.
clear itab1.

itab-first_name = '123456'.
itab-last_name = 'tessasdfasdft'.
itab-address = '中國中山'.
append itab.
clear itab.


create object application 'excel.application'.
set property of application 'visible' = 1.
call method of application 'Workbooks' = workbook.
call method of workbook 'Add'.


call method of application 'Worksheets' = sheet
exporting #1 = 1.

call method of sheet 'Activate'.
set property of sheet 'Name' = 'Sheet1'.

"寫標題
index = 1. " 1 - column name
call method of sheet 'Cells' = cells exporting #1 = index.
set property of cells 'Value' = 'Field1'.

index = index + 1. " 1 - column name
call method of sheet 'Cells' = cells exporting #1 = index.
set property of cells 'Value' = 'Field2'.

index = index + 1. " 1 - column name
call method of sheet 'Cells' = cells exporting #1 = index.
set property of cells 'Value' = 'Field3'.

"寫資料
loop at itab.
index = row_max * sy-tabix + 1. " 1 - column name
call method of sheet 'Cells' = cells exporting #1 = index.
set property of cells 'Value' = itab-first_name.

index = index + 1. " 1 - column name
call method of sheet 'Cells' = cells exporting #1 = index.
set property of cells 'Value' = itab-last_name.

index = index + 1. " 1 - column name
call method of sheet 'Cells' = cells exporting #1 = index.
set property of cells 'Value' = itab-address.
endloop.


* 儲存檔案
call method of sheet 'SaveAs'

exporting #1 = 'c:tempexceldoc1.xls' "filename

#2 = 1. "fileFormat

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9437124/viewspace-915245/,如需轉載,請註明出處,否則將追究法律責任。

相關文章