Excel Upload...

SAP中Excel文件上传程序
本文介绍了一个用于SAP系统的ABAP程序,该程序能够从Excel文件中读取数据并将其导入到SAP表中。程序通过定义的参数设置读取范围,并使用内置函数处理Excel文件的数据结构。

*-----------------------------------------------------------------------
*
* CompanyXXX Company Limited
*
* ProgrameName: ZZW_EXCEL_UPLOAD
*
* Author: Wei_Zhu
*
* Date: 07/31/2007
*
* Description: Study SAP In Program How To Upload Excel File To SAP
* Table.
*
*
*-----------------------------------------------------------------------
REPORT ZZW_EXCEL_UPLOAD MESSAGE-ID 00.

Tables: ZTBTEST.

*Data: I_Tab_1 like standard table of alsmex_tabline with header line,
* I_Tab_2 like alsmex_tabline.

*----------Excel Paraters Setting-----------
*Data: Begin_Col Type i value 1,
* Begin_Row Type i value 1,
* End_Col Type i value 128,
* End_Row Type i value 65535.

Data: Begin Of I_tab_1 Occurs 0.
INCLUDE Structure alsmex_tabline.
Data: End Of I_tab_1.

Data: Begin Of I_tab_2 Occurs 0.
INCLUDE Structure alsmex_tabline.
Data: End Of I_tab_2.

Data: Begin Of T_Col Occurs 0,
Col Like alsmex_tabline-col,
Size Type i,
End Of T_Col.

Data: Zwlen Type i,
Zwlines Type i.

DATA: tind(4) TYPE n.
DATA: zwfeld(19).

* No of columns
DATA: BEGIN OF data_tab OCCURS 0,
value_0001(50),
value_0002(50),
value_0003(50),
value_0004(50),
value_0005(50),
value_0006(50),
value_0007(50),
value_0008(50),
value_0009(50),
value_0010(50),
value_0011(50),
value_0012(50),
value_0013(50),
value_0014(50),
value_0015(50),
value_0016(50),
value_0017(50),
value_0018(50),
value_0019(50),
value_0020(50),
value_0021(50),
value_0022(50),
value_0023(50),
value_0024(50),
value_0025(50),
value_0026(50),
value_0027(50),
value_0028(50),
value_0029(50),
value_0030(50),
value_0031(50),
value_0032(50),
value_0033(50),
value_0034(50),
value_0035(50),
value_0036(50),
value_0037(50),
value_0038(50),
value_0039(50),
value_0040(50),
value_0041(50),
value_0042(50),
value_0043(50),
value_0044(50),
value_0045(50),
value_0046(50),
value_0047(50),
value_0048(50),
value_0049(50),
value_0050(50),
value_0051(50),
value_0052(50),
value_0053(50),
value_0054(50),
value_0055(50),
value_0056(50),
value_0057(50),
value_0058(50),
value_0059(50),
value_0060(50),
value_0061(50),
value_0062(50),
value_0063(50),
value_0064(50),
value_0065(50),
value_0066(50),
value_0067(50),
value_0068(50),
value_0069(50),
value_0070(50),
value_0071(50),
value_0072(50),
value_0073(50),
value_0074(50),
value_0075(50),
value_0076(50),
value_0077(50),
value_0078(50),
value_0079(50),
value_0080(50),
value_0081(50),
value_0082(50),
value_0083(50),
value_0084(50),
value_0085(50),
value_0086(50),
value_0087(50),
value_0088(50),
value_0089(50),
value_0090(50),
value_0091(50),
value_0092(50),
value_0093(50),
value_0094(50),
value_0095(50),
value_0096(50),
value_0097(50),
value_0098(50),
value_0099(50),
value_0100(50).
DATA: END OF data_tab.


Data: Begin Of FieldNames Occurs 3,
Title(60),
Table(6),
Field(10),
KZ(1),
End Of FieldNames.

Data: Begin OF Itab Occurs 0,
ID Like ZTBTEST-Z_USER_ID,
Name Like ZTBTEST-Z_USER_NAME,
Text Like ZTBTEST-Z_TEXT,
End Of Itab.

*-----------------------------------------------------------------------
* Screen
*-----------------------------------------------------------------------
Selection-Screen Begin Of Block B1 With Frame Title Text-001.
Parameters: P_fname like rlgrap-filename modif ID m3.
Selection-Screen End Of Block B1.

Selection-Screen Begin Of Block B2 With Frame Title Text-002.
Parameters: Kzheader as CheckBox. " Tick Don't Append Header
Selection-Screen End Of Block B2.

Selection-Screen Begin Of Block B3 With Frame Title Text-003.
Parameters: B_Col Type I,
B_Row Type I,
E_Col Type I,
E_Row Type I.
Selection-Screen End Of Block B3.

*-----------------------------------------------------------------------
*Initialization
*-----------------------------------------------------------------------
Initialization.

*----------------------------------------------------------------------
* Process Control
*----------------------------------------------------------------------
At Selection-Screen On Value-Request For P_fname.
Perform Fname_Value_F4.

At Selection-Screen.
If B_Col is initial.
Message e888(sabapdocu)
With '[ Begin Column ] Must Be Input Parameter!'.
EndIf.

If B_ROW Is Initial.
Message e888(sabapdocu)
With '[ Begin Row ] Must Be Input Parameter!'.
EndIf.

If E_Col is Initial.
Message e888(sabapdocu)
With '[ End Column ] Must Be Input Parameter!'.
EndIf.

If E_Row is Initial.
Message e888(sabapdocu)
With '[ End Row ] Must Be Input Parameter!'.
EndIf.

*----------------------------------------------------------------------
* Statr-Of-Selection
*----------------------------------------------------------------------
Start-Of-Selection.
Perform Get_Excel_Data.

*&---------------------------------------------------------------------
*
*& Form Fname_Value_F4
*&---------------------------------------------------------------------
*
* text
*----------------------------------------------------------------------
*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------
*
form Fname_Value_F4 .
Call Function 'KD_GET_FILENAME_ON_F4'
Exporting
Mask = ',Microsoft Excel Files,*.xls'
Static = 'X'
Changing
File_Name = P_fname.
endform. " Fname_Value_F4
*&---------------------------------------------------------------------*
*& Form Get_Excel_Data
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form Get_Excel_Data .
Call Function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
Exporting
filename = p_fname
i_begin_col = B_Col
i_begin_row = B_Row
i_end_col = E_Col
i_end_row = E_Row
Tables
intern = i_tab_1
Exceptions
inconsistent = 1
upload_old = 2
others = 3.

If Sy-subrc <> 0.
Write: / 'Upload Error', Sy-subrc.
* Message e888(sabapdocu) with 'xxxxx'.
EndIf.

Loop at i_tab_1.
i_tab_2 = i_tab_1.
Clear i_tab_2-row.
Append i_tab_2.
EndLoop.

Sort i_tab_2 by col.

Loop At i_tab_2.
At new Col.
T_Col-Col = i_tab_2-col.
Append t_col.
EndAt.
Zwlen = Strlen( I_tab_2-Value ).
Read Table T_Col With Key Col = I_tab_2-Col.
If Sy-SubRc EQ 0.
If Zwlen > T_Col-Size.
T_Col-Size = Zwlen.
Modify T_Col Index Sy-tabix.
EndIF.
EndIF.
EndLoop.

Describe Table T_Col Lines Zwlines.

Sort I_Tab_1 By Row Col.
IF KZheader = 'X'.
Loop at I_Tab_1.
FieldNames-Title = I_tab_1-Value.
Append FieldNames.

At End Of Row.
Exit.
EndAT.
EndLoop.
Else.
Do Zwlines Times.
Write Sy-Index to FieldNames-Title.
Append Fieldnames.
EndDo.
EndIF.

Field-Symbols <fs>.

Sort I_Tab_1 BY Row Col.
Loop At I_tab_1.
If Kzheader = 'X'
And I_tab_1-Row = 1.
Continue.
EndIf.
Tind = I_tab_1-Col.

Concatenate 'Data_Tab-Value_' Tind Into zwfeld.
Assign (zwfeld) To <fs>.
<fs> = I_tab_1-Value.
At End Of Row.
Append data_tab.
Clear Data_tab.
EndAT.
EndLoop.

Call Function 'DISPLAY_BASIC_LIST'
Exporting
File_Name = p_fname
Tables
Data_tab = Data_tab
FieldName_Tab = FieldNames.


* Loop at Data_tab.
* If Sy-tabix EQ 1.
** Skip.
* Continue.
* Else.
* Itab-ID = Data_Tab-Value_0001(10).
* Itab-Name = Data_Tab-Value_0002(10).
* Itab-Text = Data_Tab-Value_0003(10).
* Append Itab.
* EndIF.
*
* EndLoop.
*
* Modify ZTBTEST From Table Itab.


endform. " Get_Excel_Data

### Layui 框架中 Excel.js 的 API 使用说明 #### 1. Layui-excel 插件概述 Layui-excel 是一个基于 Layui 框架的轻量级前端 Excel 数据处理插件,主要用于实现表格数据的导入、导出和在线预览功能[^3]。以下是其主要功能和特点: - **导入 Excel**:支持通过文件选择器或 Base64 编码的 Excel 文件导入,并解析为网页上的表格数据。 - **导出 Excel**:可以将网页上的表格数据转换为 Excel 文件供用户下载。 - **在线预览**:用户上传的 Excel 文件可以直接在页面上以表格形式展示。 - **兼容性**:支持主流现代浏览器,包括 Chrome、Firefox、Safari 和 Edge。 #### 2. Excel.js 的核心功能 Excel.js 是 Layui-excel 的核心组件之一,提供了丰富的 API 来操作 Excel 文件。以下是一些常用的功能和方法: ##### 2.1 导出 Excel 表格 通过 `excel.exportTable` 方法可以将网页上的表格数据导出为 Excel 文件。以下是一个示例代码: ```javascript layui.use(['excel'], function() { const excel = layui.excel; // 获取表格数据 const tableData = [ ['ID', 'Name', 'Age'], [1, 'Alice', 25], [2, 'Bob', 30] ]; // 导出表格 excel.exportTable({ sheet1: tableData, filename: 'example_export.xlsx' }); }); ``` ##### 2.2 导入 Excel 表格 使用 `excel.importExcel` 方法可以从用户上传的 Excel 文件中读取数据。以下是一个示例代码: ```javascript layui.use(['excel'], function() { const excel = layui.excel; // 处理文件选择器 document.getElementById('upload').addEventListener('change', function(e) { const file = e.target.files[0]; if (file) { excel.importExcel(file, function(data) { console.log('解析后的数据:', data); }, function(err) { console.error('解析失败:', err); }); } }); }); ``` ##### 2.3 在线预览 Excel 文件 通过 `excel.preview` 方法可以实现在页面上实时预览 Excel 文件内容。以下是一个示例代码: ```javascript layui.use(['excel'], function() { const excel = layui.excel; // 处理文件选择器 document.getElementById('preview').addEventListener('change', function(e) { const file = e.target.files[0]; if (file) { excel.preview(file, function(data) { console.log('预览的数据:', data); }, function(err) { console.error('预览失败:', err); }); } }); }); ``` #### 3. Excel.js 的样式自定义 Layui-excel 支持对导出的 Excel 文件进行样式设置,例如字体、颜色、对齐方式等。以下是一个示例代码,展示如何设置单元格样式: ```javascript layui.use(['excel'], function() { const excel = layui.excel; // 设置表格数据和样式 const tableData = [ { content: 'ID', style: { font: { bold: true }, alignment: { horizontal: 'center' } } }, { content: 'Name', style: { font: { bold: true }, alignment: { horizontal: 'center' } } }, { content: 'Age', style: { font: { bold: true }, alignment: { horizontal: 'center' } } } ]; // 导出表格 excel.exportTable({ sheet1: tableData, filename: 'styled_export.xlsx', styles: { font: { name: 'Arial', size: 12 }, alignment: { wrapText: true } } }); }); ``` #### 4. 常见问题与解决方法 - **问题 1**:导出大容量数据时性能下降。 - **解决方法**:优化数据结构,减少不必要的计算;或者分批次导出数据[^1]。 - **问题 2**:文件名包含特殊字符导致无法保存。 - **解决方法**:对文件名进行过滤,移除所有非英文字符。 - **问题 3**:Excel 文件格式不兼容。 - **解决方法**:确保使用正确的库版本,并检查浏览器兼容性[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值