Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Sample code to download data to .CSV file using OLE method

Former Member
0 Likes
1,651

Hi All,

I need some sample code to download data to .CSV file using OLE method.

Also i need to format one of the columns to text. I have not used OLE method before, Please

provide me with some sample code for this.

Thanks in advance,

Srilakshmi.

3 REPLIES 3
Read only

former_member195383
Active Contributor
0 Likes
805

check for the link ...

http://www.sapdevelopment.co.uk/ms/ms_excel.htm

Inside that code try the below thing....

DESCRIBE TABLE it[] LINES wf_it_line.

CALL METHOD OF wf_excel 'Cells' = wf_cellx

EXPORTING

#1 = 1

#2 = 5.

CALL METHOD OF wf_excel 'Cells' = wf_celly

EXPORTING

#1 = wf_it_line

#2 = 5.

CALL METHOD OF wf_excel 'Range' = wf_cellr

EXPORTING

#1 = wf_cellx

#2 = wf_celly.

SET PROPERTY OF wf_cellr 'NumberFormat' = '@' .

The above code will make the 5th coloumn, as text format . where you will get '0004' instead of '4' .

You can change the value given to #2 to format your required coloumn.

Read only

0 Likes
805

hi,

can you paste the code from that link, because that link is not opening for me.

Thanks,

Srilakshmi.

Read only

0 Likes
805

Hi I m posting the below part which is part of my code....

data : wf_cell1 TYPE ole2_object,

wf_cellx TYPE ole2_object,

wf_celly TYPE ole2_object,

wf_cellr TYPE ole2_object,

wf_it_line type i,

wf_cell2 TYPE ole2_object,

wf_excel TYPE ole2_object,

wf_mapl TYPE ole2_object,

wf_map TYPE ole2_object,

wf_worksheet TYPE ole2_object,

wf_tmp_filename LIKE rlgrap-filename,

wf_cell TYPE ole2_object,

wf_range TYPE ole2_object,

wf_gs_font TYPE ole2_object,

wf_zl TYPE ole2_object,

wf_sheet2 TYPE ole2_object,

wf_sheet3 TYPE ole2_object,

flg_stop(1) TYPE c,

wf_deli(1) TYPE c,

l_rc TYPE i,

wf_cnt TYPE i.

CLEAR it.
  REFRESH it.
  CREATE OBJECT application 'excel.application'.
*Start the excel.

  CLEAR : wa_sheets.
  ASSIGN wf_deli TO <fs> TYPE 'X'.
  t_hex-l_tab = co_c09.
  <fs> = t_hex-l_tab.  "Assign the delimiter to field symbol.

* Test heading in the report start.





  LOOP AT tb_output1.
  

    CONCATENATE tb_output1-field1
                tb_output1-field2
 
    INTO it
    SEPARATED BY wf_deli.
    APPEND it.
    CLEAR it.
  ENDLOOP.

 

******

  DATA: excel TYPE ole2_object ,
        workbook TYPE ole2_object,
        range TYPE ole2_object.

  DATA: sheetname(10) VALUE 'TEST ',c_row TYPE i,
  scnt TYPE i,
  val(20), wb(2).


  CREATE OBJECT excel 'EXCEL.APPLICATION'.


  SET PROPERTY OF excel 'VISIBLE' = 0.


*
  IF wf_excel-header = space OR wf_excel-handle = -1.
    CREATE OBJECT wf_excel 'EXCEL.APPLICATION'.
  ENDIF.

*--- get list of workbooks, initially empty
  CALL METHOD OF wf_excel 'Workbooks' = wf_mapl.
  SET PROPERTY OF wf_excel 'Visible' = 1.



  CALL METHOD OF wf_mapl 'Add' = wf_map.




* Sheet 1---Begin .
  gv_sheet_name = text-t36.

  GET PROPERTY OF wf_excel 'Sheets' = wf_sheet3 .
  CALL METHOD OF wf_sheet2 'Add' = wf_map.

  GET PROPERTY OF wf_excel 'ACTIVESHEET' = wf_worksheet.
  SET PROPERTY OF wf_worksheet 'Name' = gv_sheet_name .

*--Formatting the area of additional data 1 and doing the BOLD
  CALL METHOD OF wf_excel 'Cells' = wf_cell1
  EXPORTING
  #1 = 1
  #2 = 1.
  CALL METHOD OF wf_excel 'Cells' = wf_cell2
  EXPORTING
  #1 = 1
  #2 = 50.
  CALL METHOD OF wf_excel 'Range' = wf_cell
  EXPORTING
  #1 = wf_cell1
  #2 = wf_cell2.



  GET PROPERTY OF wf_cell 'Font' = wf_gs_font .
  SET PROPERTY OF wf_gs_font 'Bold' = 1 .



* Changing the format 2nd coloumn
* so that 0 is not removed at the start if present.
  CLEAR wf_it_line.

  DESCRIBE TABLE it[] LINES wf_it_line.
  wf_it_line = wf_it_line + 8.
  CLEAR : wf_cellx, wf_celly, wf_cellr.
  CALL METHOD OF wf_excel 'Cells' = wf_cellx
  EXPORTING
   #1 = 1
   #2 = 2.

  CALL METHOD OF wf_excel 'Cells' = wf_celly
  EXPORTING
   #1 = wf_it_line
   #2 = 2.

  CALL METHOD OF wf_excel 'Range' = wf_cellr
   EXPORTING
 #1 = wf_cellx
 #2 = wf_celly.

  SET PROPERTY OF wf_cellr 'NumberFormat' = '@' .



***************
  CLEAR wf_it_line.


* cell test  end


*  DATA wf_l_rc TYPE i.

  CALL METHOD cl_gui_frontend_services=>clipboard_export
    IMPORTING
      data                 = it[]
    CHANGING
      rc                   = l_rc
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
*      not_supported_by_gui = 3
      OTHERS               = 4.

  CALL METHOD OF wf_excel 'Cells' = wf_cell1
    EXPORTING
     #1 = 1
     #2 = 1.
  CALL METHOD OF wf_excel 'Cells' = wf_cell2
    EXPORTING
     #1 = 1
     #2 = 1.
  CALL METHOD OF wf_excel 'Range' = wf_range
    EXPORTING
     #1 = wf_cell1
     #2 = wf_cell2.



  CALL METHOD OF wf_range 'Select'.
  CALL METHOD OF wf_worksheet 'Paste'.



* Sheet 1---End Exp.




* Saving the Excel


  wf_fname = pa_file.

  GET PROPERTY OF excel 'ActiveSheet' = sheet.

  FREE OBJECT wf_worksheet.

  GET PROPERTY OF wf_excel 'ActiveWorkbook' = wf_mapl.
*call method of wf_mapl 'SAVEAS' exporting #1 = 'D:\exceldoc3.xls'
*#2 = 1.
  CALL METHOD OF wf_mapl 'SAVEAS' EXPORTING #1 = wf_fname
  #2 = 1.

  IF sy-subrc = 0.

  ENDIF.


  CALL METHOD OF wf_mapl 'CLOSE'.
  CALL METHOD OF wf_excel 'QUIT'.


  FREE OBJECT wf_worksheet.
  FREE OBJECT wf_excel.




*--- disconnect from Excel
  FREE OBJECT wf_zl.
  FREE OBJECT wf_mapl.
  FREE OBJECT wf_map.
  FREE OBJECT wf_excel.

in the above code tb_output1 contains the two coloumns whose data u wanna show.

U are transferring that to it[] which will be converted to Excel through the above ole technique.