我有一个从Excel文件读取多个工作表的进程,然后将数据插入到数据库表中。但是,当其中一个工作表包含超过65536行时,我遇到了一些内存问题,并且正在寻找如何改进代码的想法。
总之,我使用cfspreadsheet来“读取”上传的Excel文件。(在大多数情况下,文件包含单个工作表。然而,在某些情况下,它超过2张)。这个过程总是读第一页(“细节”)。如果发现超过65533行,那么它也会读取第二个表,即"Details_1“。最后,我使用QoQ和UNION ALL创建一个组合查询。一旦读取,数据将插入到数据库表中。
有人能给出一些建议来改进这个过程,使其更少的记忆强度吗?
<cffunction name="putExcel" access="remote" returnFormat="plain" output="true">
<cfargument name="xclfile" required="no" type="string" default="0">
<cfset ins =insertUserLog("#Session.user_name#","#Session.user_code#","putExcel function called for Upload","","")>
<cftry>
<cfset fileEXCL = "#ExpandPath('../folder')#/#arguments.xclfile#" />
<!---when there e 2 Sheets --->
<!---get info from sheet1 as a "query1"--->
<cfspreadsheet action="read" src="#fileEXCL#" sheet="1" query="Query1" headerrow="1" />
<!--- recordcount for "sheet1" as "count1"--->
<cfset count1 =#Query1.recordcount#>
<!--- case when excel has more than 65533 rows
;THIS IMPLIES THAT THERE 2 SHEETS)--->
<cfif count1 gt 65533>
<!--- take info from sheet 2 as a "query2" and count as "count2"--->
<cfspreadsheet action="read" src="#fileEXCL#" sheet="2" query="Query2" headerrow="1" />
<cfset count2 =#Query2.recordcount#>
<!---club both query's using QoQ and call it "excelQuery"--->
<cfquery dbtype="query" name="excelQuery">
SELECT * FROM Query1
UNION ALL
SELECT * FROM Query2
</cfquery>
<!---total record count for "sheet1" & "sheet2"--->
<cfset rowCount =#excelQuery.recordcount#>
<cfelse>
<!---this case there is just 1 query "Query1" ;rename it "excelQuery"--->
<cfquery dbtype="query" name="excelQuery">
SELECT * FROM Query1
</cfquery>
<!--- recordcount for "sheet1"--->
<cfset rowCount =#excelQuery.recordcount#>
</cfif>
<cflog file="Collections" application="yes" text="#Session.user_info.uname# logged in. Data file #fileEXCL# read. Recordcount:#rowCount#" type="Information">
<cfset ins =insertUserLog("#Session.user_name#","#Session.user_code#","file #fileEXCL# read. ","Recordcount:#rowCount#","")>
<cfcatch type="any" >
<cflog file="Collections" application="yes" text="Error in reading Data file #fileEXCL#." type="Error">
<cfset ins =insertUserLog("#Session.user_name#","#Session.user_code#","error file","failed","#cfcatch.Message#")>
<cfreturn 1>
</cfcatch>
</cftry>
... etc...发布于 2013-08-15 15:17:11
这是一个函数,但是你还没有给我们完整的函数。你的变量范围如何?也许可以更新你的代码,向我们展示所有的功能。
您不需要在大多数cfset语句中使用八进制,如下所示:
<cfset count1 =#Query1.recordcount#>可以直接写成
<cfset count1 =Query1.recordcount>您必须使用SELECT *还是可以指定列?
最重要的是,这可能是因为.您有两个查询。通过使用UNION ALL将它们连接在一起,您将获取第一个工作表的65533行,并将其连接到第二个工作表中的X行。这是一个相当大的数据集,特别是如果您只是在使用Excel。你考虑过使用数据库吗?你现在真的需要所有这些数据吗?
https://codereview.stackexchange.com/questions/29790
复制相似问题