g****j 发帖数: 6 | 1 最近有一个棘手的project,就快完工了,可是却遇上一个不可逾越的障碍,作了好些
research,好像microsoft目前还没有解决这个问题。
任务很简单,把一个SQL Stored Procedure的return results 放到Excel spreadsheet
。下面是简单的一段code
Set cmdCommand = New ADODB.Command
cmdCommand.ActiveConnection = CurrentProject.Connection
cmdCommand.CommandType = adCmdStoredProc
cmdCommand.CommandText = "strDTPastDue"
Set rsData = New ADODB.Recordset
rsData.Open cmdCommand.Execute
objExcel.ActiveSheet.Range("A5").CopyFromRecordset rsData
一般情况下运行良好,可是只要database的某一个field (text) 太长(超过250char) |
e*u 发帖数: 17 | 2 试试在写入field前把Application.ScreenUpdating设为false。
所有数据都写入后再设成true
spreadsheet
【在 g****j 的大作中提到】 : 最近有一个棘手的project,就快完工了,可是却遇上一个不可逾越的障碍,作了好些 : research,好像microsoft目前还没有解决这个问题。 : 任务很简单,把一个SQL Stored Procedure的return results 放到Excel spreadsheet : 。下面是简单的一段code : Set cmdCommand = New ADODB.Command : cmdCommand.ActiveConnection = CurrentProject.Connection : cmdCommand.CommandType = adCmdStoredProc : cmdCommand.CommandText = "strDTPastDue" : Set rsData = New ADODB.Recordset : rsData.Open cmdCommand.Execute
|
g****j 发帖数: 6 | 3 等一下去办公室试试看,只是不明白次为何解? 自己写的Function 和Microsoft的
CopyFromRecordset的performance 还是有上百倍的差距,不知道MS下了什么料在他们
的build-in function里。 |
k***g 发帖数: 7244 | 4 这个问题我以前也遇到过,一个 string 的长度不能超过 911 characters,否则就出
Run-time error '1004' 的错误;
使用 loop 的效率太低,即便是你把 application.screenupdating 设置为 false,也
不能有大的改观;
比较好的 solution 是使用 ActiveSheet.QueryTables.Add 的方式,不会出现 911
character 的限制;
这里有一个 reference: http://groups.google.co.uk/group/microsoft.public.excel.programming/browse_thread/thread/cfe13ba9d1211089/55fe0f8715193b3a?lnk=st&q=&rnum=3&hl=en#55fe0f8715193b3a
spreadsheet
【在 g****j 的大作中提到】 : 最近有一个棘手的project,就快完工了,可是却遇上一个不可逾越的障碍,作了好些 : research,好像microsoft目前还没有解决这个问题。 : 任务很简单,把一个SQL Stored Procedure的return results 放到Excel spreadsheet : 。下面是简单的一段code : Set cmdCommand = New ADODB.Command : cmdCommand.ActiveConnection = CurrentProject.Connection : cmdCommand.CommandType = adCmdStoredProc : cmdCommand.CommandText = "strDTPastDue" : Set rsData = New ADODB.Recordset : rsData.Open cmdCommand.Execute
|
g****j 发帖数: 6 | 5 谢谢楼上的二位大哥了,虽然还是没能完全解决问题,但是也至少找到了突破口,the
problem is Excel 的每个cell不能hold 超过911 char的text, 看来这个限制不好突破
。变通了一下,
create a public function as workaround of CopyFromRecordset,then it works all the way.
Function CopyRecordsetToArray(rsData As ADODB.Recordset) As Variant()
'Create an array where # rows = # records and # columns = # fields
'in the recordset.
Dim TempArray() As Variant
Dim r As Integer, c As Integer
ReDim TempArray(1 To rsData.RecordCount, 1 To rsData.Field |