方法1:利用cl_salv_export_tool_xls 类实现导出

ALV 数据导出至 Excel,其实就是将 ALV 对应的内表数据导出至 Excel。大家常见的 GUI_DOWNLOAD 函数导出是将内表导出为文本文件,所以每次打开的时候提示文件格式不相符错误。本文提供基于 cl_salv_export_tool_xls 类实现导出的方法。为了方便调用,我将导出功能写在一个子例程中:

form frm_export_excel using p_itab type standard table.data: file_length type i.data: lt_stream   type salv_xml_xline_tabtype.data: file_name   type string.data: xls_export_tool  type ref to   cl_salv_export_tool_xls,export_config    type ref to   if_salv_export_configuration,file_content     type          cl_salv_export_tool=>y_file_content,export_exception type ref to   cx_salv_export_error." Create an instance of the Excel export toolget reference of p_itab into gr_data.xls_export_tool = cl_salv_export_tool=>create_for_excel( gr_data )." Configure export propertiesexport_config = xls_export_tool->configuration( )." Populate headerloop at gt_fieldcat.export_config->add_column( header_text  = |{ gt_fieldcat-coltext }|field_name   = |{ gt_fieldcat-fieldname }|display_type  = if_salv_export_column_conf=>display_types-text_view ).clear gt_fieldcat.endloop." exports R_DATA to requested formattry.xls_export_tool->read_result(importing content = file_content ).catch cx_salv_export_error into export_exception.message id export_exception->if_t100_message~t100key-msgidtype 'E'number export_exception->if_t100_message~t100key-msgno .endtry." Set Filenamefile_name = 'D:\Downloads\spfli.xlsx'." Convert to Binarycall function 'SCMS_XSTRING_TO_BINARY'exportingbuffer        = file_contentimportingoutput_length = file_lengthtablesbinary_tab    = lt_stream." Download file using binary formatcl_gui_frontend_services=>gui_download(exportingbin_filesize = file_lengthfiletype     = 'BIN'filename     = file_namechangingdata_tab     = lt_streamexceptionsfile_write_error        = 1no_batch                = 2gui_refuse_filetransfer = 3invalid_type            = 4no_authority            = 5unknown_error           = 6header_not_allowed      = 7separator_not_allowed   = 8filesize_not_allowed    = 9header_too_long         = 10dp_error_create         = 11dp_error_send           = 12dp_error_write          = 13unknown_dp_error        = 14access_denied           = 15dp_out_of_memory        = 16disk_full               = 17dp_timeout              = 18file_not_found          = 19dataprovider_exception  = 20control_flush_error     = 21not_supported_by_gui    = 22error_no_gui            = 23others                  = 24 ).if sy-subrc <> 0.message 'Fail to download the file.' type 'E'.else.message 'Download the file successfully.' type 'S'.endif.
endform.

完整的代码放在源码中,请自行参考。

方法二:利用 XXL_SIMPLE_API 函数

下面利用 xxl_simple_api 函数,实现通用的将内表导出到 Excel 功能。

创建一个新的函数,名为 zitab_to_excel, 在函数组中,编写一个 form 获取内表所有的字段:

form frm_get_fields using    pt_data     type any tablechanging pt_fields   type ddfields.data: lr_tabdescr  type ref to cl_abap_structdescr,lr_data      type ref to data,lt_fields    type ddfields.create data lr_data like line of pt_data.lr_tabdescr ?= cl_abap_structdescr=>describe_by_data_ref( lr_data ).lt_fields    = cl_salv_data_descr=>read_structdescr( lr_tabdescr ).pt_fields = lt_fields.
endform.

利用 XXL_SIMPLE_API 函数实现内表导出:

function zitab_to_excel.
*"--------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(INTERNAL_TAB) TYPE  ANY TABLE
*"  EXCEPTIONS
*"      DIM_MISMATCH_DATA
*"      FILE_OPEN_ERROR
*"      FILE_WRITE_ERROR
*"      INV_WINSYS
*"      INV_XXL
*"--------------------------------------------------------------------data: lt_ddfields type ddfields,ls_fields   type dfies.perform frm_get_fields using internal_tab[] changing lt_ddfields[].field-symbols <fs> type standard table.data: t_heading type table of gxxlt_v with header line,t_online  type table of gxxlt_o,t_print   type table of gxxlt_p.loop at lt_ddfields into ls_fields.t_heading-col_no   = sy-tabix.t_heading-col_name = ls_fields-scrtext_m.append t_heading.endloop.assign internal_tab to <fs>.call function 'XXL_SIMPLE_API'tablescol_text          = t_heading " heading, column textdata              = <fs>online_text       = t_onlineprint_text        = t_printexceptionsdim_mismatch_data = 1file_open_error   = 2file_write_error  = 3inv_winsys        = 4inv_xxl           = 5others            = 6.case sy-subrc.when 1. raise dim_mismatch_data.when 2. raise file_open_error.when 3. raise file_write_error.when 4. raise inv_winsys.when 5. raise inv_xxl.endcase.
endfunction.

调用示例:

form frm_user_command using p_ucomm    type sy-ucomm        " user commandp_selfield type slis_selfield.  " select fieldcase p_ucomm.when 'EXCEL'.call function 'ZITAB_TO_EXCEL'exportinginternal_tab = gt_spfli[].endcase.
endform.                    "user_command

源码

06-Download ALV to Excel

Functional ALV系列 (06) - 数据导出至Excel相关推荐

  1. 将C1Chart数据导出到Excel

    大多数情况下,当我们说将图表导出到Excel时,意思是将Chart当成图片导出到Excel中.如果是这样,你可以参考帮助文档中保存和导出C1Chart章节. 不过,也有另一种情况,当你想把图表中的数据 ...

  2. 轉:VB6中将数据导出到Excel提速之法

    from : http://www.it86.cc/develop/2008/0410/28928.shtml Excel 是一个非常优秀的报表制作软件,用VBA可以控制其生成优秀的报表,本文通过添加 ...

  3. excel文件导入hive乱码_把数据库数据导出到excel

    SQL Server BI Step by Step 2--- 使用SSIS进行简单的数据导入导出 让我们首先开始学习SSIS吧,利用SSIS把SQL Server中的数据导出.首先,打开Vs.net ...

  4. 阿里开源(EasyExcel):使用Java将数据导出为Excel表格、带样式----》java web下载 Excel文件

    目录 一.技术选型 二.实现过程 1.导入依赖 2.编写工具类 EasyExcelUtil 3.公用参数类 EasyExcelParams 4.表格样式实体类 MyWriteHandler 5.数据实 ...

  5. 百度指数常见php框架,怎么导出数据到excel表格-如何将百度指数数据导出到Excel表格...

    如何将百度指数数据导出到Excel表格 第一步:打开CAD.CAD命令行输入"Li"."选择对象"选需要提取坐标的多段线.回车. 第二步:将CAD文本框中的数据 ...

  6. php怎么将表格导出到excel表格,php怎么将excel表格数据-php 怎么把数据导出到excel表格...

    如何使用php实现将数据从excel表导入到mysql中? /** * @param array $data //二维数组 不是对象 * @param string $filename //导件 */ ...

  7. python结果输出到excel-python实现数据导出到excel的示例--普通格式

    此文是在django框架下编写,从数据库中获取数据使用的是django-orm 用python导出数据到excel,简单到爆!(普通的excel格式) 安装xlwt pip install xlwt ...

  8. python处理大量excel数据-使用python将大量数据导出到Excel中的小技巧分享

    (1) 问题描述:为了更好地展示数据,Excel格式的数据文件往往比文本文件更具有优势,但是具体到python中,该如何导出数据到Excel呢?如果碰到需要导出大量数据又该如何操作呢? 本文主要解决以 ...

  9. python输出数据到excel-python实现数据导出到excel的示例

    这篇文章主要介绍了关于python实现数据导出到excel的示例,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下 此文是在django框架下编写,从数据库中获取数据使用的是django- ...

最新文章

  1. 如何破解压缩文件密码-省时省力的方法
  2. 解决naigos+pnp4nagios部分不出图的问题
  3. C# WINFORM ListView用法详解(转)
  4. 一些琐碎+分不清的知识点
  5. 2017年10月08日普及组 蜡烛
  6. [译] RNN 循环神经网络系列 2:文本分类
  7. 三道题就能考察你对Vue掌握了多少!
  8. 【NOI2002】银河英雄传说
  9. BeanFactory not initialized or already closed - call 'refresh' before access
  10. 技术开发人员需要改变性格吗?
  11. MATLAB中滤波演示
  12. 富士通Fujitsu DPK1180K 打印机驱动
  13. 我家的电视机会“自修”
  14. 开博尔智能android播放器,高端安卓播放器的选择——开博尔Q10Plus 二代 4K高清播放器...
  15. 天津师范大学计算机与信息工程学院研究生院,天津师范大学计算机与信息工程学院2020考研调剂信息...
  16. 工程流体力学笔记暂记42 (收缩喷管中的流动)
  17. 二、rally使用指导
  18. Oracle 19c VLDB and Partitioning Guide 第4章:分区管理 读书笔记
  19. Google Chrome即将开始警告—停止支持Flash Player
  20. 了解maven无法使用Scaner 找不到符号异常 c3p0 聚合 依赖 pom插入编译版本 锁定版本 无法输出结果 乱码 plugins报红 为mave项目配置创参数 Test命令 一些错误和经验

热门文章

  1. [立体匹配与深度估计]评估网站与评估方法
  2. 家用监控百科:什么是4K摄像机,4K摄像模组是什么意思
  3. windows更改盘符的方法
  4. python-datetime计算时间间隔
  5. 查看dell笔记本电池运行记录
  6. Html.RenderPartial的三个参数的用法 用法实例
  7. 终于知道如何 把MATLAB中的 程序字体变大了!
  8. maya动画制作(2)——龙的各种姿态动画制作(修改更新)
  9. 一封奇怪的信---网易游戏(互娱)-游戏测试开发工程师真题 题解
  10. Spring Boot + Solr