Generate Excel file examples in various formats

In data processing, sometimes it is necessary to use the program to automatically generate Excel files in various formats from the structured data obtained from various channels such as query, analysis and calculation, statistics, and data sampling.

This article will introduce the implementation methods for simple export of data, export of large amounts of data, specified display attributes, fixed row and column filling, dynamic condition display attributes, grouping band details and statistics, cross-statistics, etc., and provide code examples written in esProc SPL. esProc is a professional data calculation engine. There are perfect Excel file export functions in SPL. It is very convenient to export structured data to Excel. This article does not discuss how the data to be exported is obtained, assuming it already exists in a text file.

 

1. Simply export data

1.1 Export new file

Export the column names to the first row of the Excel file, and the second row starts to export data records, one record per row.

Example: Export the order form data to a new Excel file orders.xlsx, the esProc SPL script is as follows:


A Comment
1 =file("e:/txt/orders.txt":"UTF-8").import@t()   Read in text data
2 =file("e:/excel/orders.xlsx").xlsexport@t(A1)   Export the Excel file, @t means export the column name to the first row

If the file does not exist when exporting, it will be created automatically. In the parameter of the function xlsexport, you can specify the columns that need to be exported. If you don't specify it in this example, you can export all the columns; you can also specify the sheet name of the exported data. If you don't specify it in this example, you can export it to the first sheet.

Part of the exported orders.xlsx data is shown in the figure below:

..

 

1.2 Additional data

The Excel file already exists, and the new data should be appended to the existing data. At this time, you only need to add data records, no need to export column names.

Example: Append today’s newly added order table data to the original orders.xlsx, the esProc SPL script is as follows:


A Comment
1 =file("e:/txt/aday.txt":"UTF-8").import@t()   Read in text data
2 =file("e:/excel/orders.xlsx").xlsexport@a(A1)   @a means append data to the end of the original file

 

1.3 Export to the specified Sheet

When exporting data, specify the name of the sheet where the data is saved.

Example: Export the order sheet data from Shantai company to the Sheet named Shantai in the orders.xlsx file. In this example, only the specified column data is exported and some column names are modified. The esProc SPL script is as follows:


A Comment
1 =file("e:/txt/orders.txt":"UTF-8").import@t()   Read in text data
2 =A1.select(Company==“Shantai")   Filter out Shantai company data
3 =file(“e:/excel/orders.xlsx”).xlsexport@t(A2,ID,Company,OrderDate:Date,Amount:Money;”Shantai")   Export the four columns of ID, Company, OrderDate, and Amount, and rename OrderDate to Date, Amount to Money, and export the data to a sheet named Shantai

Part of the exported orders.xlsx data is shown in the figure below:

..

 

2. Export large amounts of data

When there is a large amount of data that cannot be loaded into the memory at one time to be exported, the cursor should be used to read in the data in batches, and then the data should be written into an Excel file in OpenXml format (that is, the extension is xlsx) in a stream. Need to occupy a small amount of memory. Excel limits a sheet to store up to 1048576 rows of data, so when the number of exported data rows exceeds this limit, we need to add a new sheet to save.

Example: Export the big data order table to the big.xlsx file, the esProc SPL script is as follows:


A Comment
1 =file("e:/txt/big.txt":"UTF-8").cursor@t()   Use cursor to read text data
2 =file("e:/excel/big.xlsx").xlsexport@st(A1)   @s means export with streaming

The last few rows of the exported big.xlsx data are shown in the figure below:

..

In this example, 123663 data records are exported. In this way, any record can be exported (but ensure that the hard disk storage space is sufficient).

 

3. Specify display attributes

In addition to exporting data directly, sometimes you also want the generated excel file to be displayed more beautifully, for example, you can specify the font, color, background color, alignment, and display format. At this time, you need to build the excel file (template) in advance, define the required display attributes, and then export the data to this file, and the defined display attributes will be presented accordingly.

Example: Import the order form data into orders.xlsx and display it in the format shown in the following figure:

..

In this example, a variety of predefined display attributes are gathered together for demonstration, and the display is rather messy. It is only for the purpose of demonstrating the function and is not designed from the aesthetic point of view. The esProc SPL script is the same as in section 1.1.

The exported Excel file is shown below:

..

 

4. Fixed row and column fill

There is an Excel file with fixed number of rows and columns and cell format. There are some empty cells in it. You need to fill in the relevant data in these grids. This requires the function of filling in data to a single cell. It is often used in data reporting services.

Example: The head office of a fund company issued an excel form to the branch, asking the branch to fill in its relevant data and then send it back to the head office. The issued excel file is as follows:

..

The esProc SPL script is as follows:


A B C D E F
1 Mengniu Funds 2017 3 58.2 364 300
2 8.5 50 200 100 400 200
3 182.6 76.3 43.7 28.5 16.4
4 120 1.07 30 0.27 90 0.8
5 154 6 4


6 =file("e:/excel/result.xlsx") = A6.xlsopen ()
7 =C6.xlscell("B2",1;A1) =C6.xlscell("J2",1;B1) =C6.xlscell("L2",1;C1)
8 =C6.xlscell("B3",1;D1) =C6.xlscell("G3",1;E1) =C6.xlscell("K3",1;F1)
9 =C6.xlscell("B6",1;[A2:F2].concat("\t")) =C6.xlscell("H6",1;[A3:E3].concat("\t"))
10 =C6.xlscell("B9",1;[A4:F4].concat("\t")) =C6.xlscell("B11",1;[A5:C5].concat("\t"))
11 =A6.xlswrite(C6)


假定要填的数据已计算好(在前5行)。样表中前6个要填的单元格都是独立的,所以只能每次填一个格,第6行有连续填写的单元格,此时把待填数据拼成以\t分隔的字符串,这样可以从指定单元格开始顺序填充。数据填完以后,再把C6的Excel对象写回到result.xlsx文件中。

填写完成以后的result.xlsx如下图所示:

..

 

5.  动态条件的显示属性

导出Excel时,要求单元格的显示属性是根据条件动态变化的,比如要隔行显示不同的背景色、单元格颜色或字体与数据值相关等。这种情况需要提供一种能定义显示属性条件表达式、并在导出时能计算表达式的值来得到显示属性的工具,在esProc SPL中,这种工具就是润乾报表。

示例:将订单表数据导出到orders.xlsx中,要求数据行的背景色以两种颜色隔行交替显现,订单金额大于2000的用红色显示,低于500的用绿色显示。

打开润乾报表设计器,新建报表模板“orders.rpx”,如下图所示。

..

报表的第一行是表名称,第二行是列名称,第三行是数据记录行,如何制作报表可以参阅润乾报表的相关教程。

选中第三行的所有单元格,在背景色表达式中填入:if(row()%2==0,-853778,-1),用来指定交替显示的两种背景色。选择第三行最后一个单元格,指定显示格式为#.00,在前景色表达式中填入:if(value()>2000,-65536,if(value()<500,-16711936,-16777216)),指定根据不同金额显示不同的字体颜色。

esProc SPL脚本如下:


A 注释
1 =file("e:/txt/orders.txt":"UTF-8").import@t() 读入文本数据
2 >report_config("e:/cfg/raqsoftConfig.xml") 装载报表环境配置
3 =report_open("orders.rpx") 打开设计的报表模板
4 =report_run(A3;A1:"ds1") 将A1序表作为数据集ds1传递给报表A3进行计算
5 =report_exportXls@x(A3,"e:/excel/orders.xlsx") 将计算后的报表对象A3导出成excel文件

导出后的orders.xlsx如下图所示:

..

 

6.  分组带明细及统计

导出Excel时,要求对数据进行分组,显示各组明细数据及统计数据。这同样需要用润乾报表工具作辅助。

示例:将订单表数据导出到orders.xlsx中,要求按地区和公司分组,显示各组订单明细及订单金额总和。

打开润乾报表设计器,新建报表模板“orders_group.rpx”,如下图所示。

..

报表的第一行是表名称,第二行是列名称,在A3格按货主地区进行分组,B3格按公司名称进行分组,C3、D3、E3显示订单明细。E4格统计各公司的订单金额总和,E5格统计各地区的订单金额总和。

esProc SPL脚本如下:


A 注释
1 =file("e:/txt/orders.txt":"UTF-8").import@t() 读入文本数据
2 >report_config("e:/cfg/raqsoftConfig.xml") 装载报表环境配置
3 =report_open("orders_group.rpx") 打开设计的报表模板
4 =report_run(A3;A1:"ds1") 将A1序表作为数据集ds1传递给报表A3进行计算
5 =report_exportXls@x(A3,"e:/excel/orders.xlsx") 将计算后的报表对象A3导出成excel文件

导出后的orders.xlsx如下图所示:

..

 

7.  交叉表

Exporting the data to an Excel file in cross-tab format still needs to be assisted by the Run Dry Report Tool.

Example: Export the order form data to orders.xlsx, and it is required to display the cross statistics table by region and year.

Open the Runqian report designer and create a new report template "orders_cross.rpx", as shown in the figure below.

..

The first line of the report is the name of the table, the B2 cell is grouped by the year of the order date, the A3 cell is grouped by the shipper's region, and the B3 cell counts the sum of the order amounts of each group.

The esProc SPL script is as follows:


A Comment
1 =file("e:/txt/orders.txt":"UTF-8").import@t() Read in text data
2 >report_config("e:/cfg/raqsoftConfig.xml") Load report environment configuration
3 =report_open("orders_cross.rpx") Open the designed report template
4 =report_run(A3;A1:"ds1") Pass the A1 table sequence as the data set ds1 to report A3 for calculation
5 =report_exportXls@x(A3,"e:/excel/orders.xlsx") Export the calculated report object A3 into an excel file

The exported orders.xlsx is shown below:

There are more related calculation examples in "SPL CookBook".


Guess you like

Origin blog.51cto.com/12749034/2553479