用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. 【转】我的电脑最近忽然开不了机,启动修复也无法修复,win7系统。开机的时候如果不点启动修复直接正常启动

    原文网址:http://wenda.haosou.com/q/1356139178064356 你好,电脑开机蓝屏主要是:“磁盘有错误”或“非正常关机”引起!这是解决方法:(原创,引用请说明作者:力王 ...

  2. jQuery选择器上下文

  3. 【Android 复习】:第02期:引导界面(二)使用ViewPager实现欢迎引导页面

    一.实现的效果图 也许是养成了这样一个习惯,每次看别人的代码前,必须要先看实现的效果图达到了一个什么样的效果,是不是跟自己想要实现的效果类似,有图才有真相嘛,呵呵.           二.编码前的准 ...

  4. 12篇学通C#网络编程

    转自:http://www.cnblogs.com/huangxincheng/archive/2012/01/03/2310779.html 在C#的网络编程中,进程和线程是必备的基础知识,同时也是 ...

  5. 洛谷1439 排列LCS问题

    洛谷1439 排列LCS问题 本题地址:http://www.luogu.org/problem/show?pid=1439 题目描述 给出1-n的两个排列P1和P2,求它们的最长公共子序列. 输入输 ...

  6. struts1与strut2的区别

    struts1和struts2是两个完全不同的框架 struts1工作流程:发布Struts Web服务时,根据web.xml初始化ActionServlet,ActionContext等内容.在接到 ...

  7. Yii框架tips(转)

    yii的一些小的技巧 http://www.yiichina.com/topic/151 db组件 'schemaCachingDuration'=>3600, 为什么不起做用?需要开缓存 如何 ...

  8. MAC 下安装opencv遇到问题的解决方法(安装homebrew, wget)

    遇到问题: (1)Mac安装OpenCV下载ippicv_macosx_20141027.tgz失败解决方案 先附上当时的报错信息: -- ICV: Downloading ippicv_macosx ...

  9. oc学习之路----多级指针的使用和内存分析

    ---恢复内容开始--- 精髓:要熟悉指针的使用,首先要熟悉指针的各种状态存得是什么数据.(以一级指针 int *p1 二级指针:int **p2 三级指针:int ***p3为例) 一级指针:*p1 ...

  10. 几种不同风格的Toast

    一般情况下,我们使用Toast默认的风格就行了,只是有些时候为了达到我们自己想要的效果需要自定义一下,包括自定义显示的位置,显示的内容以及完全自定义里面的布局,代码如下: activity: pack ...