作者:乔山办公网日期:
返回目录:excel表格制作
首先7a64e58685e5aeb9364创建 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 ;
( 一 ) 使用动态创建的方法
首先创建 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 ) 设置指定行的高度(单位:磅)7a686964616fe78988e69d83330(1磅=0.035 厘米),以第二行为例:
ExcelApp.ActiveSheet.Rows [ 2 ] .RowHeight := 1 / 0.035 ; // 1厘米
在 delphi 里读取 excel 数据,用 XLSReadWriteII 控件效率最高,但如果还要操纵生成 Chart 图表,通常还是要借用 OleObject 方式来操作。
试编写示例代码如下:
procedure TForm1.Button3Click(Sender: TObject);
var
ExcelApplication,Sheet1,Cell1,Cell2,Range1:Variant;
begin
try
ExcelApplication := CreateOleObject('Excel.Application');
except
Showmessage('你的电脑里没有安装 Excel 软件');
abort;
end;
ExcelApplication.Visible:=true;
ExcelApplication.Workbooks.Add;
Sheet1:=ExcelApplication.Workbooks[1].Worksheets['sheet1'];
//建立示例数据
Sheet1.Name:='图表示例';
Sheet1.Cells.item[1,1]:='课程';
Sheet1.Cells.item[2,1]:='语文';
Sheet1.Cells.item[3,1]:='数字';
Sheet1.Cells.item[4,1]:='英语';
Sheet1.Cells.item[5,1]:='化学';
Sheet1.Cells.item[6,1]:='物理';
Sheet1.Cells.item[7,1]:='几何';
Sheet1.Cells.item[1,2]:='成绩';
Sheet1.Cells.item[2,2]:=78;
Sheet1.Cells.item[3,2]:=63;
Sheet1.Cells.item[4,2]:=52;
Sheet1.Cells.item[5,2]:=77;
Sheet1.Cells.item[6,2]:=66;
Sheet1.Cells.item[7,2]:=89;
//设定图表座标轴范围e68a84e799bee5baa6e997aee7ad94332
Cell1:=Sheet1.Cells.item[2,1];
Cell2:=Sheet1.Cells.item[7,2];
Range1:=sheet1.Range[cell1,cell2];
Range1.Borders.Color:=27;
//添加图表
Sheet1.ChartObjects.add(160, 40, 400, 280);
sheet1.ChartObjects[1].Activate; //激活图表
sheet1.ChartObjects[1].Chart.charttype:=xlLineStacked; //指定图表为折线图
sheet1.ChartObjects[1].Chart.seriescollection.ADD[Range1]; //建立数据
end;
运行效果截图:
( 一 ) 使用动态创建百度的方法
首先创建 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 '' ) ;