码迷,mamicode.com
首页 > Windows程序 > 详细

delphi 读取excel 两种方法

时间:2015-05-18 18:26:33      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

http://www.cnblogs.com/ywangzi/archive/2012/09/27/2705894.html

两种方法,一是用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] (注意要有[])

二、用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 Documents\Ca09lin1.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:\Excel\Demo1.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.columnwidth:=10; showmessage(inttostr(a.activecell.columnwidth));

delphi 读取excel 两种方法

标签:

原文地址:http://www.cnblogs.com/bnuvincent/p/4512382.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!