方法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. 每日一皮:自己运行正常,测试一测就有bug
  2. lamp/lnmp实例
  3. public ServiceException() { super(); } public ServiceException(String message, Throwable cause,
  4. Bootstrap系列 -- 41. 带表单的导航条
  5. npm ERR! Failed at the node-sass@4.13.0 postinstall script.
  6. 经典知识:交互设计规范8大总结!附84页资料:《J 东交互设计规范》
  7. 中国计算机学会推荐的国际学术刊物
  8. 详解python使用browsermobproxy获取当前网页xhr的get数据方法
  9. 如何恢复计算机永久删除文件,怎么恢复被彻底删除的文件?简单有效方法分享...
  10. 论基因检测的必要性,主动把握健康。
  11. dB、dBm、dBw的含义和转化关系
  12. IMDb站点起诉加州限制演员年龄信息披露法案的有效性
  13. Docker硬盘空间使用分析与清理
  14. 辛弃疾《青玉案·元夕》
  15. 基于二极管的温度检测电路
  16. windows10下用PowerShell命令(Get-FileHash)校验文件的Hash值(MD5、SHA1、SHA256等)
  17. matlab曲面的最小值,MATLAB中标准三维曲面
  18. 虚拟桌面分屏_Windows 分屏和虚拟桌面,也能很实用
  19. linux安装make
  20. 在Mac上为其他设备开启代理

热门文章

  1. django产生错误,Unknown field(s) () specified for StoreEntity. Check fields/fieldsets/exclude attributes
  2. WEBP图片怎么打开?如何转成JPEG
  3. iOS学习笔记(1)-iPhone分辨率
  4. Python编程从入门到实践笔记(超详细的精华讲解+内有2021最新版本代码)
  5. 阿里云自主研发云原生数据库POLARDB的开拓之路
  6. 黑客侵入电视台微博谎称奥巴马遭枪杀
  7. 如何查看 GitLab 版本号
  8. 如何成长为优秀的架构师?架构师成长的4大必经之路
  9. 三维建筑动画制作流程
  10. Error while building/deploying project untitled(永久解决办法)