怎么在delphi中读取Excel数据 转

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么在delphi中读取Excel数据 转相关的知识,希望对你有一定的参考价值。

怎么在delphi中读取Excel数据(各种详细操作) 转
( 一 ) 使用动态创建的方法

首先创建 Excel 对象,使用ComObj :
Var
ExcelApp : 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 ] .Font.Name := '' 隶书 '' ;
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.拷贝整个工作表:
ExcelApp.ActiveSheet.Used.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 Then
ExcelApp.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。

1 ) 打开Excel
ExcelApplication1.Connect ;

2 ) 显示当前窗口:
ExcelApplication1.Visible [ 0 ] := True ;

3 ) 更改 Excel 标题栏:
ExcelApplication1.Caption := '' 应用程序调用 Microsoft Excel '' ;

4 ) 添加新工作簿:
ExcelWorkbook1.ConnectTo ( ExcelApplication1.Workbooks.Add ( EmptyParam , 0 ) ) ;

5 ) 添加新工作表:
Var
Temp_Worksheet : _WorkSheet ;
Begin
Temp_Worksheet := ExcelWorkbook1.
WorkSheets.Add ( EmptyParam , EmptyParam , EmptyParam , EmptyParam , 0 ) As _WorkSheet ;
ExcelWorkSheet1.ConnectTo ( Temp_WorkSheet ) ;
End ;

6 ) 打开已存在的工作簿:
ExcelApplication1.Workbooks.Open ( c : \a.xls
EmptyParam , EmptyParam , EmptyParam , EmptyParam ,
EmptyParam , EmptyParam , EmptyParam , EmptyParam ,
EmptyParam , EmptyParam , EmptyParam , EmptyParam , 0 )

7 ) 设置第2个工作表为活动工作表:
ExcelApplication1.WorkSheets [ 2 ] .Activate ;

ExcelApplication1.WorksSheets [ '' Sheet2 '' ] .Activate ;

8 ) 给单元格赋值:
ExcelApplication1.Cells [ 1 , 4 ] .Value := '' 第一行第四列 '' ;

9 ) 设置指定列的宽度(单位:字符个数),以第一列为例:
ExcelApplication1.ActiveSheet.Columns [ 1 ] .ColumnsWidth := 5 ;

10 ) 设置指定行的高度(单位:磅)(1磅=0.035 厘米),以第二行为例:
ExcelApplication1.ActiveSheet.Rows [ 2 ] .RowHeight := 1 / 0.035 ; // 1厘米

11 ) 在第8行之前插入分页符:
ExcelApplication1.WorkSheets [ 1 ] .Rows [ 8 ] .PageBreak := 1 ;

12 ) 在第8列之前删除分页符:
ExcelApplication1.ActiveSheet.Columns [ 4 ] .PageBreak := 0 ;

13 ) 指定边框线宽度:
ExcelApplication1.ActiveSheet.Range [ '' B3 : D4 '' ] .Borders [ 2 ] .Weight := 3 ;
1 - 左 2 - 右 3 - 顶 4 - 底 5 - 斜 ( \ ) 6 - 斜 ( / )

14 ) 清除第一行第四列单元格公式:
ExcelApplication1.ActiveSheet.Cells [ 1 , 4 ] .ClearContents ;

15 ) 设置第一行字体属性:
ExcelApplication1.ActiveSheet.Rows [ 1 ] .Font.Name := '' 隶书 '' ;
ExcelApplication1.ActiveSheet.Rows [ 1 ] .Font.Color := clBlue ;
ExcelApplication1.ActiveSheet.Rows [ 1 ] .Font.Bold := True ;
ExcelApplication1.ActiveSheet.Rows [ 1 ] .Font.UnderLine := True ;

16 ) 进行页面设置:
a.页眉:
ExcelApplication1.ActiveSheet.PageSetup.CenterHeader := '' 报表演示 '' ;
b.页脚:
ExcelApplication1.ActiveSheet.PageSetup.CenterFooter := '' 第&P页 '' ;
c.页眉到顶端边距2cm:
ExcelApplication1.ActiveSheet.PageSetup.HeaderMargin := 2 / 0.035 ;
d.页脚到底端边距3cm:
ExcelApplication1.ActiveSheet.PageSetup.HeaderMargin := 3 / 0.035 ;
e.顶边距2cm:
ExcelApplication1.ActiveSheet.PageSetup.TopMargin := 2 / 0.035 ;
f.底边距2cm:
ExcelApplication1.ActiveSheet.PageSetup.BottomMargin := 2 / 0.035 ;
g.左边距2cm:
ExcelAppli

cation1.ActiveSheet.PageSetup.LeftMargin := 2 / 0.035 ;
h.右边距2cm:
ExcelApplication1.ActiveSheet.PageSetup.RightMargin := 2 / 0.035 ;
i.页面水平居中:
ExcelApplication1.ActiveSheet.PageSetup.CenterHorizontally := 2 / 0.035 ;
j.页面垂直居中:
ExcelApplication1.ActiveSheet.PageSetup.CenterVertically := 2 / 0.035 ;
k.打印单元格网线:
ExcelApplication1.ActiveSheet.PageSetup.PrintGridLines := True ;

17 ) 拷贝操作:

a.拷贝整个工作表:
ExcelApplication1.ActiveSheet.Used.Range.Copy ;

b.拷贝指定区域:
ExcelApplication1.ActiveSheet.Range [ '' A1 : E2 '' ] .Copy ;

c.从A1位置开始粘贴:
ExcelApplication1.ActiveSheet.Range. [ '' A1 '' ] .PasteSpecial ;

d.从文件尾部开始粘贴:
ExcelApplication1.ActiveSheet.Range.PasteSpecial ;

18 ) 插入一行或一列:
a.ExcelApplication1.ActiveSheet.Rows [ 2 ] .Insert ;
b.ExcelApplication1.ActiveSheet.Columns [ 1 ] .Insert ;

19 ) 删除一行或一列:
a.ExcelApplication1.ActiveSheet.Rows [ 2 ] .Delete ;
b.ExcelApplication1.ActiveSheet.Columns [ 1 ] .Delete ;

20 ) 打印预览工作表:
ExcelApplication1.ActiveSheet.PrintPreview ;

21 ) 打印输出工作表:
ExcelApplication1.ActiveSheet.PrintOut ;

22 ) 工作表保存:
If Not ExcelApplication1.ActiveWorkBook.Saved Then
ExcelApplication1.ActiveSheet.PrintPreview ;

23 ) 工作表另存为:
ExcelApplication1.SaveAs ( '' C : \Excel\Demo1.xls '' ) ;

24 ) 放弃存盘:
ExcelApplication1.ActiveWorkBook.Saved := True ;

25 ) 关闭工作簿:
ExcelApplication1.WorkBooks.Close ;

26 ) 退出 Excel:
ExcelApplication1.Quit ;
ExcelApplication1.Disconnect ;

( 三 ) 使用Delphi 控制Excle二维图
在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.charttype := 4 ;

4 )给二维图赋值
series := achart.chart.seriescollection ;
range := sheet1!r2c3 : r3c9 ;
series.add ( range , true ) ;

5 )加上二维图的标题
achart.Chart.HasTitle := True ;
achart.Chart.ChartTitle.Characters.Text := ’ Excle二维图’

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 := '' 坐标轴说明 '' ;
原文来自http://www.cnblogs.com/azhqiang/p/3678832.html
参考技术A 问题后的转是什么意思?

三种方法:
1、用office组件;
2、用comobj方法;
3、用第三方的xlsreadwrite控件。
参考技术B ( 一 ) 使用动态创建的方法

首先创建 Excel 对象,使用ComObj :
Var
ExcelApp : 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 ] .Font.Name := '' 隶书 '' ;
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.拷贝整个工作表:
ExcelApp.ActiveSheet.Used.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 Then
ExcelApp.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。

1 ) 打开Excel
ExcelApplication1.Connect ;

2 ) 显示当前窗口:
ExcelApplication1.Visible [ 0 ] := True ;

3 ) 更改 Excel 标题栏:
ExcelApplication1.Caption := '' 应用程序调用 Microsoft Excel '' ;

4 ) 添加新工作簿:
ExcelWorkbook1.ConnectTo ( ExcelApplication1.Workbooks.Add ( EmptyParam , 0 ) ) ;

5 ) 添加新工作表:
Var
Temp_Worksheet : _WorkSheet ;
Begin
Temp_Worksheet := ExcelWorkbook1.
WorkSheets.Add ( EmptyParam , EmptyParam , EmptyParam , EmptyParam , 0 ) As _WorkSheet ;
ExcelWorkSheet1.ConnectTo ( Temp_WorkSheet ) ;
End ;

6 ) 打开已存在的工作簿:
ExcelApplication1.Workbooks.Open ( c : \a.xls
EmptyParam , EmptyParam , EmptyParam , EmptyParam ,
EmptyParam , EmptyParam , EmptyParam , EmptyParam ,
EmptyParam , EmptyParam , EmptyParam , EmptyParam , 0 )

7 ) 设置第2个工作表为活动工作表:
ExcelApplication1.WorkSheets [ 2 ] .Activate ;

ExcelApplication1.WorksSheets [ '' Sheet2 '' ] .Activate ;

8 ) 给单元格赋值:
ExcelApplication1.Cells [ 1 , 4 ] .Value := '' 第一行第四列 '' ;

9 ) 设置指定列的宽度(单位:字符个数),以第一列为例:
ExcelApplication1.ActiveSheet.Columns [ 1 ] .ColumnsWidth := 5 ;

10 ) 设置指定行的高度(单位:磅)(1磅=0.035 厘米),以第二行为例:
ExcelApplication1.ActiveSheet.Rows [ 2 ] .RowHeight := 1 / 0.035 ; // 1厘米

11 ) 在第8行之前插入分页符:
ExcelApplication1.WorkSheets [ 1 ] .Rows [ 8 ] .PageBreak := 1 ;

12 ) 在第8列之前删除分页符:
ExcelApplication1.ActiveSheet.Columns [ 4 ] .PageBreak := 0 ;

13 ) 指定边框线宽度:
ExcelApplication1.ActiveSheet.Range [ '' B3 : D4 '' ] .Borders [ 2 ] .Weight := 3 ;
1 - 左 2 - 右 3 - 顶 4 - 底 5 - 斜 ( \ ) 6 - 斜 ( / )

14 ) 清除第一行第四列单元格公式:
ExcelApplication1.ActiveSheet.Cells [ 1 , 4 ] .ClearContents ;

15 ) 设置第一行字体属性:
ExcelApplication1.ActiveSheet.Rows [ 1 ] .Font.Name := '' 隶书 '' ;
ExcelApplication1.ActiveSheet.Rows [ 1 ] .Font.Color := clBlue ;
ExcelApplication1.ActiveSheet.Rows [ 1 ] .Font.Bold := True ;
ExcelApplication1.ActiveSheet.Rows [ 1 ] .Font.UnderLine := True ;

16 ) 进行页面设置:
a.页眉:
ExcelApplication1.ActiveSheet.PageSetup.CenterHeader := '' 报表演示 '' ;
b.页脚:
ExcelApplication1.ActiveSheet.PageSetup.CenterFooter := '' 第&P页 '' ;
c.页眉到顶端边距2cm:
ExcelApplication1.ActiveSheet.PageSetup.HeaderMargin := 2 / 0.035 ;
d.页脚到底端边距3cm:
ExcelApplication1.ActiveSheet.PageSetup.HeaderMargin := 3 / 0.035 ;
e.顶边距2cm:
ExcelApplication1.ActiveSheet.PageSetup.TopMargin := 2 / 0.035 ;
f.底边距2cm:
ExcelApplication1.ActiveSheet.PageSetup.BottomMargin := 2 / 0.035 ;
g.左边距2cm:
ExcelAppli

cation1.ActiveSheet.PageSetup.LeftMargin := 2 / 0.035 ;
h.右边距2cm:
ExcelApplication1.ActiveSheet.PageSetup.RightMargin := 2 / 0.035 ;
i.页面水平居中:
ExcelApplication1.ActiveSheet.PageSetup.CenterHorizontally := 2 / 0.035 ;
j.页面垂直居中:
ExcelApplication1.ActiveSheet.PageSetup.CenterVertically := 2 / 0.035 ;
k.打印单元格网线:
ExcelApplication1.ActiveSheet.PageSetup.PrintGridLines := True ;

17 ) 拷贝操作:

a.拷贝整个工作表:
ExcelApplication1.ActiveSheet.Used.Range.Copy ;

b.拷贝指定区域:
ExcelApplication1.ActiveSheet.Range [ '' A1 : E2 '' ] .Copy ;

c.从A1位置开始粘贴:
ExcelApplication1.ActiveSheet.Range. [ '' A1 '' ] .PasteSpecial ;

d.从文件尾部开始粘贴:
ExcelApplication1.ActiveSheet.Range.PasteSpecial ;

18 ) 插入一行或一列:
a.ExcelApplication1.ActiveSheet.Rows [ 2 ] .Insert ;
b.ExcelApplication1.ActiveSheet.Columns [ 1 ] .Insert ;

19 ) 删除一行或一列:
a.ExcelApplication1.ActiveSheet.Rows [ 2 ] .Delete ;
b.ExcelApplication1.ActiveSheet.Columns [ 1 ] .Delete ;

20 ) 打印预览工作表:
ExcelApplication1.ActiveSheet.PrintPreview ;

21 ) 打印输出工作表:
ExcelApplication1.ActiveSheet.PrintOut ;

22 ) 工作表保存:
If Not ExcelApplication1.ActiveWorkBook.Saved Then
ExcelApplication1.ActiveSheet.PrintPreview ;

23 ) 工作表另存为:
ExcelApplication1.SaveAs ( '' C : \Excel\Demo1.xls '' ) ;

24 ) 放弃存盘:
ExcelApplication1.ActiveWorkBook.Saved := True ;

25 ) 关闭工作簿:
ExcelApplication1.WorkBooks.Close ;

26 ) 退出 Excel:
ExcelApplication1.Quit ;
ExcelApplication1.Disconnect ;

( 三 ) 使用Delphi 控制Excle二维图
在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.charttype := 4 ;

4 )给二维图赋值
series := achart.chart.seriescollection ;
range := sheet1!r2c3 : r3c9 ;
series.add ( range , true ) ;

5 )加上二维图的标题
achart.Chart.HasTitle := True ;
achart.Chart.ChartTitle.Characters.Text := ’ Excle二维图’

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 := '' 坐标轴说明 '' ;

原文:http://www.cnblogs.com/awei0611/archive/2011/02/28/1967080.html
参考技术C 1. Uses内加入 comobj
2. 界面放入OpenDialog1、ADOQuery1组件
3. 加入如下代码
procedure TF_Cash.SpeedButton1Click(Sender: TObject);
var excelx,excely,excelz,shm: string;
i : integer;
ExcelApp,WorkBook,ExcelRowCount: Variant;
begin
try
ExcelApp := CreateOleObject('Excel.Application');
WorkBook := ExcelApp.WorkBooks.Open(OpenDialog1.FileName);
ExcelApp.Visible := false;
ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;
for i := 2 to excelrowcount+2 do
begin
excelx := excelapp.Cells[i,1].Value;
excely := excelapp.Cells[i,2].Value;
excelz := excelapp.Cells[i,3].Value;
if ((excelapp.Cells[i,1].Value = '') and (ExcelApp.Cells[i,2].Value = '') and (ExcelApp.Cells[i,3].Value = '')) then
exit
else
with ADOQuery1 do
begin
close;
sql.clear;
sql.add('insert into TableA(字段A,字段B,字段C) values(:字段A,:字段B,:字段C)');
ADOQuery1.Parameters.ParamByName('字段A').Value := excelx;
ADOQuery1.Parameters.ParamByName('字段B').Value := excely;
ADOQuery1.Parameters.ParamByName('字段C').Value := excelz;
execsql;
end;
end;
finally
WorkBook.Close;
ExcelApp.Quit;
ExcelApp := Unassigned;
WorkBook := Unassigned;
end;
end;
参考技术D 给你个网址吧,里面很详细了
http://www.cnblogs.com/safezone/archive/2008/08/05/1260831.html

delphi创建文件和读取.ini文件怎么写

RT

filecreate('路径加文件名');//创建一个文件。

读写ini文件:
先在 uses 定义 Inifiles, 在 var 定义 myinifile:Tinifile;

实现部分写代码:
myinifile:=Tinifile.create('d:\1.ini');//打开D盘的 1.ini 文件。
myinifile.readstring('小节名','关键字','缺省值');//读取字符型数据。
myinifile.redainteger('小节名','关键字','缺省值');//读取整数型数据。
myinifile.readbool('小节名','关键字','缺省值');//读取逻辑型数据。

写入INI文件:
myinifile.writestring('小节名','关键字',变量或字符串值);//写入字符型数据。
myinifile.writeinteger('小节名','关键字','变量或整型数值);//写入整数型数据。
myinifile.writebool('小节名','关键字',变量或TRUE或FALSE);//写入逻辑型数

myinifile.Free;//释放INI文件。
参考技术A Tinifile就是delphi专门处理ini文件的类
一、有必要了解INI文件的结构:
;注释
[小节名]
关键字=值
...
---- INI文件允许有多个小节,每个小节又允许有多个关键字, “=”后面是该关键字的值。
---- 值的类型有三种:字符串、整型数值和布尔值。其中字符串存贮在INI文件中时没有引号,布尔真值
用1表示,布尔假值用0表示。
---- 注释以分号“;”开头。
二、定义
---- 1、在Interface的Uses节增加IniFiles;
---- 2、在Var变量定义部分增加一行:
myinifile:Tinifile;
---- 然后,就可以对变量myinifile进行创建、打开、读取、写入等操作了。
三、打开INI文件
myinifile:=Tinifile.create('program.ini');
---- 上面这一行语句将会为变量myinifile与具体的文件 program.ini建立联系,然后,就可以通过变量
myinifile,来读写program.ini文件中的关键字的值了
---- 值得注意的是,如果括号中的文件名没有指明路径的话,那么这个Program.ini文件会存储在Windows
目录中,把Program.ini文件存储在应用程序当前目录中的方法是:为其指定完整的路径及文件名。下面的两
条语句可以完成这个功能:
Filename:=ExtractFilePath(Paramstr
(0))+'program.ini';
myinifile:=Tinifile.Create(filename);
四、读取关键字的值
---- 针对INI文件支持的字符串、整型数值、布尔值三种数据类型,TINIfiles类提供了三种不同的对象方法
来读取INI文件中关键字的值。
---- 假设已定义变量vs、vi、vb分别为string、 integer、boolean类型。
vs:=myinifile.Readstring
('小节名','关键字',缺省值);
vi:=myinifile.Readinteger
('小节名','关键字',缺省值);
vb:=myinifile.Readbool
('小节名','关键字',缺省值);
---- 其中缺省值为该INI文件不存在该关键字时返回的缺省值。
五、写入INI文件
---- 同样的,TInifile类也提供了三种不同的对象方法,向INI文件写入字符串、整型数及布尔类型的关键字。
myinifile.writestring('小节名','关键字',变量或字符串值);
myinifile.writeinteger('小节名','关键字',变量或整型数值);
myinifile.writebool('小节名','关键字',变量或True或False);
---- 当这个INI文件不存在时,上面的语句还会自动创建该INI文件。
六、删除关键字
---- 除了可用写入方法增加一个关键字,Tinifile类还提供了一个删除关键字的对象方法:
myinifile.DeleteKey('小节名','关键字');
七、小节操作
---- 增加一个小节可用写入的方法来完成,删除一个小节可用下面的对象方法:
myinifile.EraseSection('小节名');
---- 另外Tinifile类还提供了三种对象方法来对小节进行操作:
---- myinifile.readsection('小节名',TStrings变量);可将指定小节中的所有关键字名读取至一个字符串列
表变量中;
---- myinifile.readsections(TStrings变量);可将INI文件中所有小节名读取至一个字符串列表变量中去。
---- myinifile.readsectionvalues('小节名',TStrings变量);可将INI文件中指定小节的所有行(包括关键
字、=、值)读取至一个字符串列表变量中去。
八、释放
在适当的位置用下面的语句释放myinifile:
myinifile.distory;
参考技术B 我写的一个 给你参考一下 实例最直接明白:说明后面
unit PhoneConfig;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls,Inifiles;

type
TPhoneConfigUnit = class(TForm)
Panel1: TPanel;
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
CheckBox1: TCheckBox;
Label4: TLabel;
Edit2: TEdit;
Label5: TLabel;
Label6: TLabel;
Edit3: TEdit;
Edit4: TEdit;
Label7: TLabel;
Label8: TLabel;
Edit5: TEdit;
Edit6: TEdit;
Button1: TButton;
Button2: TButton;
Label9: TLabel;
Edit7: TEdit;
Label10: TLabel;
CheckBox2: TCheckBox;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
Private declarations
public
Public declarations
end;

var
PhoneConfigUnit: TPhoneConfigUnit;
inifile:Tinifile;

implementation

uses PhoneMain;

$R *.dfm

procedure TPhoneConfigUnit.Button2Click(Sender: TObject);
begin
Close;
end;

procedure TPhoneConfigUnit.Button1Click(Sender: TObject);
begin
inifile.WriteString('Regular','FilChar',Edit1.Text);
if CheckBox1.Checked=true then
inifile.WriteString('Regular','NotNum','true')
else
inifile.WriteString('Regular','NotNum','false');
if CheckBox2.Checked=true then
inifile.WriteString('Regular','Category','true')
else
inifile.WriteString('Regular','Category','false');
inifile.WriteString('Regular','ForCheck',Edit2.Text);
inifile.WriteString('Regular','Unitcom',Edit3.Text);
inifile.WriteString('Regular','Move',Edit4.Text);
inifile.WriteString('Regular','Telecom',Edit5.Text);
inifile.WriteString('Regular','Min',Edit6.Text);
inifile.WriteString('Regular','Max',Edit7.Text);
with PhoneCheckUnit do
begin
FilChar:=Edit1.Text;
if CheckBox1.Checked then
NotNum:=true
else
NotNum:=false;
if CheckBox2.Checked then
Category:=true
else
Category:=false;
ForCheck:=Edit2.Text;
Unitcom:=Edit3.Text;
Move:=Edit4.Text;
Telecom:=Edit5.Text;
Min:=Edit6.Text;
Max:=Edit7.Text;

if CheckBox2.Checked then
begin
StringList5.Cells[1,0]:='电信手机';
StringList5.Visible:=true;
sleep(1);
StringList4.Cells[1,0]:='联通手机';
StringList4.Visible:=true;
Splitter6.Height:=3;
sleep(1);
StringList2.Cells[1,0]:='固定电话';
StringList3.Visible:=true;
Splitter5.Height:=3;
sleep(1);
StringList3.Cells[1,0]:='移动手机';
Splitter4.Height:=3;
N10.Visible:=true;
N11.Visible:=true;
N12.Visible:=true;
N13.Visible:=true;
N47.Visible:=false;
end
else
begin
StringList2.Cells[1,0]:='电话号码';
StringList3.Visible:=false;
StringList4.Visible:=false;
StringList5.Visible:=false;
Splitter4.Height:=0;
Splitter5.Height:=0;
Splitter6.Height:=0;
N10.Visible:=false;
N11.Visible:=false;
N12.Visible:=false;
N13.Visible:=false;
N47.Visible:=true;
end;
end;
Close;
end;

procedure TPhoneConfigUnit.FormDestroy(Sender: TObject);
begin
inifile.Destroy;
end;

procedure TPhoneConfigUnit.FormCreate(Sender: TObject);
var
fileName:String;
begin
fileName:=ExtractFilePath(ParamStr(0))+'PhoneCheck.ini';
inifile:=Tinifile.Create(fileName);
Edit1.Text:=inifile.ReadString('Regular','FilChar','-');
if inifile.ReadString('Regular','NotNum','true')='true' then
CheckBox1.Checked:=true
else
CheckBox1.Checked:=false;
if inifile.ReadString('Regular','Category','false')='true' then
CheckBox2.Checked:=true
else
CheckBox2.Checked:=false;
Edit2.Text:= inifile.ReadString('Regular','ForCheck','0');
Edit3.Text:= inifile.ReadString('Regular','Unitcom','130 131 132 155 156');
Edit4.Text:= inifile.ReadString('Regular','Move','139 138 137 136 135 134 159 158 150 151 152 154');
Edit5.Text:= inifile.ReadString('Regular','Telecom','133 153 189');
Edit6.Text:= inifile.ReadString('Regular','Min','12');
Edit7.Text:= inifile.ReadString('Regular','Max','13');
end;

end.

有几点注意一下
1.uses 里面要加上Inifiles引用
2.var
inifile:Tinifile; 变量声明
3.窗体创建程序里面初始化 变量 inifile
fileName:=ExtractFilePath(ParamStr(0))+'PhoneCheck.ini';
inifile:=Tinifile.Create(fileName);
ExtractFilePath(ParamStr(0))是exe文件当前目录,
4.数据读取通过 inifile.ReadString
后面有三个参数:先看一下 ini文件内容结构
[Regular]
FilChar=-
NotNum=true
Category=true
ForCheck=0
Unitcom=130 131 132 155 156
Move=134 135 136 137 138 139 138 137 136 135 134 159 158 150 151 152 154
Telecom=133 153 189
Max=13
Min=11
inifile.ReadString 第一个参数就是[Regular]中的 Regular 标题了
第二个参数是 下面每行等号左边的变量
第三个参数是 若读取变量没有找到相应的变量情况下的默认值
5.数据写入
inifile.WriteString
后面也是3个参数
第一个是同上
第二个也同上
第三个是要写入ini文件变量等号右边的值

6.最后别忘了用过的资源要释放掉,在FormDestroy中用
inifile.Destroy;释放资源

以上是关于怎么在delphi中读取Excel数据 转的主要内容,如果未能解决你的问题,请参考以下文章

利用Python将excel数据读取到word表格

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

java读取excel文件,怎么取日期列?

labview怎么读取RS485转网口的数据

C# OLEDB读取EXCEL的数据为空值

Laya---数据读取--- Excel转Json