GeneXus学习笔记-Excel导出

本文介绍了一种在Procedure中实现Excel导出的方法。通过定义参数传递筛选条件,并处理超过65535行数据的问题。文章详细展示了如何使用循环来填充单元格值,以及如何在每个新的sheet中重复标题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Excel导出是在一个Procedure中实现的。

就是一个方法。传入的参数可以是你的页面筛选条件。传出的可以是一些比如文件名之类的。


参数

传入传出参数是在rules中定义:

Parm(in:&inParm1,in:&inParm2,in:&inParm3,out:&Filename,out:&ErrorMessage);

参数可以自己定


具体实现

下面把标蓝的地方改成需要的就可以直接用

do 'InitParm'
do 'WriteTitles'
do 'WriteCells'

Sub 'WriteCells'
    For each
		where Attribute1 = &Variable1 when not &Variable1.IsEmpty()    //添加筛选条件,就是rules里传过来的
		where Attribute2 = &Variable2 when not &Variable2.IsEmpty()
		where Attribute3 = &Variable3 when not &Variable3.IsEmpty()
        // Write cell values
        if &CellRow >= 65535  //Excel2003版本的一个sheet最多支持65535行数据,当行数超过65535行时,需要新建一个sheet
            &Sheeti += 1
            &ExcelDocument.SelectSheet('Sheet'+&Sheeti.ToString().Trim())//新建sheet,并切换到新sheet上
            Do 'WriteTitles'
        endif        
        &CellRow += 1
		&CellCursor = 0
        &ExcelDocument.Cells(&CellRow, &FirstColumn + &CellCursor).Text = Attribute1
		&CellCursor += 1
        &ExcelDocument.Cells(&CellRow, &FirstColumn + &CellCursor).Text = Attribute2
		&CellCursor += 1
        &ExcelDocument.Cells(&CellRow, &FirstColumn + &CellCursor).Text = Attribute3
		&CellCursor += 1
        &ExcelDocument.Cells(&CellRow, &FirstColumn + &CellCursor).Text = Attribute4
		&CellCursor += 1
	&ExcelDocument.Cells(&CellRow, &FirstColumn + &CellCursor).Text = Attribute5
		&CellCursor += 1
    Endfor
	&ExcelDocument.Save() 
	Do 'CheckStatus'
	&ExcelDocument.Close()
EndSub

Sub 'WriteTitles'                                                                  //写titles
	// Write titles
	&CellCursor = 0
	&CellRow = 1
	&FirstColumn = 1
	&ExcelDocument.Cells(&CellRow, &FirstColumn + &CellCursor).Bold = True
	&ExcelDocument.Cells(&CellRow, &FirstColumn + &CellCursor).Text = 'title1'
	&CellCursor += 1
	&ExcelDocument.Cells(&CellRow, &FirstColumn + &CellCursor).Bold = True
	&ExcelDocument.Cells(&CellRow, &FirstColumn + &CellCursor).Text = 'title2'
	&CellCursor += 1
	&ExcelDocument.Cells(&CellRow, &FirstColumn + &CellCursor).Bold = True
	&ExcelDocument.Cells(&CellRow, &FirstColumn + &CellCursor).Text = 'title3'
	&CellCursor += 1
	&ExcelDocument.Cells(&CellRow, &FirstColumn + &CellCursor).Bold = True
	&ExcelDocument.Cells(&CellRow, &FirstColumn + &CellCursor).Text = 'title4'
	&CellCursor += 1
	&ExcelDocument.Cells(&CellRow, &FirstColumn + &CellCursor).Bold = True
	&ExcelDocument.Cells(&CellRow, &FirstColumn + &CellCursor).Text = 'title5'
	&CellCursor += 1
EndSub

Sub 'InitParm'
	&Random = Random() * 10000
	&Filename =&Today.Year().ToString().Trim()+&Today.Month().ToString().Trim()+&Today.Day().ToString().Trim()+"_文件名_"+ &Random.ToString().Trim()
	//&ExcelTypeCode = P_GetSysParm('SysExcelType')    //这里是获取Excel 是.xls 还是.xlsx的。自己设置也行    from郑俊
	//if &ExcelTypeCode = ''
 		&ExcelTypeCode = '.xls'
	//endif
	&Filename += &ExcelTypeCode
	&Filename = "Excel\Export\" + &Filename
	&ExcelDocument.Open(&Filename)
	Do 'CheckStatus'
	&ExcelDocument.Clear()
	&Sheeti = 1
EndSub

Sub 'CheckStatus'
	If (&ExcelDocument.ErrCode <> 0)
		&Filename = ""
		&ErrorMessage = &ExcelDocument.ErrDescription
		&ExcelDocument.Close()
		Return
	Endif
Endsub                                                                                    //from 吕晓萌





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值