在PB下开发,我们经常遇到要把数据库的数据导出来变成Excel的问题。网络上有利用第三方组件去完成这个任务的例子。我这里介绍用DataStore检索数据,然后导出Excel。
关键点:
- 1.SyntaxFromSQL
- 2.DataStore.Create from the format SQL
- 3.DataStore.SetTransObject
- 4.DataStore.Retrieve
- 5.DataStore.SaveAs
源代码:
- string ERRORS, sql_syntax
- string presentation_str, dwsyntax_str
- sql_syntax = "select id, fname, lname, address, phone from customer"
- presentation_str = "style(type=grid)"
- dwsyntax_str = SQLCA.SyntaxFromSQL(sql_syntax, presentation_str, ERRORS)
- IF Len(ERRORS) > 0 THEN
- MessageBox("Caution", "SyntaxFromSQL caused these errors: " + ERRORS)
- GOTO Exit_PT
- END IF
- DataStore dob
- dob = create DataStore
- dob.create(dwsyntax_str, ERRORS)
- IF Len(ERRORS) > 0 THEN
- MessageBox("Caution", "Create cause these errors: " + ERRORS)
- GOTO Exit_PT
- END IF
- IF dob.SetTransObject(SQLCA) <> 1 THEN
- MessageBox("Caution", "Fail at SetTransObject")
- GOTO Exit_PT
- END IF
- IF dob.Retrieve() = -1 THEN
- MessageBox("Caution", "Fail at Retrieve")
- GOTO Exit_PT
- END IF
- IF dob.SaveAs("C:\test.XLS", Excel!, true) <> 1 THEN
- MessageBox("Caution", "Fail at SaveAs")
- GOTO Exit_PT
- ELSE
- MessageBox("Caution", "SUCCEED")
- END IF
- Exit_PT:
- IF IsValid(dob) THEN
- Destroy dob
- END IF
导出的Excel: