| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5309 人关注过本帖
标题:如何从多个复杂EXCEL文件中批量取得数据存入dbf表文件?
只看楼主 加入收藏
沙枣
Rank: 4
来 自:宁夏银川
等 级:业余侠客
威 望:5
帖 子:103
专家分:221
注 册:2015-5-31
收藏
得分:0 
回复 40楼 antony521
    楼主测试所出现的问题,在我这里一样都没有,程序运行得很顺畅。我用的是windows xp系统,vfp6.0,office2003。扩展名为.xlsx的文件可能不能正确识别(至少我没有这样设计),另外,excel单元格的值只要数据类型正确,不会出现所说的问题,空也是“字符”,你用是什么系统,也许系统会有些不兼容。
    还有,我的这个设计是按照你给的样本数据来的,前提是不同的“检验报告”对应单元格的数据类型都相同,我没有设置容错和数据类型校验、转换功能,如果不同份“检验报告”对应单元格数据类型有异,可能会导致程序运行出错。


[ 本帖最后由 沙枣 于 2015-7-12 16:31 编辑 ]
2015-07-12 16:20
antony521
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:170
专家分:175
注 册:2009-8-20
收藏
得分:0 
回复 41楼 沙枣
我的系统是win7,VFP 9.0 7423.
空值的问题加个判断已解决.另一个问题是程序运行时有EXCEL文件在打开状态,这个以后注意下就可以了.
现在在win7 32位下读取.XSLX文件没再发现问题.周一试试64位系统下,读取大量文件数据.我想应该不会有意外情况.

erase bj-ext.dbf
erase bj-ext.fpt

删除的这两个文件是哪来的?

[ 本帖最后由 antony521 于 2015-7-12 21:13 编辑 ]
2015-07-12 21:02
gyp0117
Rank: 1
等 级:新手上路
帖 子:44
专家分:8
注 册:2012-3-9
收藏
得分:0 
(使用方法把以下代码复制到新建程序中,以程序(.PRG)行式保存,并以ImportExcel命名即可,调用时用DO ImportExcel就可以了)
**Func ImportExcel
*-- Excel 数据导入程序
*-- 编程:红虎 于 2001年11月16日
 
*-- 根据用户选择的 Excel 文件,程序自动将该文件复制到临时目录一份
*-- 取名为:ImpExcel.xls
*-- 循环 Excel 文件中的每一个 Sheet 将有内容的表导入出来。
*-- 依次生成 OnlySheetN 格式的临时文件
 
*-- 返回零,则表示导入失败
 
Private llShowExcel,lcExcelFile,lcTempPath,loExcel,lnSheetCount,lnCurSheet,lcSheetName
Private lcAllSheetsFile,lcCurrentSheetFile,lcTempCursorFile,lcOnlySheetFile  
 
llShowExcel= .F. && 是否显示 Excel 程序
lcAllSheetsFile    = "ImpExcel.xls"&& 将选取的Excel文件复制成的临时文件名
lcCurrentSheetFile= "OnlySheet"&& 得到的每一个工作表的名称
 
lcExcelFile = GetFile("XLS")
If    Empty(lcExcelFile)
    Messagebox("你没有选择任何文件,中断程序的处理!",48,"提醒")
    Retu 0
Endif
 
ShowWait("正在导入Excel文件,请稍候...")
 
lcTempPath = GetEnv("Temp")
*-- 如果需要将源EXCEL文件COPY到临时文件来处理,可以通过下面代码
    *!*    If    File("&lcTempPath.\&lcAllSheetsFile")
    *!*        Dele File "&lcTempPath.\&lcAllSheetsFile"
    *!*    Endif
    *!*    Copy File "&lcExcelFile" to "&lcTempPath.\&lcAllSheetsFile"
    *!*    lcExcelFile = "&lcTempPath.\ImpExcel.xls"
 
loExcel = CreateObject("Excel.Application")    && 创建EXCEL对象
With loExcel
    .Visible    = llShowExcel        && 显示 EXCEL 程序
    .WorkBooks.Open("&lcExcelFile") && 打开临时文件
    lnSheetCount = .WorkBooks(1).Sheets.Count && 统计工作表数量
    *-- Name,Index
*!*        Messagebox("该 Excel 文件有 " +allt(str(lnSheetCount)) + " 个工作表!",64,"Excel")
    ShowProcessBar("正在导入Excel文件,共 " +allt(str(lnSheetCount))+" 个工作表,请稍候...",lnSheetCount)         
    For lnCurSheet=1 to lnSheetCount    && 循环每一个表
        lcSheetName    = .WorkBooks(1).Sheets(lnCurSheet).Name
        ShowBar(lnCurSheet,"正在导入第 "+allt(str(lnCurSheet))+" 个工作表 &lcSheetName ...")
         
        .WorkBooks(1).Sheets(lnCurSheet).Select    && 选择依次的一个表
        .WorkBooks(1).Sheets(lnCurSheet).Cells.Select    && 全选
        .Selection.Copy    && 复制
         
        If    Empty(_ClipText)    && 判断表中有没有内容
            Loop
        Endif
 
        .WorkBooks.Add            && 新增一个工作薄
        .WorkBooks(2).Sheets(1).Activate    && 激活 sheet1
        .WorkBooks(2).Sheets(1).Cells.Select    && 全选 sheet1
        .Selection.PasteSpecial(3)    && 只粘贴数据
        _cliptext = ''
 
        lcOnlySheetFile = "&lcTempPath.\&lcCurrentSheetFile" + allt(str(lnCurSheet)) + ".xls"
        If    File("&lcOnlySheetFile")
            Dele File "&lcOnlySheetFile"
        Endif
        .WorkBooks(2).SaveAs("&lcOnlySheetFile") && 保存临时文件
        .WorkBooks(2).Close    && 关闭
         
        *-- 导入该临时文件
        lcTempTableFile = lcCurrentSheetFile + allt(str(lnCurSheet))
        lcTempCursorFile    = "Temp_Sheet" + allt(str(lnCurSheet))
        If    Used("&lcTempTableFile")
            Use In &lcTempTableFile
        Endif
        If    Used("&lcTempCursorFile")
            Use In &lcTempCursorFile
        Endif
        Select 0
        Import From "&lcOnlySheetFile" Type XLS    && 导入
        Sele * From "&lcTempTableFile" Where .T. Into Cursor "&lcTempCursorFile"
        Use In &lcTempTableFile
        Dele File "&lcTempTableFile..dbf"
    Endfor
     
    .Quit    && 关闭  Excel
 
Endwith
 
Release loExcel
 
ShowWait("导入Excel文件完成",1)
 
Retu lnSheetCount    && 返回多少个工作表
不知这个可否
2015-07-13 07:40
tlliqi
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:204
帖 子:15453
专家分:65956
注 册:2006-4-27
收藏
得分:0 
回复 43楼 gyp0117
应该可行
2015-07-13 07:56
沙枣
Rank: 4
来 自:宁夏银川
等 级:业余侠客
威 望:5
帖 子:103
专家分:221
注 册:2015-5-31
收藏
得分:0 
回复 42楼 antony521
    楼主所主张的判定为“空”即不行代换的做法可能有问题,遇有多条“物资名称”时,我是将当前dbf记录复制到下一条记录中,再用excel表下一行的内容对“物资名称”等进行代换,如果遇“空”即不行代换,则有可能导致excel上一行的内容“残留”在下一条dbf记录中,而引发数据导入错误,因此,excel单元格是“空”也要代换,只不过要把不确定的“空”,变成具体的空字符、0或空日期。
    为此,根据你所试之结果,我对源代码进行了优化,现贴上。上一次所贴源代码中的erase bj-ext.dbf和erase bj-ext.fpt是无用的文件,是我最早用传统方法构建库结构时用的中间库,后来改为sql语句,这两句也就没用了。
    要从excel文件中读取数据,excel文件必须是打开的,我用的是前台运行方式,所以看得见,后台运行方式不可见,但实际还是打开的状态,我认为还是前台方式好些,至少能看见程序在动作,程序运行中不乱点鼠标就行了。
    另外,如果程序运行中意外中断,要设法关闭内存已启动的excel程序,xp下是ctrl+alt+del三键,选中程序列表中的excel点结束程序,其它的系统参见指导手册。这样,程序才能在下一次运行中不出问题。

private all
set talk off
set scoreboard off
set status off
set safety off
set exact on
dimension jgfh(300)
store 10 to jgfh
set color to +7/1,+6/4
clear
cxh=getdir()
cxh=alltrim(cxh)
if cxh=""
clear
wait "未指定文件夹,不予导入." window at 16,50 nowait
tdsj=inkey(1)
return
endif
xjkm1=left(cxh,len(cxh)-1)
*以下程序段以递归法获取某一目录及其下所有子目录名(包括隐含)
Create Table lscfzk (bbnr c(120),wjcd n(10),wjrq d,wjshj c(10),wjsx c(6))
append blank
replace bbnr with xjkm1
wait "正在提取子目录,进度:0/0" window at 16,50 nowait
tdsj=inkey(0.2)
do while .not.eof()
wait "正在提取子目录,进度:"+ltrim(str(recno()))+"/"+ltrim(str(reccount())) window at 16,50 nowait
ttpk=recno()
xjkm1=alltrim(bbnr)+"\"
zdst=adir(fac,(xjkm1+"*.*"),"rashd")
if zdst<>0
*数组前2个单元所存为"."和".."
for upk=3 to zdst
fac(upk,1)=xjkm1+fac(upk,1)
endfor
append from array fac for alltrim(bbnr)<>".".and.alltrim(bbnr)<>"..".and."D"$wjsx
endif
go ttpk
skip
enddo
use
Create Table lsdata (文件名 c(254),所在路径 c(254),生成日期 d,生成时刻 c(8),文件大小 n(6,0),导入结果 c(20))
*xjkm1代表的文件名决定导入何种文件,这里只导入excel文档.
xjkm1="*.xls"
sele 1
use lscfzk
*以下程序段用于获取某一目录下所有的excel文档(包括隐含)
jdt=25
wait clear
go top
do while .not.eof()
gdhh=recno()*64.4/reccount()+25
@12,44 say "正在提取Excel文档目录,进度:"+ltrim(str(recno()*100/reccount(),10,2))+"%  "
do while jdt<=gdhh
set color to +6/1
@13,jdt to 14,90 pen 2
jdt=jdt+0.1
set color to +7/1
enddo
jdt=jdt-0.1
tdsj=inkey()
if recno()/reccount()=1
tdsj=inkey(1)
endif
wady=trim(bbnr)+"\"+xjkm1
num=adir(fac,wady,"rashd")
spk=1
sele 2
use lsdata
do while spk<=num
if alltrim(fac(spk,1))<>".".and.alltrim(fac(spk,1))<>"..".and.(.not."D"$fac(spk,5))
append blank
replace 文件名 with fac(spk,1)
replace 所在路径 with lower(trim(a->bbnr))
replace 生成日期 with fac(spk,3)
replace 生成时刻 with fac(spk,4)
replace 文件大小 with fac(spk,2)
endif
spk=spk+1
enddo
sele 1
skip
enddo
use
sele 2
use
sele 1
Create Table lscfzk (项目名称 c(60),项目编号 c(16),合约编号 c(20),表单编号 c(20),到货日期 d,使用单位 c(10),项目负责人 c(10),物资名称 c(20),物资型号 c(30),数量 n(7,0),单位 c(4),品牌 c(16))
use
use lsdata
*以下程序段用于读取Excel文档
set color to +7/1
clear
wait "正在启动Excel软件并导入各个文档" window at 16,48 nowait
tdsj=inkey(0.5)
OleApp=CreateObject("Excel.application")
if type("OleApp")#"O"
wait clear
messagebox( "访问Excel失败!请检查是否正确安装Excel软件!",48,"没有安装Excel")
OleApp.Quit
release OleApp
use
return
endif
go top
sele 2
use lscfzk
sele 1
do while .not.eof()
xjkm1=trim(所在路径)+"\"+trim(文件名)
if trim(文件名)=""
replace 导入结果 with  "文件名为空,不能导入."
skip
loop
endif

OleApp.Caption ="正在处理第["+ltrim(str(recno()))+"]类文档,进度:"+ltrim(str(recno()*100/reccount(),10,2))+"%"

*导入可用Excel打开的文档
if ".XLS"$upper(XJKM1).and.(.not."$"$xjkm1)
oleapp.application.workbooks.open(xjkm1)
OleApp.Visible =.t.
sele 2
append blank
pp=oleapp.Cells(2,3).Value
if isnull(pp)
pp=""
endif
replace 项目名称 with pp
pp=oleapp.Cells(3,3).Value
if isnull(pp)
pp=""
endif
replace 项目编号 with pp
pp=oleapp.Cells(4,3).Value
if isnull(pp)
pp=""
endif
replace 合约编号 with pp
pp=oleapp.Cells(2,8).Value
if isnull(pp)
pp=""
endif
replace 表单编号 with pp
pp=oleapp.Cells(4,16).Value
if isnull(pp)
pp=ctod("  /  /     ")
endif
replace 到货日期 with pp
pp=oleapp.Cells(4,12).Value
if isnull(pp)
pp=""
endif
replace 使用单位 with pp
pp=oleapp.Cells(3,16).Value
if isnull(pp)
pp=""
endif
replace 项目负责人 with pp
xa=7
pp=oleapp.Cells(xa,4).Value
if isnull(pp)
pp=""
endif
replace 物资名称 with pp
pp=oleapp.Cells(xa,5).Value
if isnull(pp)
pp=""
endif
replace 物资型号 with pp
pp=oleapp.Cells(xa,6).Value
if isnull(pp)
pp=0
endif
replace 数量 with pp
pp=oleapp.Cells(xa,7).Value
if isnull(pp)
pp=""
endif
replace 单位 with pp
pp=oleapp.Cells(xa,8).Value
if isnull(pp)
pp=""
endif
replace 品牌 with pp
xa=xa+1
pp=oleapp.Cells(xa,5).Value
if isnull(pp)
pp=""
endif
do while .not."检验结论"$pp
scatter to aaa
append blank
gather from aaa
pp=oleapp.Cells(xa,4).Value
if isnull(pp)
pp=""
endif
replace 物资名称 with pp
pp=oleapp.Cells(xa,5).Value
if isnull(pp)
pp=""
endif
replace 物资型号 with pp
pp=oleapp.Cells(xa,6).Value
if isnull(pp)
pp=0
endif
replace 数量 with pp
pp=oleapp.Cells(xa,7).Value
if isnull(pp)
pp=""
endif
replace 单位 with pp
pp=oleapp.Cells(xa,8).Value
if isnull(pp)
pp=""
endif
replace 品牌 with pp
xa=xa+1
pp=oleapp.Cells(xa,5).Value
if isnull(pp)
pp=""
endif
enddo
sele 1
replace 导入结果 with  "导入成功"
else
replace 导入结果 with  "文件类型错误,不能导入."
endif
skip
enddo
go top
OleApp.Quit
release OleApp
clear
wait "以下是记录在lsdata库的Excel文档直读结果" at 16,50 windows nowait
brow
sele 2
wait "以下是保存在lscfzk库的Excel文档导入数据" at 16,50 windows nowait
brow
use
sele 1
return


[ 本帖最后由 沙枣 于 2015-7-13 09:03 编辑 ]
2015-07-13 08:59
antony521
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:170
专家分:175
注 册:2009-8-20
收藏
得分:0 
回复 43楼 gyp0117
提取方式Import From "&lcOnlySheetFile" Type Xlsx不适用.XSLX文件(已做相应修改).它的适用范围应该是那些规范的EXCEL表格.有些编程方法值得借鉴,谢谢提供范例!
这个问题用沙枣的方法得到完美解决.
2015-07-13 09:21
antony521
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:170
专家分:175
注 册:2009-8-20
收藏
得分:0 
回复 45楼 沙枣
对这种情况,中间有空行应该怎么处理?
图片附件: 游客没有浏览图片的权限,请 登录注册

只要判断下行物资名称为空就停止导入.
我这样改: Do While .Not."检验结论"$pp .and..not.empty(oleapp.Cells(xa,4).Value)仍能导入.
测试1.rar (41.39 KB)


[ 本帖最后由 antony521 于 2015-7-13 12:32 编辑 ]
2015-07-13 11:24
沙枣
Rank: 4
来 自:宁夏银川
等 级:业余侠客
威 望:5
帖 子:103
专家分:221
注 册:2015-5-31
收藏
得分:0 
回复 47楼 antony521
“物资名称”下的空行,可以短路越过,可以适应“物资名称”首行之后任意行放置空行的情况,预设最多越过500行,否则有可能进入死循环,程序如下:

private all
set talk off
set scoreboard off
set status off
set safety off
set exact on
dimension jgfh(300)
store 10 to jgfh
set color to +7/1,+6/4
clear
cxh=getdir()
cxh=alltrim(cxh)
if cxh=""
clear
wait "未指定文件夹,不予导入." window at 16,50 nowait
tdsj=inkey(1)
return
endif
xjkm1=left(cxh,len(cxh)-1)
*以下程序段以递归法获取某一目录及其下所有子目录名(包括隐含)
Create Table lscfzk (bbnr c(120),wjcd n(10),wjrq d,wjshj c(10),wjsx c(6))
append blank
replace bbnr with xjkm1
wait "正在提取子目录,进度:0/0" window at 16,50 nowait
tdsj=inkey(0.2)
do while .not.eof()
wait "正在提取子目录,进度:"+ltrim(str(recno()))+"/"+ltrim(str(reccount())) window at 16,50 nowait
ttpk=recno()
xjkm1=alltrim(bbnr)+"\"
zdst=adir(fac,(xjkm1+"*.*"),"rashd")
if zdst<>0
*数组前2个单元所存为"."和".."
for upk=3 to zdst
fac(upk,1)=xjkm1+fac(upk,1)
endfor
append from array fac for alltrim(bbnr)<>".".and.alltrim(bbnr)<>"..".and."D"$wjsx
endif
go ttpk
skip
enddo
use
Create Table lsdata (文件名 c(254),所在路径 c(254),生成日期 d,生成时刻 c(8),文件大小 n(6,0),导入结果 c(20))
*xjkm1代表的文件名决定导入何种文件,这里只导入excel文档.
xjkm1="*.xls"
sele 1
use lscfzk
*以下程序段用于获取某一目录下所有的excel文档(包括隐含)
jdt=25
wait clear
go top
do while .not.eof()
gdhh=recno()*64.4/reccount()+25
@12,44 say "正在提取Excel文档目录,进度:"+ltrim(str(recno()*100/reccount(),10,2))+"%  "
do while jdt<=gdhh
set color to +6/1
@13,jdt to 14,90 pen 2
jdt=jdt+0.1
set color to +7/1
enddo
jdt=jdt-0.1
tdsj=inkey()
if recno()/reccount()=1
tdsj=inkey(1)
endif
wady=trim(bbnr)+"\"+xjkm1
num=adir(fac,wady,"rashd")
spk=1
sele 2
use lsdata
do while spk<=num
if alltrim(fac(spk,1))<>".".and.alltrim(fac(spk,1))<>"..".and.(.not."D"$fac(spk,5))
append blank
replace 文件名 with fac(spk,1)
replace 所在路径 with lower(trim(a->bbnr))
replace 生成日期 with fac(spk,3)
replace 生成时刻 with fac(spk,4)
replace 文件大小 with fac(spk,2)
endif
spk=spk+1
enddo
sele 1
skip
enddo
use
sele 2
use
sele 1
Create Table lscfzk (项目名称 c(60),项目编号 c(16),合约编号 c(20),表单编号 c(20),到货日期 d,使用单位 c(10),项目负责人 c(10),物资名称 c(20),物资型号 c(30),数量 n(7,0),单位 c(4),品牌 c(16))
use
use lsdata
*以下程序段用于读取Excel文档
set color to +7/1
clear
wait "正在启动Excel软件并导入各个文档" window at 16,48 nowait
tdsj=inkey(0.5)
OleApp=CreateObject("Excel.application")
if type("OleApp")#"O"
wait clear
messagebox( "访问Excel失败!请检查是否正确安装Excel软件!",48,"没有安装Excel")
OleApp.Quit
release OleApp
use
return
endif
go top
sele 2
use lscfzk
sele 1
do while .not.eof()
xjkm1=trim(所在路径)+"\"+trim(文件名)
if trim(文件名)=""
replace 导入结果 with  "文件名为空,不能导入."
skip
loop
endif

OleApp.Caption ="正在处理第["+ltrim(str(recno()))+"]类文档,进度:"+ltrim(str(recno()*100/reccount(),10,2))+"%"

*导入可用Excel打开的文档
if ".XLS"$upper(XJKM1).and.(.not."$"$xjkm1)
oleapp.application.workbooks.open(xjkm1)
OleApp.Visible =.t.
sele 2
append blank
pp=oleapp.Cells(2,3).Value
if isnull(pp)
pp=""
endif
replace 项目名称 with pp
pp=oleapp.Cells(3,3).Value
if isnull(pp)
pp=""
endif
replace 项目编号 with pp
pp=oleapp.Cells(4,3).Value
if isnull(pp)
pp=""
endif
replace 合约编号 with pp
pp=oleapp.Cells(2,8).Value
if isnull(pp)
pp=""
endif
replace 表单编号 with pp
pp=oleapp.Cells(4,16).Value
if isnull(pp)
pp=ctod("  /  /     ")
endif
replace 到货日期 with pp
pp=oleapp.Cells(4,12).Value
if isnull(pp)
pp=""
endif
replace 使用单位 with pp
pp=oleapp.Cells(3,16).Value
if isnull(pp)
pp=""
endif
replace 项目负责人 with pp
xa=7
pp=oleapp.Cells(xa,4).Value
if isnull(pp)
pp=""
endif
replace 物资名称 with pp
pp=oleapp.Cells(xa,5).Value
if isnull(pp)
pp=""
endif
replace 物资型号 with pp
pp=oleapp.Cells(xa,6).Value
if isnull(pp)
pp=0
endif
replace 数量 with pp
pp=oleapp.Cells(xa,7).Value
if isnull(pp)
pp=""
endif
replace 单位 with pp
pp=oleapp.Cells(xa,8).Value
if isnull(pp)
pp=""
endif
replace 品牌 with pp
xa=xa+1
pp1=oleapp.Cells(xa,5).Value
if isnull(pp1)
pp1=""
endif
do while .not."检验结论"$pp1
pp=oleapp.Cells(xa,4).Value
if isnull(pp)
pp=""
endif
*下一行的“物资名称”为空时短路循环
if alltrim(pp)=""
xa=xa+1
pp1=oleapp.Cells(xa,5).Value
if isnull(pp1)
pp1=""
endif
*短路循环500次仍未找到“物资名称”时,当前excel工作簿放弃导入。
if xa>=500
exit
endif
loop
endif
scatter to aaa
append blank
gather from aaa
replace 物资名称 with pp
pp=oleapp.Cells(xa,5).Value
if isnull(pp)
pp=""
endif
replace 物资型号 with pp
pp=oleapp.Cells(xa,6).Value
if isnull(pp)
pp=0
endif
replace 数量 with pp
pp=oleapp.Cells(xa,7).Value
if isnull(pp)
pp=""
endif
replace 单位 with pp
pp=oleapp.Cells(xa,8).Value
if isnull(pp)
pp=""
endif
replace 品牌 with pp
xa=xa+1
pp=oleapp.Cells(xa,5).Value
if isnull(pp)
pp=""
endif
pp1=pp
enddo
sele 1
if xa<500
replace 导入结果 with  "导入成功"
else
replace 导入结果 with  "导入失败"
endif
else
replace 导入结果 with  "文件类型错误,不能导入."
endif
skip
enddo
go top
OleApp.Quit
release OleApp
clear
wait "以下是记录在lsdata库的Excel文档直读结果" at 16,50 windows nowait
brow
sele 2
wait "以下是保存在lscfzk库的Excel文档导入数据" at 16,50 windows nowait
brow
use
sele 1
return

[ 本帖最后由 沙枣 于 2015-7-13 14:46 编辑 ]
2015-07-13 14:34
antony521
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:170
专家分:175
注 册:2009-8-20
收藏
得分:0 
回复 48楼 沙枣
这个搞复杂了,出现空行就是下面没有物资项了,检测到alltrim(物资名称)=""等同于"检验结论"$pp结束循环即可.不可能出现上下都有物资项,中间是空行的情况,如果出现这样的表格,我会掐死制表人的.
就是这个判断为空不知道用那种方法能识别出来,上次用了EMpty(),我再用alltrim()试试.
你的乐于助人和一丝不苟的严谨态度真让人佩服.
当然这段程序没有任何问题,也适用于中间有空行下方还有物资项的情况.太感谢了

[ 本帖最后由 antony521 于 2015-7-13 16:50 编辑 ]
2015-07-13 15:45
sylknb
Rank: 4
等 级:贵宾
威 望:14
帖 子:1528
专家分:180
注 册:2006-6-3
收藏
得分:0 
以下是引用gyp0117在2015-7-13 07:40:34的发言:

(使用方法把以下代码复制到新建程序中,以程序(.PRG)行式保存,并以ImportExcel命名即可,调用时用DO ImportExcel就可以了)
**Func ImportExcel  
*-- Excel 数据导入程序  
*-- 编程:红虎 于 2001年11月16日  
  
*-- 根据用户选择的 Excel 文件,程序自动将该文件复制到临时目录一份  
*-- 取名为:ImpExcel.xls  
*-- 循环 Excel 文件中的每一个 Sheet 将有内容的表导入出来。  
*-- 依次生成 OnlySheetN 格式的临时文件  
  
*-- 返回零,则表示导入失败  
  
Private llShowExcel,lcExcelFile,lcTempPath,loExcel,lnSheetCount,lnCurSheet,lcSheetName  
Private lcAllSheetsFile,lcCurrentSheetFile,lcTempCursorFile,lcOnlySheetFile   
  
llShowExcel= .F. && 是否显示 Excel 程序  
lcAllSheetsFile    = "ImpExcel.xls"&& 将选取的Excel文件复制成的临时文件名  
lcCurrentSheetFile= "OnlySheet"&& 得到的每一个工作表的名称  
  
lcExcelFile = GetFile("XLS")  
If    Empty(lcExcelFile)  
    Messagebox("你没有选择任何文件,中断程序的处理!",48,"提醒")  
    Retu 0  
Endif  
  
ShowWait("正在导入Excel文件,请稍候...")  
  
lcTempPath = GetEnv("Temp")  
*-- 如果需要将源EXCEL文件COPY到临时文件来处理,可以通过下面代码  
    *!*    If    File("&lcTempPath.\&lcAllSheetsFile")  
    *!*        Dele File "&lcTempPath.\&lcAllSheetsFile"  
    *!*    Endif  
    *!*    Copy File "&lcExcelFile" to "&lcTempPath.\&lcAllSheetsFile"  
    *!*    lcExcelFile = "&lcTempPath.\ImpExcel.xls"  
  
loExcel = CreateObject("Excel.Application")    && 创建EXCEL对象  
With loExcel  
    .Visible    = llShowExcel        && 显示 EXCEL 程序  
    .WorkBooks.Open("&lcExcelFile") && 打开临时文件  
    lnSheetCount = .WorkBooks(1).Sheets.Count && 统计工作表数量  
    *-- Name,Index  
*!*        Messagebox("该 Excel 文件有 " +allt(str(lnSheetCount)) + " 个工作表!",64,"Excel")  
    ShowProcessBar("正在导入Excel文件,共 " +allt(str(lnSheetCount))+" 个工作表,请稍候...",lnSheetCount)         
    For lnCurSheet=1 to lnSheetCount    && 循环每一个表  
        lcSheetName    = .WorkBooks(1).Sheets(lnCurSheet).Name  
        ShowBar(lnCurSheet,"正在导入第 "+allt(str(lnCurSheet))+" 个工作表 &lcSheetName ...")  
         
        .WorkBooks(1).Sheets(lnCurSheet).Select    && 选择依次的一个表  
        .WorkBooks(1).Sheets(lnCurSheet).Cells.Select    && 全选  
        .Selection.Copy    && 复制  
         
        If    Empty(_ClipText)    && 判断表中有没有内容  
            Loop  
        Endif  
  
        .WorkBooks.Add            && 新增一个工作薄  
        .WorkBooks(2).Sheets(1).Activate    && 激活 sheet1  
        .WorkBooks(2).Sheets(1).Cells.Select    && 全选 sheet1  
        .Selection.PasteSpecial(3)    && 只粘贴数据  
        _cliptext = ''  
  
        lcOnlySheetFile = "&lcTempPath.\&lcCurrentSheetFile" + allt(str(lnCurSheet)) + ".xls"  
        If    File("&lcOnlySheetFile")  
            Dele File "&lcOnlySheetFile"  
        Endif  
        .WorkBooks(2).SaveAs("&lcOnlySheetFile") && 保存临时文件  
        .WorkBooks(2).Close    && 关闭  
         
        *-- 导入该临时文件  
        lcTempTableFile = lcCurrentSheetFile + allt(str(lnCurSheet))  
        lcTempCursorFile    = "Temp_Sheet" + allt(str(lnCurSheet))  
        If    Used("&lcTempTableFile")  
            Use In &lcTempTableFile  
        Endif  
        If    Used("&lcTempCursorFile")  
            Use In &lcTempCursorFile  
        Endif  
        Select 0  
        Import From "&lcOnlySheetFile" Type XLS    && 导入  
        Sele * From "&lcTempTableFile" Where .T. Into Cursor "&lcTempCursorFile"  
        Use In &lcTempTableFile  
        Dele File "&lcTempTableFile..dbf"  
    Endfor  
      
    .Quit    && 关闭  Excel  
  
Endwith  
  
Release loExcel  
  
ShowWait("导入Excel文件完成",1)  
  
Retu lnSheetCount    && 返回多少个工作表
不知这个可否
里面中有不少语句是什么意思如:ShowWait("正在导入Excel文件,请稍候...")与ShowProcessBar 是属何种语句?程序不能运行?

2015-07-15 15:11
快速回复:如何从多个复杂EXCEL文件中批量取得数据存入dbf表文件?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030588 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved