Application Development 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: 

Export into excel, problem with encoding

Former Member
0 Kudos

Hi experts,

I want to save my alv content into a local file type spreadshit then when I open my excel table I see very the data in a very strange order. Everything is in the wrong place and the columns are mixed up.

What could be the problem?

4 REPLIES 4

Former Member
0 Kudos

Hi,

If your problem is the one I'm thinking about, you have all your date and amount fields moved to the right of your sheet instead of being displayed at their normal place.

These fields are the ones where a format conversion is needed when exporting data to excel.

As far as I know, the export is completely standard so I think you should look for an OSS note regarding this issue (if there is one...).

Regards,

Nicolas

Former Member
0 Kudos

Use the FM - ALV_XXL_CALL. here is the sample -

REPORT ZSKC_ALV_XXL.

TYPE-POOLS : KKBLO.

DATA : ITAB LIKE T100 OCCURS 0,

T_FCAT_LVC TYPE LVC_S_FCAT OCCURS 0 WITH HEADER LINE,

T_FCAT_KKB TYPE KKBLO_T_FIELDCAT.

START-OF-SELECTION.

  • Get data.

SELECT * UP TO 20 ROWS

FROM T100

INTO TABLE ITAB

WHERE SPRSL = SY-LANGU.

CHECK SY-SUBRC EQ 0.

  • Create the field catalog.

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

EXPORTING

I_STRUCTURE_NAME = 'T100'

CHANGING

CT_FIELDCAT = T_FCAT_LVC[]

EXCEPTIONS

INCONSISTENT_INTERFACE = 1

PROGRAM_ERROR = 2

OTHERS = 3.

CHECK SY-SUBRC EQ 0.

  • make sure you pass the correct internal table name in the field catalog.

t_fcat_lvC-tabname = 'ITAB'.

MODIFY T_FCAT_LVC TRANSPORTING TABNAME WHERE TABNAME NE SPACE.

  • Transfer to KKBLO format.

CALL FUNCTION 'LVC_TRANSFER_TO_KKBLO'

EXPORTING

IT_FIELDCAT_LVC = T_FCAT_LVC[]

IMPORTING

ET_FIELDCAT_KKBLO = T_FCAT_KKB

EXCEPTIONS

IT_DATA_MISSING = 1

IT_FIELDCAT_LVC_MISSING = 2

OTHERS = 3.

CHECK SY-SUBRC EQ 0.

  • Call XXL.

CALL FUNCTION 'ALV_XXL_CALL'

EXPORTING

I_TABNAME = 'ITAB'

IT_FIELDCAT = T_FCAT_KKB

TABLES

IT_OUTTAB = ITAB[]

EXCEPTIONS

FATAL_ERROR = 1

NO_DISPLAY_POSSIBLE = 2

OTHERS = 3.

IF SY-SUBRC <> 0.

ENDIF.

Former Member
0 Kudos

Download a report to excel with format (border, color cell, etc)

Try this program...it may help you to change the font ..etc.

Code:

REPORT ZSIRI NO STANDARD PAGE HEADING.

  • this report demonstrates how to send some ABAP data to an

  • EXCEL sheet using OLE automation.

INCLUDE OLE2INCL.

  • handles for OLE objects

DATA: H_EXCEL TYPE OLE2_OBJECT, " Excel object

H_MAPL TYPE OLE2_OBJECT, " list of workbooks

H_MAP TYPE OLE2_OBJECT, " workbook

H_ZL TYPE OLE2_OBJECT, " cell

H_F TYPE OLE2_OBJECT. " font

TABLES: SPFLI.

DATA H TYPE I.

  • table of flights

DATA: IT_SPFLI LIKE SPFLI OCCURS 10 WITH HEADER LINE.

&----


*& Event START-OF-SELECTION

&----


START-OF-SELECTION.

  • read flights

SELECT * FROM SPFLI INTO TABLE IT_SPFLI UP TO 10 ROWS.

  • display header

ULINE (61).

WRITE: / SY-VLINE NO-GAP,

(3) 'Flg'(001) COLOR COL_HEADING NO-GAP, SY-VLINE NO-GAP,

(4) 'Nr'(002) COLOR COL_HEADING NO-GAP, SY-VLINE NO-GAP,

(20) 'Von'(003) COLOR COL_HEADING NO-GAP, SY-VLINE NO-GAP,

(20) 'Nach'(004) COLOR COL_HEADING NO-GAP, SY-VLINE NO-GAP,

(8) 'Zeit'(005) COLOR COL_HEADING NO-GAP, SY-VLINE NO-GAP.

ULINE /(61).

  • display flights

LOOP AT IT_SPFLI.

WRITE: / SY-VLINE NO-GAP,

IT_SPFLI-CARRID COLOR COL_KEY NO-GAP, SY-VLINE NO-GAP,

IT_SPFLI-CONNID COLOR COL_NORMAL NO-GAP, SY-VLINE NO-GAP,

IT_SPFLI-CITYFROM COLOR COL_NORMAL NO-GAP, SY-VLINE NO-GAP,

IT_SPFLI-CITYTO COLOR COL_NORMAL NO-GAP, SY-VLINE NO-GAP,

IT_SPFLI-DEPTIME COLOR COL_NORMAL NO-GAP, SY-VLINE NO-GAP.

ENDLOOP.

ULINE /(61).

  • tell user what is going on

CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'

EXPORTING

  • PERCENTAGE = 0

TEXT = TEXT-007

EXCEPTIONS

OTHERS = 1.

  • start Excel

CREATE OBJECT H_EXCEL 'EXCEL.APPLICATION'.

  • PERFORM ERR_HDL.

SET PROPERTY OF H_EXCEL 'Visible' = 1.

  • CALL METHOD OF H_EXCEL 'FILESAVEAS' EXPORTING #1 = 'c:\kis_excel.xls'

.

  • PERFORM ERR_HDL.

  • tell user what is going on

CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'

EXPORTING

  • PERCENTAGE = 0

TEXT = TEXT-008

EXCEPTIONS

OTHERS = 1.

  • get list of workbooks, initially empty

CALL METHOD OF H_EXCEL 'Workbooks' = H_MAPL.

PERFORM ERR_HDL.

  • add a new workbook

CALL METHOD OF H_MAPL 'Add' = H_MAP.

PERFORM ERR_HDL.

  • tell user what is going on

CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'

EXPORTING

  • PERCENTAGE = 0

TEXT = TEXT-009

EXCEPTIONS

OTHERS = 1.

  • output column headings to active Excel sheet

PERFORM FILL_CELL USING 1 1 1 'Flug'(001).

PERFORM FILL_CELL USING 1 2 0 'Nr'(002).

PERFORM FILL_CELL USING 1 3 1 'Von'(003).

PERFORM FILL_CELL USING 1 4 1 'Nach'(004).

PERFORM FILL_CELL USING 1 5 1 'Zeit'(005).

LOOP AT IT_SPFLI.

  • copy flights to active EXCEL sheet

H = SY-TABIX + 1.

PERFORM FILL_CELL USING H 1 0 IT_SPFLI-CARRID.

PERFORM FILL_CELL USING H 2 0 IT_SPFLI-CONNID.

PERFORM FILL_CELL USING H 3 0 IT_SPFLI-CITYFROM.

PERFORM FILL_CELL USING H 4 0 IT_SPFLI-CITYTO.

PERFORM FILL_CELL USING H 5 0 IT_SPFLI-DEPTIME.

ENDLOOP.

  • changes by Kishore - start

  • CALL METHOD OF H_EXCEL 'Workbooks' = H_MAPL.

CALL METHOD OF H_EXCEL 'Worksheets' = H_MAPL." EXPORTING #1 = 2.

PERFORM ERR_HDL.

  • add a new workbook

CALL METHOD OF H_MAPL 'Add' = H_MAP EXPORTING #1 = 2.

PERFORM ERR_HDL.

  • tell user what is going on

SET PROPERTY OF H_MAP 'NAME' = 'COPY'.

CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'

EXPORTING

  • PERCENTAGE = 0

TEXT = TEXT-009

EXCEPTIONS

OTHERS = 1.

  • output column headings to active Excel sheet

PERFORM FILL_CELL USING 1 1 1 'Flug'(001).

PERFORM FILL_CELL USING 1 2 0 'Nr'(002).

PERFORM FILL_CELL USING 1 3 1 'Von'(003).

PERFORM FILL_CELL USING 1 4 1 'Nach'(004).

PERFORM FILL_CELL USING 1 5 1 'Zeit'(005).

LOOP AT IT_SPFLI.

  • copy flights to active EXCEL sheet

H = SY-TABIX + 1.

PERFORM FILL_CELL USING H 1 0 IT_SPFLI-CARRID.

PERFORM FILL_CELL USING H 2 0 IT_SPFLI-CONNID.

PERFORM FILL_CELL USING H 3 0 IT_SPFLI-CITYFROM.

PERFORM FILL_CELL USING H 4 0 IT_SPFLI-CITYTO.

PERFORM FILL_CELL USING H 5 0 IT_SPFLI-DEPTIME.

ENDLOOP.

  • changes by Kishore - end

  • disconnect from Excel

  • CALL METHOD OF H_EXCEL 'FILESAVEAS' EXPORTING #1 = 'C:\SKV.XLS'.

FREE OBJECT H_EXCEL.

PERFORM ERR_HDL.

----


  • FORM FILL_CELL *

----


  • sets cell at coordinates i,j to value val boldtype bold *

----


FORM FILL_CELL USING I J BOLD VAL.

CALL METHOD OF H_EXCEL 'Cells' = H_ZL EXPORTING #1 = I #2 = J.

PERFORM ERR_HDL.

SET PROPERTY OF H_ZL 'Value' = VAL .

PERFORM ERR_HDL.

GET PROPERTY OF H_ZL 'Font' = H_F.

PERFORM ERR_HDL.

SET PROPERTY OF H_F 'Bold' = BOLD .

PERFORM ERR_HDL.

ENDFORM.

&----


*& Form ERR_HDL

&----


  • outputs OLE error if any *

----


  • --> p1 text

  • <-- p2 text

----


FORM ERR_HDL.

IF SY-SUBRC <> 0.

WRITE: / 'Fehler bei OLE-Automation:'(010), SY-SUBRC.

STOP.

ENDIF.

ENDFORM. " ERR_HDL

Please note that this example maybe slow at filling the excel table

(perhaps four fields per second on a 900 MHz machine - almost 30 seconds

for a short example).

To get the data on properties and methods - there is a bit of smoke and mirrors

going on here; they are EXCEL properties and methods, not sap ones - so you need

to look at excel help to determine how a particular function is structured. then

build the block in sap, as shown in the example.

If you only want to transfer the data to Excel like when you transfer the data from

ALV to Excel simply use the Function Modules:

XXL_SIMPLE_API

If you want more modifications when you transfer it to Excel use:

XXL_FULL_API

Former Member
0 Kudos

The problem origined from the fieldcatalog. Thanks for the advices.