原文地址: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. 响应面分析 | response surface analysis | R代码

    先开题,慢慢补充. 参考: 什么是响应面(RSM)分析 Response-Surface Methods in R, Using rsm In-class Examples with R Code R ...

  2. JVM常量的含义与反编译助记符详解

    1.定义一个常量 public class MyTest2 { public static void main(String[] args) { System.out.println(MyParent ...

  3. 手写MyBatis ORM框架实践

    一.实现手写Mybatis三个难点 1.接口既然不能被实例化?那么我们是怎么实现能够调用的? 2.参数如何和sql绑定 3.返回结果 下面是Mybatis接口 二.Demo实现 1.创建Maven工程 ...

  4. linux 查看gpu信息

  5. gogs 实现webhook钩子(php接口形式)

    1.概要流程 2.准备工作 gogs服务器 linux网站服务器(宝塔) 本地客户端 3.编写钩子访问的接口 在public下新建githook.php文件,代码如下: <?php $cmd = ...

  6. vue 自己编写向左滑动的动画 仿transition

    vue 模板代码: <div class="content-wrap clearfix" :class="{slideIn: showIn, slideOut: s ...

  7. ubuntu上的 /dev/loop0 到 /dev/loop18占到100%的处理

    date : 2019-08-13  09:39:09 author: headsen  chen 处理方法:apt autoremove --purge snapd 再次检测:

  8. Python - Django - JsonResponse 对象

    用 json 模块和 HttpResponse 返回生成的 json views.py: from django.shortcuts import render, HttpResponse impor ...

  9. springboot 整合 Froala Editor 3

    springboot项目中使用 Froala Editor 3,参考官网文档:https://www.froala.com/wysiwyg-editor/docs/overview 下载文件后,引入c ...

  10. JAVA视频压缩

    https://www.cnblogs.com/chuanyueinlife/p/9014627.html