To reorganize a dataset when it exports Excel

Source: Internet
Author: User
Tags final sorted by name first row

A while ago, a classmate asked me to analyze two data and a section of Dephi code, he himself has initially implemented the export from the dataset to Excel, but did not implement the line break and the occurrence of the other columns except the first column are missing a row of records. So hopefully I can help. An algorithm that imports multiple product encodings from one production list into the same Excel. The initial data (in the dataset) is generally as follows:

订单编号     名称          属性编码  属性值
0000001     A基础信息     功能     单功能
0000001     A基础信息     外观      WG
0000001     B结构技术要求   内部颜色  白色
0000001     B结构技术要求   外部颜色  红色
0000001     C其他技术要求   分级     A类
0000001     D参数信息      X       100
0000001     D参数         Y       200

Final effect data (shown in Excel):

A基础信息     B结构技术要求   C其他技术要求
功能:单功能    内部颜色:白色  分级:A类
外观:WG      外部颜色:红色

D parameter Information

X:100
Y:200

According to the initial diagram and the final effect, Excel wraps every 3 columns and implements the property codes and property values for different names. The source code is as follows:

Trow: = 3; ----is actually implemented from the third row because of the Excel header information, which holds the name
IRow: =trow+1; ---Set the starting line for initial property information
Max: =irow; ---Gets the maximum value of the attribute information in the peer name
Icol: = 1; -----columns
Cdsviewcpdconfiginfl.first; ---to move the recordset to the first row, where the recordset is sorted by name
STRCPD: =cdsviewcpdconfiginfl.fieldbyname (' name '). asstring; ---Gets the name value of the current Recordset and holds it in the variable so that later comparisons are categorized
While does cdsviewcpdconfiginfl.eof do
Begin
Write row data under this group with the same group name
If Cdsviewcpdconfiginfl.fieldbyname (name). asstring = STRCPD Then
Begin
Eclapp2. Worksheets[i].cells[trow, Icol].value: = Cdsviewcpdconfiginfl.fieldbyname (name). asstring//write group name
Eclapp2. Worksheets[i].cells[irow, Icol].value: = Cdsviewcpdconfiginfl.fieldbyname (property encoding). Asstring +
': ' + cdsviewcpdconfiginfl.fieldbyname (property value). asstring;
INC (IRow);
End
else//Group swap
Begin
If Irow>max Then
Max: =irow+1; ---Get the maximum row value
If Icol>3 then----the value of the row, when column 4, wrap
Begin
Trow: =max; Gets the first row of the most recent group name----line Change
Icol: = 0; ----also set the column to 0
End
IRow: =trow+1; ----Set the starting line for the new group Name property information
Eclapp2. Worksheets[i].cells[trow, Icol+1].value: = Cdsviewcpdconfiginfl.fieldbyname (' groupname '). asstring;//write Group name
Eclapp2. Worksheets[i].cells[irow, Icol+1].value: = Cdsviewcpdconfiginfl.fieldbyname (' Attr_number '). Asstring +
': ' + cdsviewcpdconfiginfl.fieldbyname (' Attr_value '). asstring; Write attribute name and attribute value
INC (IRow); ----Row number Overlay
INC (Icol); -----Number of columns superimposed
End;
Cdsviewcpdconfiginfl.next;
End

Note: When changing columns, the first reason to simply think that only the number of columns to stack, so that all except the first column has a line of property information. INC (IRow) is required, and when the column reaches 3, the Icol is set to 0, taking into account that Icol is added when the row is not changed.

Attached here Dephi and VB prepared the main source code, see annex. I think the following code may need to be improved, please give comments or suggestions

This article supporting source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.