用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. DevExpress中XtraGrid控件对GridView每行的颜色设置 zt

    改变行颜色 private void GridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArg ...

  2. Count the string -- HDOJ 3336

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. OGRE插件设计-Texture与GLTexture

    背景: 学习OGRE,在OGRE中 Core是最小的精简逻辑集合,而真正的功能则需要插件来实现,但是作为插件应该与Core保持最小的连接,同时Core不会调用插件的接口,而动态链接库又不能直接把类连接 ...

  4. vijosP1388 二叉树数

    vijosP1388 二叉树数 链接:https://vijos.org/p/1388 [思路] Catalan数.根据公式h=C(2n,n)/(n+1)计算.首先化简为 (n+i)/i的积(1< ...

  5. php class中public,private,protected的区别,以及实例

    一,public,private,protected的区别 public:权限是最大的,可以内部调用,实例调用等. protected: 受保护类型,用于本类和继承类调用. private: 私有类型 ...

  6. python学习之字符串

    最近在学习python,随手做些记录,方便以后回顾 #字符串是不可再改变的序列aa='abcd'#aa[2:]='ff' #报错,不可直接赋值#字符串格式化:使用格式化操作符即百分号%来实现print ...

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

    第一部分:实验 首先还是网易云课堂的学习,这次的课程是进程的创建和进程的描述. linux进程的状态与操作系统原理中的描述的进程状态有些不同,例如就绪状态和运行状态都是TASK_RUNNING. Li ...

  8. python实战--Http代理服务器

    打算好好深入研究下pytho的socket编程,那天看了这篇博文,http://www.apprk.com/archives/146,于是打算学习下,仿写了一下,发现写好还真不容易,中途出现很多问题, ...

  9. WPF button 圆角制作

    将以下节点复制到app.xaml的<Application.Resources>节点下 <Style TargetType="{x:Type Button}"&g ...

  10. linux-0.11内核 调试教程+GCC源代码

    http://pan.baidu.com/share/home?uk=453348606&view=share#category/type=0 http://blog.csdn.net/old ...