delphi对Excel的操作1

合集下载

怎么在delphi中读取Excel数据(各种详细操作)

怎么在delphi中读取Excel数据(各种详细操作)

怎么在delphi中读取Excel数据(各种详细操作)转( 一) 使用动态创建的方法首先创建Excel 对象,使用ComObj :VarExcelApp : Variant ;ExcelApp := CreateOleObject ( '' Excel.Application '' ) ;1 ) 显示当前窗口:ExcelApp.Visible := True ;2 ) 更改Excel 标题栏:ExcelApp.Caption := '' 应用程序调用Microsoft Excel '' ;3 ) 添加新工作簿:ExcelApp.WorkBooks.Add ;4 ) 打开已存在的工作簿:ExcelApp.WorkBooks.Open ( '' C : \Excel\Demo.xls '' ) ;5 ) 设置第2个工作表为活动工作表:ExcelApp.WorkSheets [ 2 ] .Activate ;或ExcelApp.WorksSheets [ '' Sheet2 '' ] .Activate ;6 ) 给单元格赋值:ExcelApp.Cells [ 1 , 4 ] .Value := '' 第一行第四列'' ;7 ) 设置指定列的宽度(单位:字符个数),以第一列为例:ExcelApp.ActiveSheet.Columns [ 1 ] .ColumnsWidth := 5 ;8 ) 设置指定行的高度(单位:磅)(1磅=0.035 厘米),以第二行为例:ExcelApp.ActiveSheet.Rows [ 2 ] .RowHeight := 1 / 0.035 ; // 1厘米9 ) 在第8行之前插入分页符:ExcelApp.WorkSheets [ 1 ] .Rows [ 8 ] .PageBreak := 1 ;10 ) 在第8列之前删除分页符:ExcelApp.ActiveSheet.Columns [ 4 ] .PageBreak := 0 ;11 ) 指定边框线宽度:ExcelApp.ActiveSheet.Range [ '' B3 : D4 '' ] .Borders [ 2 ] .Weight := 3 ;1 - 左2 - 右3 - 顶4 - 底5 - 斜( \ )6 - 斜( / )12 ) 清除第一行第四列单元格公式:ExcelApp.ActiveSheet.Cells [ 1 , 4 ] .ClearContents ;13 ) 设置第一行字体属性:ExcelApp.ActiveSheet.Rows [ 1 ] := '' 隶书'' ;ExcelApp.ActiveSheet.Rows [ 1 ] .Font.Color := clBlue ;ExcelApp.ActiveSheet.Rows [ 1 ] .Font.Bold := True ;ExcelApp.ActiveSheet.Rows [ 1 ] .Font.UnderLine := True ;14 ) 进行页面设置:a.页眉:ExcelApp.ActiveSheet.PageSetup.CenterHeader := '' 报表演示'' ;b.页脚:ExcelApp.ActiveSheet.PageSetup.CenterFooter := '' 第&P页'' ;c.页眉到顶端边距2cm:ExcelApp.ActiveSheet.PageSetup.HeaderMargin := 2 / 0.035 ;d.页脚到底端边距3cm:ExcelApp.ActiveSheet.PageSetup.HeaderMargin := 3 / 0.035 ;e.顶边距2cm:ExcelApp.ActiveSheet.PageSetup.TopMargin := 2 / 0.035 ;f.底边距2cm:ExcelApp.ActiveSheet.PageSetup.BottomMargin := 2 / 0.035 ;g.左边距2cm:ExcelApp.ActiveSheet.PageSetup.LeftMargin := 2 / 0.035 ;h.右边距2cm:ExcelApp.ActiveSheet.PageSetup.RightMargin := 2 / 0.035 ;i.页面水平居中:ExcelApp.ActiveSheet.PageSetup.CenterHorizontally := 2 / 0.035 ;j.页面垂直居中:ExcelApp.ActiveSheet.PageSetup.CenterVertically := 2 / 0.035 ;k.打印单元格网线:ExcelApp.ActiveSheet.PageSetup.PrintGridLines := True ;15 ) 拷贝操作:a.拷贝整个工作表:ed.Range.Copy ;b.拷贝指定区域:ExcelApp.ActiveSheet.Range [ '' A1 : E2 '' ] .Copy ;c.从A1位置开始粘贴:ExcelApp.ActiveSheet.Range. [ '' A1 '' ] .PasteSpecial ;d.从文件尾部开始粘贴:ExcelApp.ActiveSheet.Range.PasteSpecial ;16 ) 插入一行或一列:a.ExcelApp.ActiveSheet.Rows [ 2 ] .Insert ;b.ExcelApp.ActiveSheet.Columns [ 1 ] .Insert ;17 ) 删除一行或一列:a.ExcelApp.ActiveSheet.Rows [ 2 ] .Delete ;b.ExcelApp.ActiveSheet.Columns [ 1 ] .Delete ;18 ) 打印预览工作表:ExcelApp.ActiveSheet.PrintPreview ;19 ) 打印输出工作表:ExcelApp.ActiveSheet.PrintOut ;20 ) 工作表保存:If Not ExcelApp.ActiveWorkBook.Saved ThenExcelApp.ActiveSheet.PrintPreview ;21 ) 工作表另存为:ExcelApp.SaveAs ( '' C : \Excel\Demo1.xls '' ) ;22 ) 放弃存盘:ExcelApp.ActiveWorkBook.Saved := True ;23 ) 关闭工作簿:ExcelApp.WorkBooks.Close ;24 ) 退出Excel:ExcelApp.Quit ;( 二) 使用Delphi 控件方法在Form中分别放入ExcelApplication , ExcelWorkbook和ExcelWorksheet。

【Delphi】汇出Excel时,Excel单元格格式设置(附Delphi操作Excel方法)

【Delphi】汇出Excel时,Excel单元格格式设置(附Delphi操作Excel方法)

【Delphi】汇出Excel时,Excel单元格格式设置(附Delphi操作Excel方法)一个例子:excelworksheet1.Cells.Item[row,3]. numberformatlocal:='$#,##0.00;[红色]-$#,##0.00';excelworksheet1.Cells.Item[row,3].Formula:='=SUM(R[-'+inttostr(row-rowflag+1)+']C:R[-1]C)';excelworksheet1.Cells.Item[row,3].Borders[3].LineStyle :=xlContinuous; excelworksheet1.Cells.Item[row,3].Borders[4].LineStyle :=xlDouble; excelworksheet1.Cells.Item[row,4].Borders[3].LineStyle :=xlContinuous; excelworksheet1.Cells.Item[row,4].Borders[4].LineStyle :=xlDouble; excelworksheet1.Cells.Item[row,4].Formula:='=R[-1]C';注:不知道公式及格式,均可通过录制宏找出对应公式及格式。

效果如下图:资料来自网络单元格设置1.设置单元格线框Excel.ActiveSheet.Range[B10:C13].Borders[N].LineStyle := xlNoneExcel.ActiveSheet.Range[B10:C13].Borders[N].Weight := xlThin边框的类型 Borders[N]xlEdgeLeft 左=1xlEdgeRight 右=2xlEdgeTop 顶=3xlEdgeBottom 底=4xlDiagonalUp 左上右下=5xlDiagonalDown 左下右上=6xlEdgeLeft 外部左边框=7xlEdgeTop 外部上边框=8xlEdgeBottom 外部下边框=9xlEdgeRight 外部右边框=10xlInsideVertical 内部竖线=11xlInsideHorizontal 内部横线=12(其中1:为左 2:右 3:顶 4:底 5:斜\ 6:斜/)线条类型LineStyle,宽度Weight单条线的LineStyle := xlContinuous双条线的LineStyle := xlDouble虚线 xlHairline 1实线 xlThin中实线 xlMedium粗实线 xlThick2.给单元格赋值:Excel.Cells[1,4].Value := 第一行第四列;3.设置第一行字体属性(隶书,蓝色,加粗,下划线):Excel.ActiveSheet.Rows[1] := 隶书; Excel.ActiveSheet.Rows[1].Font.Color := clBlue; Excel.ActiveSheet.Rows[1].Font.Bold := True; Excel.ActiveSheet.Rows[1].Font.UnderLine := True;4.设置整个表字体为9Excel.Cells.Font.Size:=9;5.在第8行之前插入/删除分页符:Excel.WorkSheets[1].Rows[8].PageBreak := 1; (0为删除)6.清除第一行第四列单元格公式:Excel.ActiveSheet.Cells[1,4].ClearContents;7.从数字类型转换成文本类型(不知道格式化字符串,请录制宏,抽出宏中格式化字符串。

如何在Delphi中操作Excel

如何在Delphi中操作Excel

如何在Delphi中操作Excel在数据库应用软件的开发过程中,经常需要把数据转换成Excel文件,让用户作进一步的数据处理。

而Delphi是开发数据库应用系统的常用工具,那么,如何在Delphi中操作Excel呢?我们知道,在Microsoft Office软件中有一种内嵌的编程语言VBA,它是一种宏语言,利用它,你可以编写出功能强大的代码,如打开文件、修改数据、保存数据和设置字体等。

另一方面,Microsoft Office软件中的宏能以VBA代码的形式记录下你的操作过程。

因此借助宏操作,可以很轻松地实现某一功能,并把这些代码稍作修改嵌入到你的软件中。

但是VBA也存在一个缺点,它必须有Microsoft Office作平台,在哪里编写,必须在哪里执行。

例如在Excel下编写的一段VBA代码,则它只有在Excel下才能运行。

因此,笔者的方法就是把VBA代码嵌入到Delphi中,从而实现用Delphi操作Excel。

下面,我们以Delphi程序为例,说明这种调用方法:Unit excel;interfaceusesWindows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs, StdCtrls,ComObj;//ComObj是操作OLE对象的函数集type TForm1 =class(TForm)Button1: TButton;procedure Button1Click(Sender: TObject);private{ Private declarations }public{ Public declarations }end;varForm1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);vareclApp,WorkBook:Variant;//声明为OLE Automation 对象xlsFileName:string;beginxlsFileName:=‘ex.xls‘;try//创建OLE对象Excel Application与 WorkBookeclApp:=CreateOleObject(‘Excel.Application‘);WorkBook:=CreateOleobject(‘Excel.Sheet‘);exceptShowMessage(‘您的机器里未安装Microsoft Excel。

delphi excel 例程

delphi excel 例程

delphi excel 例程Delphi Excel 例程:一步一步回答在Delphi中,我们可以使用Excel 例程来实现与Excel文件的交互。

Excel 例程是一组可以通过Delphi编程语言来操作Excel文件的方法和函数。

本文将介绍如何使用Delphi和Excel 例程来创建、读取、写入和操作Excel 文件。

第一步:安装Excel 例程要开始使用Delphi和Excel 例程,我们首先需要安装Excel 例程库。

这可以通过三种方法之一来实现:1. 使用ActiveX组件:在Delphi IDE的“Component”菜单下,选择“Import ActiveX Control”。

在弹出的窗口中,选择“Microsoft Excel xx.x Object Library”(其中"xx.x"表示Excel版本号),然后点击“Install”按钮进行安装。

2. 使用第三方库:你还可以使用第三方库,如TExcelComponent或Flexcel,它们提供了更多的功能和灵活性。

3. 编写自己的Excel 例程:如果你对编程充满信心,你可以自己编写一组Excel 例程,以与Excel文件交互。

不过,这需要一些时间和努力。

第二步:创建一个新的Excel文件在使用Delphi和Excel 例程之前,我们首先需要创建一个Excel文件。

下面是示例代码,展示了如何使用Excel 例程来创建和保存一个新的Excel文件:delphivarExcelApp: Variant;ExcelWorkbook: Variant;ExcelWorksheet: Variant;begin创建Excel应用程序对象ExcelApp := CreateOleObject('Excel.Application');创建一个新的工作簿ExcelWorkbook := ExcelApp.Workbooks.Add;获取第一个工作表ExcelWorksheet := ExcelWorkbook.Worksheets.Item[1];设置单元格的值ExcelWorksheet.Cells[1, 1].Value := 'Hello'; ExcelWorksheet.Cells[1, 2].Value := 'World!';保存工作簿ExcelWorkbook.SaveAs('C:\NewExcelFile.xlsx');关闭Excel应用程序ExcelApp.Quit;end;通过这段代码,我们可以创建一个新的Excel应用程序对象,并在其中创建一个新的工作簿。

Delphi操作Excel

Delphi操作Excel

4)
添加新工作簿:
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Add(EmptyParam,0));
5)
添加新工作表:
var Temp_Worksheet: _WorkSheet;
22)
放弃存盘:
ExcelApp.ActiveWorkBook.Saved := True;
23)
关闭工作簿:
ExcelApp.WorkBooks.Close;
24)
退出
Excel

ExcelApp.Quit;
h.
右边距
2cm

ExcelApp.ActiveSheet.PageSetup.RightMargin := 2/0.035;
i.
页面水平居中:
ExcelApp.ActiveSheet.PageSetup.CenterHorizontally := 2/0.035;
ExcelApp.ActiveSheet.Range[ 'B3:D4' ].Borders[2].Weight := 3;
1-

2-

3-

4-

5-

( \ ) 6-

( / )
12)
清除第一行第四列单元格公式:
15)
拷贝操作:
a.
拷贝整个工作表:
ed.Range.Copy;
b.
拷贝指定区域:
ExcelApp.ActiveSheet.Range[ 'A1:E2' ].Copy;

Delphi中控制Excel

Delphi中控制Excel
10.页面垂直居中:
Excel.ActiveSheet.PageSetup.CenterVertically := 2/0.035;
11.打印单元格网线:
Excel.ActiveSheet.PageSetup.PrintGridLines := True;
12.横向打印
Excel.ActiveSheet.PageSetup.Orientation:=2
第9种=6,3
第12种=1,3
第13种=1,4
第2种=2,1
单条线的LineStyle := xlContinuous
双条线的LineStyle := xlDouble
虚线 xlHairline 1
实线 xlThin
中实线 xlMedium
粗实线 xlThick
XLLineStyle等值参考Excel里面的VisualBasic对象浏览器帮助 (如图)
Excel.ActiveSheet.Columns[A:A].EntireColumn.AutoFit; //第一列
6.居中显示J列
Excel.Columns[J:J].HorizontalAlignment:=3;
7.设置E列单元格式为3位小数数值
Excel.Columns[E:E].NumberFormatLocal:=#,##0.000_
8.设置E列单元格式为文字
Excel.Columns[E:E].NumberFormatLocal:=@
四、单元格设置
1.设置单元格线框
Excel.ActiveSheet.Range[B10:C13].Borders[N].LineStyle := xlNone

Delphi操作EXCEL_函数整理

Delphi操作EXCEL_函数整理

一、使用单元:Use :EXCEL 2000, ComObj二、定义var ExcelApp,Sheet,Range: Variant;注释:ExcelApp 为定义的Excel 对象Sheet 为定义的工作表(Sheet)对象Range 为定义的工作表范围三、关于Excel 对象创建初始化以及工作表、工作表范围的初始化1. 创建excel对象:ExcelApp := CreateOleObject( 'Excel.Application' );2. 显示当前窗口:ExcelApp.Visible := True;3. 更改Excel 标题栏:ExcelApp.Caption := '应用程序调用Microsoft Excel';4. 添加新工作簿:ExcelApp.WorkBooks.Add;5 打开已存在的工作簿:ExcelApp.WorkBooks.Open(FilePath);6.设置工作簿默认工作表张数ExcelApp.SheetsInNewWorkbook := 1;7.设置工作表名称ExcelApp.Workbooks[WorkbookNum].WorkSheets[WorkSheetNum].Name := PName;8.工作表初始化Sheet:= ExcelApp.Workbooks[WorkbookNum].WorkSheets[WorkSheetNum];9.工作表范围初始化Range := Sheet.Range['A1:DL1'] ;10.设置第2个工作表为活动工作表:ExcelApp.WorkSheets[2].Activate;或ExcelApp.WorksSheets[ 'Sheet2' ].Activate;三、单元格赋值设置数值类型1. 给单元格赋值:ExcelApp.Cells[row, column].Value := '第一行第四列';或Sheet.Cells[row, column] := ‘第一行第四列’;2 清除第一行第四列单元格公式:ExcelApp.ActiveSheet.Cells[row,column].ClearContents;或Sheet.Cells[row,column]. ClearContents3设置单元格数值类型为文字型Range.NumberFormatLocal := '@';四、设置列宽和行高1. 设置指定列的宽度(单位:字符个数):ExcelApp.ActiveSheet.Columns[column].ColumnsWidth := 5;或Sheet.Columns[column].ColumnWidth := 5;2.设置指定行的高度(单位:磅)(1磅=0.035厘米)ExcelApp.ActiveSheet.Rows[row].RowHeight := 1/0.035; // 1厘米或Sheet. Rows [row]. RowHeight:= 1/0.035;五、对齐方式1.水平对齐Range.HorizontalAlignment:=xlCenter; 居中Range.HorizontalAlignment:=xlLeft;居左Range.HorizontalAlignment:=xlRight;居右或Sheet.Cells[row,column].HorizontalAlignment:=xlCenter; 居中Sheet.Cells[row,column].HorizontalAlignment:=xlLeft;居左Sheet.Cells[row,column].HorizontalAlignment:=xlRight;居右2.垂直对齐Range. VerticalAlignment:=xlCenter; 居中Range. VerticalAlignment:= xltop;顶端对齐Range. VerticalAlignment:= xlbottomfont;底部对齐或Sheet.Cells[row,column]. VerticalAlignment:=xlCenter; 居中Sheet.Cells[row,column]. VerticalAlignment:= xltop;顶端对齐Sheet.Cells[row,column]. VerticalAlignment:= xlbottomfont; 底部对齐六、文字控制1.自动换行Range.WrapText:=true;或Sheet.Cells[row,column]. WrapText:=true;2.合并单元格Sheet.range['A17:G17'].Merge;3.跨越合并Sheet.range['C27:D34'].Merge(true);七、字型设置1.设置字体大小Range.Characters.Font.Size := 10;或Sheet.Cells[row,column].Characters.Font.Size := 10;2.字体加重Range.Characters.Font. Bold:= true;或Sheet.Cells[row,column].Characters.Font. Bold:= true;3.字体倾斜Range.Characters.Font. Italic:= true;或Sheet.Cells[row,column].Characters.Font. Italic:= true;4.字体:=’楷体_GB2312'或Sheet.Cells[row,column].Characters.Font. Name:=’楷体_GB2312’5字体样式(常规,斜体,粗体,粗斜体)Range.Characters.Font.FontStyle:=’常规'或Sheet.Cells[row,column].Characters.Font.FontStyle:=’常规'6.下划线Range.Characters.Font.UnderLine := True;或Sheet.Cells[row,column].Characters.Font.UnderLine := True;八、分页符操作1.在第8行之前插入分页符:ExcelApp.WorkSheets[1].Rows.PageBreak := 1;或Sheet. Rows. PageBreak := 1;或Range. Rows. PageBreak := 1;2.在第8列之前删除分页符:ExcelApp.ActiveSheet.Columns[4].PageBreak := 0;或Sheet.Columns[4]. PageBreak := 0;或Range.Columns[4].PageBreak :=0;九、关于边框的操作:Border :1-左2-右3-顶4-底5-斜( \ ) 6-斜( / )1.指定边框线宽度:ExcelApp.ActiveSheet.Range[ 'B3:D4' ].Borders[Border].Weight := 3;或Sheet. Borders[Border].Weight := 3;或Range Borders[Border].Weight := 3;2.边框线线条形状Sheet.Borders[Border].linestyle :=1;或Range.Borders[Border]. Linestyle := 1;Linestyle 1:实线2 虚线12 双横线十、关于颜色1. 设置单元格背景色Sheet.Cells[row,column].Interior.ColorIndex := 38;或Range.Interior.ColorIndex := 38;2.字体颜色Range.Characters.Font.Colorindex:=6;或Sheet.Cells[row,column].Characters.Font.Colorindex:=6 Colorindex的值十一、冻结窗口Sheet.Cells[2,3].select;ExcelApp.ActiveWindow.FreezePanes := True;十二、打印设置1.页眉:Sheet.PageSetup.CenterHeader := '报表演示';2.页脚:Sheet.PageSetup.CenterFooter := '第&P页';3页眉到顶端边距2cm:Sheet.PageSetup.HeaderMargin := 2/0.035;4.页脚到底端边距3cm:Sheet.PageSetup.HeaderMargin := 3/0.035;5.顶边距2cm:Sheet.PageSetup.TopMargin := 2/0.035;6.底边距2cm:Sheet.PageSetup.BottomMargin := 2/0.035;7.左边距2cm:Sheet.PageSetup.LeftMargin := 2/0.035;8.右边距2cm:Sheet.PageSetup.RightMargin := 2/0.035;9.页面水平居中:Sheet.PageSetup.CenterHorizontally := 2/0.035;10.页面垂直居中:Sheet.PageSetup.CenterVertically := 2/0.035;11.打印单元格网线:Sheet.PageSetup.PrintGridLines := True;12缩放比例Sheet.PageSetup.zoom := 75;13横向打印Sheet.PageSetup.Orientation:= xllandscape;14 纵向打印Sheet.PageSetup.Orientation:=xlportrait;15.设置打印标题行Sheet.PageSetup.PrintTitleRows := Sheet.Rows['1:3'].Address ;Sheet.PageSetup.PrintTitleColumns := Sheet.Rows[3].Address;16.打印预览工作表:Sheet.PrintPreview;17 打印输出工作表:Sheet.PrintOut;十三、拷贝操作:1.拷贝整个工作表:ed.Range.Copy;2. b.拷贝指定区域:Sheet.Range[ 'A1:E2' ].Copy;3.从A1位置开始粘贴:Sheet.Range.[ 'A1' ].PasteSpecial;4.从文件尾部开始粘贴:Sheet.Range.PasteSpecial;十四、行列的插入删除1. 插入一行或一列:a. ExcelApp.ActiveSheet.Rows[2].Insert;b. ExcelApp.ActiveSheet.Columns[1].Insert;2. 删除一行或一列:a. ExcelApp.ActiveSheet.Rows[2].Delete;b. ExcelApp.ActiveSheet.Columns[1].Delete;十五、Excel保存关闭1.工作表保存:if not ExcelApp.ActiveWorkBook.Saved thenExcelApp.ActiveSheet.PrintPreview;2.工作表另存为:ExcelApp. SaveAs ( filepath);3 放弃存盘:ExcelApp.ActiveWorkBook.Saved := True;4. 关闭工作簿:ExcelApp.WorkBooks.Close;5. 退出Excel:ExcelApp.Quit;6. 工作表关闭ExcelApp.WorkBooks.Close ;十六、其他1. 已经使用的行数edRange.Rows.Count;(三)使用Delphi控制Excel二维图在Form中分别放入ExcelApplication,ExcelWorkbook和ExcelWorksheet Var asheet1,achart,range:variant;1)选择当第一个工作簿第一个工作表asheet1:=ExcelApplication1.Workbooks[1].Worksheets[1];2)增加一个二维图achart:=asheet1.chartobjects.add(100,100,200,200);3)选择二维图的形态achart.chart.chartype:=4;4)给二维图赋值series:=achart.chart.seriescollection;range:=sheet1!r2c3:r3c9;series.add(range,true);5)加上二维图的标题achart.Chart.HasTitle:=True;achart.Chart.ChartTitle.Characters.Text:=’Excel二维图’6)改变二维图的标题字体大小achart.Chart.ChartTitle.Font.size:=6;7)给二维图加下标说明achart.Chart.Axes(xlCategory,xlPrimary).HasTitle:=True;achart.Chart.Axes(xlCategory,xlPrimary).AxisTitle.Characters.Text:=’下标说明’;8)给二维图加左标说明achart.Chart.Axes(xlValue,xlPrimary).HasTitle:=True;achart.Chart.Axes(xlValue,xlPrimary).AxisTitle.Characters.Text:=’左标说明’;9)给二维图加右标说明achart.Chart.Axes(xlValue,xlSecondary).HasTitle:=True;achart.Chart.Axes(xlValue,xlSecondary).AxisTitle.Characters.Text:=’右标说明’;10)改变二维图的显示区大小achart.Chart.PlotArea.Left:=5;achart.Chart.PlotArea.Width:=223;achart.Chart.PlotArea.Height:=108;11)给二维图坐标轴加上说明achart.chart.seriescollection[1].NAME:=’坐标轴说明’;。

delphi中如何读取Excel数据?delphi中读取Excel数据详解

delphi中如何读取Excel数据?delphi中读取Excel数据详解

delphi中如何读取Excel数据?delphi中读取Excel数据详解( ⼀ ) 使⽤动态创建的⽅法⾸先创建 Excel 对象,使⽤ComObj :VarExcelApp : Variant ;ExcelApp := CreateOleObject ( '' Excel.Application '' ) ;1 ) 显⽰当前窗⼝:ExcelApp.Visible := True ;2 ) 更改 Excel 标题栏:ExcelApp.Caption := '' 应⽤程序调⽤ Microsoft Excel '' ;3 ) 添加新⼯作簿:ExcelApp.WorkBooks.Add ;4 ) 打开已存在的⼯作簿:ExcelApp.WorkBooks.Open ( '' C : \Excel\Demo.xls '' ) ;5 ) 设置第2个⼯作表为活动⼯作表:ExcelApp.WorkSheets [ 2 ] .Activate ;或ExcelApp.WorksSheets [ '' Sheet2 '' ] .Activate ;6 ) 给单元格赋值:ExcelApp.Cells [ 1 , 4 ] .Value := '' 第⼀⾏第四列 '' ;7 ) 设置指定列的宽度(单位:字符个数),以第⼀列为例:ExcelApp.ActiveSheet.Columns [ 1 ] .ColumnsWidth := 5 ;8 ) 设置指定⾏的⾼度(单位:磅)(1磅=0.035 厘⽶),以第⼆⾏为例:ExcelApp.ActiveSheet.Rows [ 2 ] .RowHeight := 1 / 0.035 ; // 1厘⽶9 ) 在第8⾏之前插⼊分页符:ExcelApp.WorkSheets [ 1 ] .Rows [ 8 ] .PageBreak := 1 ;10 ) 在第8列之前删除分页符:ExcelApp.ActiveSheet.Columns [ 4 ] .PageBreak := 0 ;11 ) 指定边框线宽度:ExcelApp.ActiveSheet.Range [ '' B3 : D4 '' ] .Borders [ 2 ] .Weight := 3 ;1 - 左2 - 右3 - 顶4 - 底5 - 斜 ( \ )6 - 斜 ( / )12 ) 清除第⼀⾏第四列单元格公式:ExcelApp.ActiveSheet.Cells [ 1 , 4 ] .ClearContents ;13 ) 设置第⼀⾏字体属性:ExcelApp.ActiveSheet.Rows [ 1 ] := '' ⾪书 '' ; ExcelApp.ActiveSheet.Rows [ 1 ] .Font.Color := clBlue ; ExcelApp.ActiveSheet.Rows [ 1 ] .Font.Bold := True ; ExcelApp.ActiveSheet.Rows [ 1 ] .Font.UnderLine := True ;14 ) 进⾏页⾯设置:a.页眉:ExcelApp.ActiveSheet.PageSetup.CenterHeader := '' 报表演⽰ '' ;b.页脚:ExcelApp.ActiveSheet.PageSetup.CenterFooter := '' 第&P页 '' ; c.页眉到顶端边距2cm:ExcelApp.ActiveSheet.PageSetup.HeaderMargin := 2 / 0.035 ; d.页脚到底端边距3cm:ExcelApp.ActiveSheet.PageSetup.HeaderMargin := 3 / 0.035 ; e.顶边距2cm:ExcelApp.ActiveSheet.PageSetup.TopMargin := 2 / 0.035 ;f.底边距2cm:ExcelApp.ActiveSheet.PageSetup.BottomMargin := 2 / 0.035 ; g.左边距2cm:ExcelApp.ActiveSheet.PageSetup.LeftMargin := 2 / 0.035 ;h.右边距2cm:ExcelApp.ActiveSheet.PageSetup.RightMargin := 2 / 0.035 ;i.页⾯⽔平居中:ExcelApp.ActiveSheet.PageSetup.CenterHorizontally := 2 / 0.035 ; j.页⾯垂直居中:ExcelApp.ActiveSheet.PageSetup.CenterVertically := 2 / 0.035 ; k.打印单元格⽹线:ExcelApp.ActiveSheet.PageSetup.PrintGridLines := True ;15 ) 拷贝操作:a.拷贝整个⼯作表:ed.Range.Copy ;b.拷贝指定区域:ExcelApp.ActiveSheet.Range [ '' A1 : E2 '' ] .Copy ;c.从A1位置开始粘贴:ExcelApp.ActiveSheet.Range. [ '' A1 '' ] .PasteSpecial ;d.从⽂件尾部开始粘贴:ExcelApp.ActiveSheet.Range.PasteSpecial ;16 ) 插⼊⼀⾏或⼀列:a.ExcelApp.ActiveSheet.Rows [ 2 ] .Insert ;b.ExcelApp.ActiveSheet.Columns [ 1 ] .Insert ;17 ) 删除⼀⾏或⼀列:a.ExcelApp.ActiveSheet.Rows [ 2 ] .Delete ;b.ExcelApp.ActiveSheet.Columns [ 1 ] .Delete ;18 ) 打印预览⼯作表:ExcelApp.ActiveSheet.PrintPreview ;19 ) 打印输出⼯作表:ExcelApp.ActiveSheet.PrintOut ;20 ) ⼯作表保存:If Not ExcelApp.ActiveWorkBook.Saved ThenExcelApp.ActiveSheet.PrintPreview ;21 ) ⼯作表另存为:ExcelApp.ActiveWorkBook.SaveAs ( '' C : \Excel\Demo1.xls '' ) ;22 ) 放弃存盘:ExcelApp.ActiveWorkBook.Saved := True ;23 ) 关闭⼯作簿:ExcelApp.WorkBooks.Close ;24 ) 退出 Excel:ExcelApp.Quit ;( ⼆ ) 使⽤Delphi 控件⽅法在Form中分别放⼊ExcelApplication , ExcelWorkbook和ExcelWorksheet。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Delphi编程将数据库数据快速导入Excel
责任编辑:ncic 更新日期:2007-4-3
在开发数据库应用程序中,经常要将类型相同的数据导出来,放到Excel文件中,利用Excel强大的编辑功能,对数据作进一步的加工处理。

这有许多的方法,我们可以使用OLE 技术,在Delphi中创建一个自动化对象,通过该对象来传送数据。

也可以使用ADO,通过与Excel数据存储建立连接,使用ADO这种独立于数据库后端的技术来导出数据集的数据。

可这两种技术都有一个共同的缺点,那就是慢,数据量少还好,用户不会有太多的感觉,可一旦数据量大,比如,超过1千条,速度就让人难以忍受了,那么有没有更好的办法,既可以快速地导出数据,又不用安装附加的软件。

也许好多人都想到了剪贴板的方式,这种方式速度是快,可也有不好的一面,那就是数据量大占用内存也大,并且在Excel中调用PASTE 方法时,需要锁定输入,这使用起来,就有点不方便了
这里我为大家介始一种比较好的方法,使用文件流的方式,通过TfileStream直接写入Excel文件。

我写了一个函数,通过它可将数据集中的数据直接导入到Excel文件中。

我测试了一下,1M的数据,不到十秒就完成了。

附源程序。

首先在你的程序中定义以下几个数组:
arXlsBegin: array[0..5] of Word = ($809, 8, 0, $10, 0, 0);
arXlsEnd: array[0..1] of Word = ($0A, 00);
arXlsString: array[0..5] of Word = ($204, 0, 0, 0, 0, 0);
arXlsNumber: array[0..4] of Word = ($203, 14, 0, 0, 0);
arXlsInteger: array[0..4] of Word = ($27E, 10, 0, 0, 0);
arXlsBlank: array[0..4] of Word = ($201, 6, 0, 0, $17);
接着调用下面的函数。

Procedure ExportExcelFile(FileName: string; bWriteTitle: Boolean; aDataSet: TDataSet);
var
i, j: integer;
Col, row: word;
ABookMark: TBookMark;
aFileStream: TFileStream;
procedure incColRow; //增加行列号
begin
if Col = ADataSet.FieldCount - 1 then
begin
Inc(Row);
Col :=0;
end
else
Inc(Col);
end;
procedure WriteStringCell(AValue: string);//写字符串数据
var
L: Word;
begin
L := Length(AValue);
arXlsString[1] := 8 + L;
arXlsString[2] := Row;
arXlsString[3] := Col;
arXlsString[5] := L;
aFileStream.WriteBuffer(arXlsString, SizeOf(arXlsString)); aFileStream.WriteBuffer(Pointer(AValue)^, L);
IncColRow;
end;
procedure WriteIntegerCell(AValue: integer);//写整数
var
V: Integer;
begin
arXlsInteger[2] := Row;
arXlsInteger[3] := Col;
aFileStream.WriteBuffer(arXlsInteger, SizeOf(arXlsInteger));
V := (AValue shl 2) or 2;
aFileStream.WriteBuffer(V, 4);
IncColRow;
end;
procedure WriteFloatCell(AValue: double);//写浮点数
begin
arXlsNumber[2] := Row;
arXlsNumber[3] := Col;
aFileStream.WriteBuffer(arXlsNumber, SizeOf(arXlsNumber)); aFileStream.WriteBuffer(AValue, 8);
IncColRow;
end;
begin
if FileExists(FileName) then DeleteFile(FileName); //文件存在,先删除aFileStream := TFileStream.Create(FileName, fmCreate);
Try
//写文件头
aFileStream.WriteBuffer(arXlsBegin, SizeOf(arXlsBegin));
//写列头
Col := 0; Row := 0;
if bWriteTitle then
begin
for i := 0 to aDataSet.FieldCount - 1 do
WriteStringCell(aDataSet.Fields[i].FieldName);
end;
//写数据集中的数据
aDataSet.DisableControls;
ABookMark := aDataSet.GetBookmark;
aDataSet.First;
while not aDataSet.Eof do
begin
for i := 0 to aDataSet.FieldCount - 1 do
case ADataSet.Fields[i].DataType of
ftSmallint, ftInteger, ftWord, ftAutoInc, ftBytes:
WriteIntegerCell(aDataSet.Fields[i].AsInteger);
ftFloat, ftCurrency, ftBCD:
WriteFloatCell(aDataSet.Fields[i].AsFloat)
else
WriteStringCell(aDataSet.Fields[i].AsString);
end;
aDataSet.Next;
end;
//写文件尾
AFileStream.WriteBuffer(arXlsEnd, SizeOf(arXlsEnd));
if ADataSet.BookmarkValid(ABookMark) then aDataSet.GotoBookmark(ABookMark);
Finally
AFileStream.Free;
ADataSet.EnableControls;
end;
end;。

相关文档
最新文档