ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 内表写入EXCEL

内表写入EXCEL

原创 Linux操作系统 作者:shangchongtian 时间:2009-01-13 12:29:52 0 删除 编辑
 
*******************单个单元格操作如下,可操作单个单元格子的数据,格式,函数.总之,VBA能用的在此都能用
一.一段代码:
CONSTANTS: OK TYPE I VALUE 0.
INCLUDE OLE2INCL.
DATA: EXCEL TYPE OLE2_OBJECT,
WORKBOOK TYPE OLE2_OBJECT,
SHEET TYPE OLE2_OBJECT,
CELL TYPE OLE2_OBJECT,
ROW TYPE OLE2_OBJECT.
DATA:col_num TYPE i VALUE 0,
     row_num TYPE I VALUE 0.

CREATE OBJECT EXCEL 'EXCEL.APPLICATION'.
IF SY-SUBRC NE OK.
MESSAGE I000(zd003) WITH SY-MSGLI.
ENDIF.
CALL METHOD OF EXCEL 'WORKBOOKS' = WORKBOOK .
set property of EXCEL 'SHEETSINNEWWORKBOOK' = 1.
call method of WORKBOOK 'ADD'.
SET PROPERTY OF EXCEL 'VISIBLE' = 1 .
DATA:ITEMS TYPE I VALUE  0.
DESCRIBE TABLE IT_ZINVOICE4_SUB[] LINES ITEMS.
CHECK ITEMS > 0.

LOOP AT IT_ZINVOICE4_SUB.
  row_num = row_num + 1.
DO 10 TIMES.
  col_num = col_num + 1.
CALL METHOD OF EXCEL 'CELLS' = CELL EXPORTING #1 = row_num #2 = col_num.
CASE col_num.
  WHEN 1.
SET PROPERTY OF CELL 'VALUE' = IT_ZINVOICE4_SUB-VBELN NO FLUSH.
FREE OBJECT CELL.
  WHEN 2.
SET PROPERTY OF CELL 'VALUE' = IT_ZINVOICE4_SUB-BUKRS NO FLUSH.
FREE OBJECT CELL.
  WHEN 3.
SET PROPERTY OF CELL 'VALUE' = IT_ZINVOICE4_SUB-TAX_MONEY NO FLUSH.
FREE OBJECT CELL.
  WHEN 4.
SET PROPERTY OF CELL 'VALUE' = IT_ZINVOICE4_SUB-DEMO NO FLUSH.
FREE OBJECT CELL.
  WHEN 5.
SET PROPERTY OF CELL 'VALUE' = IT_ZINVOICE4_SUB-TAX_CX NO FLUSH.
FREE OBJECT CELL.
  WHEN 6.
SET PROPERTY OF CELL 'VALUE' = IT_ZINVOICE4_SUB-TEXT_NO NO FLUSH.
FREE OBJECT CELL.
  WHEN 7.
SET PROPERTY OF CELL 'VALUE' = IT_ZINVOICE4_SUB-TAX_TYPE NO FLUSH.
FREE OBJECT CELL.
  WHEN 8.
SET PROPERTY OF CELL 'VALUE' = IT_ZINVOICE4_SUB-PRINT_T NO FLUSH.
FREE OBJECT CELL.
  WHEN 9.
SET PROPERTY OF CELL 'VALUE' = IT_ZINVOICE4_SUB-DATE_D NO FLUSH.
FREE OBJECT CELL.
  WHEN 10.
ENDCASE.
ENDDO.
  CLEAR col_num.
ENDLOOP.

  **Free all objects
*FREE OBJECT CELL.
*FREE OBJECT WORKBOOK.
*FREE OBJECT EXCEL.
*EXCEL-HANDLE = -1.
*FREE OBJECT ROW.

二,一个完整的程序:
SPAN {font-family: "Courier New";font-size: 10pt;color: #000000;background: #FFFFFF;}.L1S52 {color: #0000FF;}report  ztest_excel.
SPAN {font-family: "Courier New";font-size: 10pt;color: #000000;background: #FFFFFF;}.L1S31 {font-style. italic;color: #808080;}.L1S32 {color: #3399FF;}.L1S33 {color: #4DA619;}.L1S52 {color: #0000FF;}include ole2incl.
data: excel_object type ole2_object, " EXCEL OBJECT
workbook_list type ole2_object, "LIST OF WORKBOOKS
workbook type ole2_object, " WORKBOOK
cell type ole2_object, " CELL
font type ole2_object. " FONT
tables: pa0002.
data row_number type i.
data: it_pa0002 like pa0002 occurs 10 with header line.
start-of-selection.
select * from pa0002 into table it_pa0002 up to 10 rows.
create object excel_object 'EXCEL.APPLICATION'.
set property of excel_object 'VISIBLE' = 1.
call method of excel_object 'WORKBOOKS' = workbook_list.
call method of workbook_list 'ADD' = workbook.
* FILL THE ACTIVE SHEET WITH COLUMN HEADINGS
perform. fill_cell_with_data using 1 1 1 1 'Employee Number'.
perform. fill_cell_with_data using 1 2 1 1 'First Name'.
perform. fill_cell_with_data using 1 3 1 1 'Last Name'.
perform. fill_cell_with_data using 1 4 1 1 'Date of Birth'.
perform. fill_cell_with_data using 1 5 1 3 'Place of Birth'.
* FILL EMPLOYEE DATA TO ACTIVE EXCEL SHEET
loop at it_pa0002.
row_number = sy-tabix + 1.
perform. fill_cell_with_data using row_number 1 0 1 it_pa0002-pernr.
perform. fill_cell_with_data using row_number 2 0 1 it_pa0002-vorna.
perform. fill_cell_with_data using row_number 3 0 1 it_pa0002-nachn.
perform. fill_cell_with_data using row_number 4 0 1 it_pa0002-gbdat.
perform. fill_cell_with_data using row_number 5 0 1 '=SUM(A2:A11)'."IT_PA0002-GBORT.
endloop.
free object excel_object.
form. fill_cell_with_data using row column bold color val .
call method of excel_object
'CELLS' = cell
exporting #1 = row
#2 = column.
set property of cell 'VALUE' = val.
get property of cell 'FONT' = font.
set property of font 'BOLD' = bold.
set property of font 'COLORINDEX' = color.
endform.

三,
*************************** 另有函数,可以快速导入
CALL FUNCTION 'WS_EXCEL'
EXPORTING   FILENAME            = 'C:\TEST.XLS'
*   SYNCHRON            = ' '
  TABLES
    DATA                = IT_ZINVOICE4_SUB[]
* EXCEPTIONS
* UNKNOWN_ERROR       = 1


*************************附EXCLE导入内表
另有EXCLE导入内表函数
ALSM_EXCEL_TO_INTERNAL_TABLE
使用如下:


TEST.XLS表格的33列的9个单元格子


TEST.XLS记录如下:




结果如下:


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9633129/viewspace-536596/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2009-01-13

  • 博文量
    18
  • 访问量
    31410