用delphi写excel文件
2007-03-18 21:12

1.引用:      Excel2000, OleServer,Comobj, StdCtrls

2.声明变量:     ExcelApplication,Sheet1:Variant;(全局的或局部的).

3.创建及写入:

try
     ExcelApplication:=CreateOleObject('Excel.Application');
except
     Showmessage('Sorry,你可能?FONT COLOR="#000000">]有安裝Excel');
     abort;
end;
ExcelApplication.Visible:=true;
ExcelApplication.Workbooks.Add(xlWBatWorkSheet);
Sheet1:=ExcelApplication.Workbooks[1].Worksheets['sheet1'];
Sheet1.Name:='Delphi控制Excel Chart';
Sheet1.Cells.item[1,1]:='姓名';     //第1行,第1列

4.使用图表

var Cell1,Cell2,Cell3,Cell4,Range1,Range2:Variant;

//向工作表中添加內嵌圖表﹐Add方法中的四個參數分別表示與儲存格A1的左邊距﹑頂部邊距﹑以及圖表的寬度and高度﹔
Sheet1.ChartObjects.add(10, 60, 500, 280);
sheet1.ChartObjects[1].Activate; //激活當前圖表
sheet1.ChartObjects[1].Chart.charttype:=xl3DColumnClustered; //指定圖表類型﹕立體叢集直條圖
sheet1.ChartObjects[1].Chart.seriescollection.ADD[Range2];     //建立新數例
sheet1.ChartObjects[1].Chart.seriescollection[1].values:=Range2; //指定新數例值
sheet1.ChartObjects[1].Chart.seriesCollection[1].hasdatalabels:=True; //顯示圖表中數列的資料標簽﹔
sheet1.ChartObjects[1].Chart.Axes[xlValue].MinimumScale:=100;//設定數值座標軸的最小刻度值﹔
sheet1.ChartObjects[1].Chart.Axes[xlValue].MaximumScale:=200;//設定數值座標軸的最大刻度值﹔
sheet1.ChartObjects[1].Chart.Axes[xlValue].MajorUnit:=10; //設定數值座標的主要單位﹔
sheet1.ChartObjects[1].Chart.Axes[xlValue].MinorUnit:=10; //設定數值座標的次要單位﹔
sheet1.ChartObjects[1].Chart.Axes[xlCategory].HasTitle:=True;
sheet1.ChartObjects[1].Chart.Axes[xlCategory].AxisTitle.Text:='星期';     //類別座標軸標簽。
sheet1.ChartObjects[1].Chart.Axes[xlCategory].CategoryNames:=Range1;
sheet1.ChartObjects[1].Chart.HasLegend:=false;//不顯示圖例
sheet1.ChartObjects[1].Chart.ChartArea.Fill.Visible:=true; //圖表區域填滿
sheet1.ChartObjects[1].Chart.ChartArea.Fill.ForeColor.SchemeColor:= 28;     //前景色彩﹔
sheet1.ChartObjects[1].Chart.ChartArea.Fill.BackColor.SchemeColor:= 42;     //背景色彩﹔
sheet1.ChartObjects[1].Chart.Rotation :=44; // 以度為單位傳回或設定立體圖表檢視的旋轉值﹔
sheet1.ChartObjects[1].Chart.walls.Interior.ColorIndex:=28;
//如果指定圖表的座標軸為直角﹐并與圖表的轉角或仰角無關﹐則為True,僅適用于立體折線圖﹐直條圖與橫條圖﹔
sheet1.ChartObjects[1].Chart.RightAngleAxes := True;
sheet1.ChartObjects[1].Chart.ChartGroups(1).VaryByCategories:=true;//對每個資料標號指定不同的色彩或圖樣.

////////////////////////////////////////////////////////////////////////////////////////////////////

以下是完整文档

unit Unit1;

interface

uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs

, Excel2000, OleServer,Comobj, StdCtrls;

type
    TForm1 = class(TForm)
      Button1: TButton;
      procedure Button1Click(Sender: TObject);
    private
    { Private declarations }
    //==================================
    ExcelApplication,Sheet1:Variant;
    public
      { Public declarations }
    end;

var
    Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var Cell1,Cell2,Cell3,Cell4,Range1,Range2:Variant;
begin
try
    ExcelApplication:=CreateOleObject('Excel.Application');
except
    Showmessage('Sorry,你可能?FONT COLOR="#000000">]有安裝Excel');
    abort;
end;
ExcelApplication.Visible:=true;
ExcelApplication.Workbooks.Add(xlWBatWorkSheet);
Sheet1:=ExcelApplication.Workbooks[1].Worksheets['sheet1'];
Sheet1.Name:='Delphi控制Excel Chart';
Sheet1.Cells.item[1,1]:='Excel Chart -范例';
Sheet1.Cells.item[2,1]:='星期';         //第二行,第1列
Sheet1.Cells.item[2,2]:='星期一';
Sheet1.Cells.item[2,3]:='星期二';
Sheet1.Cells.item[2,4]:='星期三';       //我要知道sheet的所有属性
Sheet1.Cells.item[2,5]:='星期四';
Sheet1.Cells.item[2,6]:='星期五-';
Sheet1.Cells.item[2,7]:='星期六';
Sheet1.Cells.item[2,8]:='星期日';
Sheet1.Cells.item[3,1]:='銷售量';
Sheet1.Cells.item[3,2]:=115;
Sheet1.Cells.item[3,3]:=112;
Sheet1.Cells.item[3,4]:=156;
Sheet1.Cells.item[3,5]:=148;
Sheet1.Cells.item[3,6]:=132;
Sheet1.Cells.item[3,7]:=196;
Sheet1.Cells.item[3,8]:=162;
Cell1:=Sheet1.Cells.item[2,2];
Cell2:=Sheet1.Cells.item[2,8];
Cell3:=Sheet1.Cells.item[3,2];
Cell4:=Sheet1.Cells.item[3,8];
Range1:=sheet1.Range[cell1,cell2]; //設定Chart類別座標軸的取值范圍
Range2:=sheet1.Range[cell3,cell4]; //設定Chart數值座標軸的取值范圍
Range1.Borders.Color:=27;
Range2.Borders.Color:=6;
//向工作表中添加內嵌圖表﹐Add方法中的四個參數分別表示與儲存格A1的左邊距﹑頂部邊距﹑以及圖表的寬度and高度﹔
Sheet1.ChartObjects.add(10, 60, 500, 280);
sheet1.ChartObjects[1].Activate; //激活當前圖表
sheet1.ChartObjects[1].Chart.charttype:=xl3DColumnClustered; //指定圖表類型﹕立體叢集直條圖
sheet1.ChartObjects[1].Chart.seriescollection.ADD[Range2];    //建立新數例
sheet1.ChartObjects[1].Chart.seriescollection[1].values:=Range2; //指定新數例值
sheet1.ChartObjects[1].Chart.seriesCollection[1].hasdatalabels:=True; //顯示圖表中數列的資料標簽﹔
sheet1.ChartObjects[1].Chart.Axes[xlValue].MinimumScale:=100;//設定數值座標軸的最小刻度值﹔
sheet1.ChartObjects[1].Chart.Axes[xlValue].MaximumScale:=200;//設定數值座標軸的最大刻度值﹔
sheet1.ChartObjects[1].Chart.Axes[xlValue].MajorUnit:=10; //設定數值座標的主要單位﹔
sheet1.ChartObjects[1].Chart.Axes[xlValue].MinorUnit:=10; //設定數值座標的次要單位﹔
sheet1.ChartObjects[1].Chart.Axes[xlCategory].HasTitle:=True;
sheet1.ChartObjects[1].Chart.Axes[xlCategory].AxisTitle.Text:='星期';    //類別座標軸標簽。
sheet1.ChartObjects[1].Chart.Axes[xlCategory].CategoryNames:=Range1;
sheet1.ChartObjects[1].Chart.HasLegend:=false;//不顯示圖例
sheet1.ChartObjects[1].Chart.ChartArea.Fill.Visible:=true; //圖表區域填滿
sheet1.ChartObjects[1].Chart.ChartArea.Fill.ForeColor.SchemeColor:= 28;    //前景色彩﹔
sheet1.ChartObjects[1].Chart.ChartArea.Fill.BackColor.SchemeColor:= 42;    //背景色彩﹔
sheet1.ChartObjects[1].Chart.Rotation :=44; // 以度為單位傳回或設定立體圖表檢視的旋轉值﹔
sheet1.ChartObjects[1].Chart.walls.Interior.ColorIndex:=28;
//如果指定圖表的座標軸為直角﹐并與圖表的轉角或仰角無關﹐則為True,僅適用于立體折線圖﹐直條圖與橫條圖﹔
sheet1.ChartObjects[1].Chart.RightAngleAxes := True;
sheet1.ChartObjects[1].Chart.ChartGroups(1).VaryByCategories:=true;//對每個資料標號指定不同的色彩或圖樣.
end;

Delphi Excel的更多相关文章

  1. Delphi Excel 操作大全

    Delphi Excel 操作大全 (一) 使用动态创建的方法首先创建 Excel 对象,使用ComObj:var ExcelApp: Variant;ExcelApp := CreateOleObj ...

  2. Delphi Excel导入 的通用程序转载

    Delphi Excel导入 的通用程序 (-- ::)转载▼ 标签: it 分类: Delphi相关 步骤: 连excel(自己知道其格式,最好是没个字段在数据一一对应) 读excel数据,填入到数 ...

  3. Delphi Excel FastReport

    unit Unit1; interface uses Printers,Windows, Messages, SysUtils, Variants, Classes, Graphics, Contro ...

  4. Delphi Excel导入 的通用程序

    步骤: 1 连excel(自己知道其格式,最好是没个字段在数据一一对应) 2 读excel数据,填入到数据库 我这里有个函数,实现把excel表格中数据导入数据库,在一条数据导入前判断数据库中是否有该 ...

  5. Delphi Excel操作,写了个ADODataSet转Excel的函数作为后期学习的例子

    使用该函数需要先Use Excel2010 //DataSet导出Excel2010格式//FileName=待导出的Excel的文件名,不带路径以及后缀:TitleLine1=导出后Excel第一表 ...

  6. Delphi中使用python脚本读取Excel数据

    Delphi中使用python脚本读取Excel数据2007-10-18 17:28:22标签:Delphi Excel python原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 . ...

  7. Delphi操作Excel大全

    Delphi操作Excel大全 DELPHI操作excel(转)(一) 使用动态创建的方法 首先创建 Excel 对象,使用ComObj:var ExcelApp: Variant;ExcelApp ...

  8. Delphi中控制Excel(转载)

    用Delphi从数据库中取得资料,然后导出到Excel中做成报表是个不错的选择,因为Excel强大的报表功能那可是没话说前提Delphi中要 uses comobj;var Excel:Variant ...

  9. delphi 完全控制Excel 文件

    ( 一 ) 使用动态创建的方法 uses ComObj; 首先创建 Excel 对象Var   ExcelApp : Variant ;   ExcelApp := CreateOleObject ( ...

随机推荐

  1. [CODEVS1220]数字三角形

    题目描述 Description 如图所示的数字三角形,从顶部出发,在每一结点可以选择向左走或得向右走,一直走到底层,要求找出一条路径,使路径上的值最大. 输入描述 Input Description ...

  2. 进程与线程(7) 进程间通信之信号量 (java os)

    花3分钟浏览一下: http://blog.csdn.net/liu765023051/article/details/8067601 1.生产者,消费者的列子. 2.互斥和同步到底什么东西? 互斥是 ...

  3. sql date 的精度问题

    new  java.sql.Date(...) 是经过yyyy-MM-dd 格式化后的时间格式. 如果需要:HH:mm:ss .则要用 new java.sql.Timestamp(.....);

  4. poj 2932 Coneology(扫描线+set)

    Coneology Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3574   Accepted: 680 Descript ...

  5. ubuntu 手动添加jar到本地仓库

    前言:maven仓库的下载速度太慢了,而且有些jar不存在,或者加入自己开发的依赖包,还是要学会自己手动加入jar.下面以 javax.servlet-api为例. 1.获取下载的javax.serv ...

  6. POI2001 金矿

    问题描述 金矿的老师傅年底要退休了.经理为了奖赏他的尽职尽责的工作,决定在一块包含 n(n ≤ 15000) 个采金点的长方形土地中划出一块长度为 S ,宽度为 W 的区域奖励给他(1 ≤ s , w ...

  7. LR(1)表驱动语法分析程序

    /* * LR(1) 语法分析 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #inc ...

  8. 在Mybatis-spring中由于默认Autowired导致不能配置多个数据源的问题分析及解决

    在使用Mybatis中,通常使用接口来表示一个Sql Mapper的接口以及相对应的xml实现,而在spring的配置文件中,通常会使用MapperScannerConfigurer来达到批量扫描以及 ...

  9. 20169210《Linux内核原理与分析》第四周作业

    网易云课堂学习: 在实验楼的学习中,这次的实验是利用mykernel实验模拟计算机硬件平台 首先进入linux-3.9.4, $cd LinuxKernel/linux-3.9.4 如下图所示 接下来 ...

  10. [D3] 10. Creating Axes with D3

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...