原文地址:http://blog.chinaunix.net/uid-20586802-id-3235549.html

/*****************简单的导入功能,涉及到数据类型判断*****************/

string ls_path,ls_name,ls_filter
integer li_ret
long ll_rows,ll_columns
string a[1000,100]  //导入数据行数最大1000行,列数最大100列
ls_filter = "Excel文件(*.xls),*.xls"
li_ret = GetFileOpenName("请选择后缀名为XLS的文件",ls_path,ls_name,"xls",ls_filter)
if li_ret <> 1 then return

//建立OLE对象
OLEObject ExcelServer
ExcelServer = create OLEObject
integer li_excel
li_excel = ExcelServer.ConnectToNewObject("excel.application")
if li_excel < 0 then 
    MessageBox("错误提示","连接excel失败,检查你的系统是否安装了office!")
    return 
end if

ExcelServer.Workbooks.Open(ls_path,0,False)
ExcelServer.Application.DisplayAlerts = False
ll_rows = ExcelServer.ActiveSheet.UsedRange.Rows.Count            //取得总行数
ll_columns= ExcelServer.ActiveSheet.UsedRange.columns.Count    //取得总行数

dw_grid.reset()
if ll_rows = 0 then
    messagebox("提示!","没有Excel数据可导,请检查Excel表格后重来!")
    ExcelServer.Workbooks.close()
    ExcelServer.DisConnectObject()
    Destroy ExcelServer
    return 
end if

//导入的excel文件行数是否超过1000 行
if ll_rows >1000 then
    messagebox("错误提示!","导入的excel文件列数过大,无法导入!")
    ExcelServer.Workbooks.close()
    ExcelServer.DisConnectObject()
    Destroy ExcelServer
    return 
end if

//导入的excel文件列数与所对应的数据窗口列数不一致,则提示
integer li_columncount    //得到数据窗口的总列数,总共14列
li_columncount = integer(dw_grid.describe("datawindow.column.count"))
if ll_columns <> li_columncount - 2 then
    messagebox("提示错误!","导入的excel文件列数与所对应的数据窗口类数不一致!")
    ExcelServer.Workbooks.close()
    ExcelServer.DisConnectObject()
    Destroy ExcelServer
    return 
end if

//从excel文件导入到数据窗口,没有空行,字段,计算列字段都为字符串型
//long m,n
//for m = 1 to ll_rows
//     dw_grid.insertrow(0)
//     for n = 1 to ll_columns
//          a[m,n] = string(ExcelServer.ActiveSheet.Cells[m,n].value)
//          dw_grid.Setitem(m,n,a[m,n])
//    next
//next

//ll_null为空行数
long m,amount,ll_null
amount = 0
for m = 1 to ll_rows
    a[m,1] = String(ExcelServer.ActiveSheet.Cells[m,1].value)
    if  len(a[m,1]) <> 0 then
        amount = amount+1
    end if
next
ll_null = ll_rows - amount

long i,li_count
string ls_text
for i=(ll_null+1+1) to ll_rows   //+1+1即表中列名称算一行,从+1行读起
    li_count = dw_grid.insertrow(0)
    dw_grid.setrow(li_count)
    
    //简称
    dw_grid.object.clientcode_1[li_count] = ExcelServer.ActiveSheet.Cells[i,2].text 
    //客户编码 
    dw_grid.object.clientcode[li_count] =  ExcelServer.ActiveSheet.Cells[i,1].text 
    //数期
    ls_text = ExcelServer.ActiveSheet.Cells[i,3].text
    if not isnumber( ls_text ) and ls_text <> '' then
        messagebox("错误","第["+ string(i) + ',3]单元格必须是数字!')
        return
    else
        dw_grid.object.yyprd_bas_client_sq[li_count] = Long(ls_text)
    end if
    //信用额
    if not isnumber(ExcelServer.ActiveSheet.Cells[i,4].text) and ExcelServer.ActiveSheet.Cells[i,4].text <> '' then
        messagebox("错误","第["+ string(i) + ',4]单元格必须是数字!')
        return
    else
        dw_grid.object.yyprd_bas_client_creditamount[li_count] = Dec(ExcelServer.ActiveSheet.Cells[i,4].text)
    end if
    //所属月份
    if not isdate(ExcelServer.ActiveSheet.Cells[i,5].text) and ExcelServer.ActiveSheet.Cells[i,5].text <> '' then
        messagebox("错误","第["+ string(i) + ',5]单元格必须是日期!')
        return
    else
        dw_grid.object.months[li_count] = datetime(ExcelServer.ActiveSheet.Cells[i,5].text)
    end if
    //货币
    dw_grid.object.currency[li_count] = String(ExcelServer.ActiveSheet.Cells[i,6].text)  
    //汇率
    if not isnumber(ExcelServer.ActiveSheet.Cells[i,7].text) and ExcelServer.ActiveSheet.Cells[i,7].text <> '' then
        messagebox("错误","第["+ string(i) + ',7]单元格必须是数字!')
        return
    else
        dw_grid.object.currencyrate[li_count] = dec(ExcelServer.ActiveSheet.Cells[i,7].text)
    end if
     //销售金额
    if not isnumber(ExcelServer.ActiveSheet.Cells[i,8].text) and ExcelServer.ActiveSheet.Cells[i,8].text <> '' then
        messagebox("错误","第["+ string(i) + ',8]单元格必须是数字!')
        return
    else
        dw_grid.object.salesamount[li_count] = Dec(ExcelServer.ActiveSheet.Cells[i,8].text)
    end if
     //备注
    dw_grid.object.yyprd_cdc_sales_remarks[li_count] = String(ExcelServer.ActiveSheet.Cells[i,9].text)   
    //建立日期
    ls_text = ExcelServer.ActiveSheet.Cells[i,10].text
    if  isnumber(ls_text) and ls_text <> '' then
        messagebox("错误","第["+ string(i) + ',10]单元格必须是日期!')
        return
    else
        dw_grid.object.created_date[li_count] = datetime(ExcelServer.ActiveSheet.Cells[i,10].text)
    end if
next

messagebox("提示","导入数据成功,请点击保存按钮提交到数据库!")
ExcelServer.Workbooks.close()
ExcelServer.DisConnectObject()
Destroy ExcelServer

PowerBuilder学习笔记之导入Excel数据的更多相关文章

  1. .NET MVC 学习笔记(六)— 数据导入

    .NET MVC 学习笔记(六)—— 数据导入 在程序使用过程中,有时候需要新增大量数据,这样一条条数据去Add明显不是很友好,这时候最好就是有一个导入功能,导入所需要的数据,下面我们就一起来看一下导 ...

  2. R学习笔记(4): 使用外部数据

    来源于:R学习笔记(4): 使用外部数据 博客:心内求法 鉴于内存的非持久性和容量限制,一个有效的数据处理工具必须能够使用外部数据:能够从外部获取大量的数据,也能够将处理结果保存.R中提供了一系列的函 ...

  3. 导入excel数据

    前提条件:先要安装好EXCEL软件. 程序中经常要用到导入excel数据的功能.其实通过ole操作excel就简单的几行代码,但记性不好,经常要用经常要找, 还是作篇笔记吧. var ExcelApp ...

  4. SQL Server服务器上需要导入Excel数据的必要条件

    SQL Server服务器上需要导入Excel数据,必须安装2007 Office system 驱动程序:数据连接组件,或者Access2010的数据库引擎可再发行程序包,这样就不必在服务器上装Ex ...

  5. PLSQL Developer导入Excel数据

    LSQL Developer导入Excel数据 最近处理将Excel数据导入Oracle的工作比较多.之前都是采用Sqlldr命令行导入的方式处理.每次导入不同格式的Excel表数据,都需要先把Exc ...

  6. (转)PLSQL Developer导入Excel数据

    场景:近来在做加班记录的统计,主要是统计Excel表格中的时间,因为我对于Excel表格的操作不是很熟悉,所以就想到把表格中的数据导入到数据库中,通过脚本语言来统计,就很方便了!但是目前来看,我还没有 ...

  7. 结合bootstrap fileinput插件和Bootstrap-table表格插件,实现文件上传、预览、提交的导入Excel数据操作流程

    1.bootstrap-fileinpu的简单介绍 在前面的随笔,我介绍了Bootstrap-table表格插件的具体项目应用过程,本篇随笔介绍另外一个Bootstrap FieInput插件的使用, ...

  8. 基于 Aspose.Cells与XML导入excel 数据----操作类封装

    前言 导入excel数据, 在每个项目中基本上都会遇到,第三方插件或者基于微软office,用的最多的就是npoi,aspose.cells和c#基于office这三种方式,其中各有各的优缺点,在这也 ...

  9. 【转】 如何导入excel数据到数据库,并解决导入时间格式问题

    在办公环境下,经常会用到处理excel数据,如果用写程序导入excel数据到数据库那就太麻烦了,涉及解析excel,还要各种格式问题,下面简单利用数据库本身支持的功能解决这类导入问题. 准备 创建表 ...

随机推荐

  1. 肠道微生物研究进展 | Microbiology | Human Gut Microbiome | human gut microbiota

    之前我有过一篇16s基本概念和数据分析的文章.16S 基础知识.分析工具和分析流程详解 可以分成两部分,生物层面和技术层面. 生物层面: 1. 肠道微生物里面包含了哪些微生物?显然包含了所有层面的微生 ...

  2. VS2010专业版和旗舰版(中文版)下载

    本文转载自https://blog.csdn.net/chy2z/article/details/80080399 注意: 中文版为iso镜像文件,使用 Daemon Tools 虚拟光驱软件载入进行 ...

  3. centos下如何开放某个端口?

    命令如下: firewall-cmd --permanent --add-port=/tcp (开放22端口) firewall-cmd --reload

  4. SSH SCP 远程密钥登录配置和服务器间的文件传输

    目录 ssh ssh是什么 ssh安装 使用ssh登录远程主机 退出登录 使用ssh执行单条指令 密钥验证 详细操作 scp rsync sftp 进阶 ssh ssh是什么 ssh (Secure ...

  5. 5.7.27版本mysql新增用户

    因为我们目前只有root,所以只能先用root登陆mysql,再新增用户: $ bin/mysql -u root -p Enter password: Welcome to the MySQL mo ...

  6. MyBatis的学习总结:调用存储过程【参考】

    一.创建存储过程 存储过程的目的:统计edi_test_task 正在运行的任务和非运行的任务 CREATE DEFINER=`root`@`%` PROCEDURE `edihelper`.`SP_ ...

  7. 【428】Dijkstra 算法

    算法思想:(单源最短路径) 1个点到所有其他点的最短路径 查找顶点到其他顶点的最短路径,无法到达的记为+∞,找到最小的,就找到了最短路径的顶点 查看上一轮找到的最小点到达其他点的最小值,找到最短路径的 ...

  8. Python web-Http

    web应用 Web应用程序一般指浏览器端/服务器端应用程序,这类应用程序一般借助谷歌,火狐等浏览器来运行.在网络编程的意义下,浏览器是一个socket客户端,服务器是一个socket服务端 impor ...

  9. docker容器中解决出现:^H^H^H^H

    docker容器中解决出现:^H^H^H^H 环境:docker容器是debain系统 解决: 把stty erase ^H 添加到.bash_profile中 vim /etc/profile st ...

  10. jenkins中点击增加云没反应

    问题:非容器版jenkins中无法增加云 分析: 之前在jenkins中找自带的Kubernetes 插件找不到,所以就下载Kubernetes 插件进行离线安装,明明显示安装成功了,仍然不能增加云, ...