• delphi 读取excel 两种方法


    两种方法,一是用ADO连接,问题是Excel文件内容要规则,二是用OLE打开,但操作就没有象

    操作数据库那么方便了.

    一、用ADO连接:
    设置属性ConnetionString 
    选择 Microsoft Jet 4.0 OLE DB provider 
    Select or enter a datasorce name -> 选择你要打开Excel文件 
    User name默认是Admin 密码默认为空,可以不用理会 
    Extended properties 设为:Excel 8.0 
    sql语句 select * from [yourtablename] (注意要有[]) 
    如:SELECT   *   FROM   [Sheet1$]

    二、用OLE打开(以下是一个范例,注释掉的代码也是有用的语句,注意要uses ExtCtrls,ComObj单元):
    var ExcelApp:Variant;
    begin
    ExcelApp:=CreateOleObject('Excel.Application');
    //ExcelApp.visible:=true;
    ExcelApp.Caption:='应用程序调用 Microsoft Excel';
    ExcelApp.WorkBooks.Add; //新增工作簿
    //ExcelApp.workBooks.Open('C:My DocumentsCa09lin1.xls'); //打开已存在工作簿
    ExcelApp.Worksheets[2].activate; //打开第2个工作表
    //ExcelApp.WorkSheets['第四章'].activate; //打开名为第四章的工作表
    ExcelApp.Cells[1,4].Value:='第一行第四列';
    ExcelApp.Cells[1,5].Value:='第一行第五列';
    ExcelApp.ActiveSheet.Columns[4].ColumnWidth:=15;
    ExcelApp.ActiveSheet.Rows[1].RowHeight:=15;
    //ExcelApp.WorkSheets[1].Rows[8].PageBreak:=1; //设置分页符,但似无效
    //Excelapp.ActiveSheet.Rows[8].PageBreak:=1; //同上
    ExcelApp.ActiveSheet.Range['B3:D4'].Borders[2].Weight:=3;
    ExcelApp.ActiveSheet.Range['B3:D4'].Borders[1].Weight:=3;
    ExcelApp.ActiveSheet.Range['B3:D4'].Borders[3].Weight:=3;
    ExcelApp.ActiveSheet.Range['B3:D4'].Borders[4].Weight:=3;
    //ExcelApp.ActiveSheet.Range['B3:D4'].Borders[5].Weight:=3; //会直接在范围内的各Cell内加上斜杠|
    //ExcelApp.ActiveSheet.Range['B3:D4'].Borders[6].Weight:=3; //与上句类似
    //Bordrs:1-左 2-右 3-顶 4-底 5-斜( ) 6-斜( / )
    ExcelApp.Cells[3,2].Value:='三行二列';
    ExcelApp.Cells[3,3].Value:='三行三列';
    ExcelApp.Cells[3,4].Value:='三行四列';
    ExcelApp.Cells[4,2].Value:='四行二列';
    ExcelApp.Cells[4,3].Value:='四行三列';
    ExcelApp.Cells[4,4].Value:='四行四列';
    //ExcelApp.ActiveSheet.Range['B3:D4'].Value.CopyToClipBoard;
    ExcelApp.activeSheet.Cells[1,4].ClearContents; //清除一行四列的内容,activeSheet可以省略
    Excelapp.Rows[3].font.Name:='隶书'; //这里Rows前省略了activeSheet,但针对也只是当前工作表而非整个工作簿
    ExcelApp.Rows[3].font.Color:=clBlue;
    ExcelApp.Rows[3].Font.Bold:=True;
    ExcelApp.Rows[3].Font.UnderLine:=True;
    ExcelApp.Range['B3:D4'].Copy;
    RichEdit1.PasteFromClipboard;
    //ExcelApp.ActiveSheet.PageSetup.CenterFooter:='第$P页';
    //所有页面设置(PageSetup的属性)都不能进行,不知为何
    //ExcelApp.ActiveSheet.PrintPreview; //打印预览
    //ExcelApp.ActiveSheet.PrintOut; //直接打印输出
    //if not ExcelApp.ActiveWorkBook.Saved then //工作表保存:
    // ExcelApp.ActiveSheet.PrintPreview;
    //ExcelApp.SaveAs( 'C:ExcelDemo1.xls' ); //工作表另存为
    ExcelApp.ActiveWorkBook.Saved := True; // 放弃存盘
    ExcelApp.WorkBooks.Close; //关闭工作簿
    ExcelApp.Quit; //退出 Excel

    ExcelApp:=Unassigned;//释放excel进程
    end;
    另:
    得到excel的行数、列数:
    Maxc :=ExlApp.WorkSheets[1].UsedRange.Columns.Count;
    Maxr :=ExlApp.WorkSheets[1].UsedRange.Rows.Count;
    得到列宽
    a:=createoleobject('excel.application');
    a.workbooks.add;
    a.activecell.column=10;
    showmessage(inttostr(a.activecell.columnwidth));

  • 相关阅读:
    解决使用intellij idea开发MAVEN项目在target目录下不存在mapper.xml文件
    Mybatis中接口和对应的mapper文件位置配置详解
    Mybatis(1、核心配置文件、Properties、Settings、typeAliases...)
    nginx的常用负载均衡算法,分别是
    修改JVM的参数、Jstat、Jstack、gclog
    shiro 系列
    sso简单原理及实现
    Thymeleaf3.0内容
    Thymeleaf模板引擎+Spring整合使用方式的介绍
    给 IIS Express 配置虚拟目录
  • 原文地址:https://www.cnblogs.com/xtfnpgy/p/9285427.html
Copyright © 2020-2023  润新知