用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. Interleaving String——Leetcode

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  2. [并发编程]使用线程安全队列和条件变量的notify来安排分步骤任务

    // 方法1:直接构建N个THread来run foreach (i, size) { thread trd(&Instance::doWork, &inst); lstTrd.pus ...

  3. HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)

    Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...

  4. [ZETCODE]wxWidgets教程五:布局管理

    本教程原文链接:http://zetcode.com/gui/wxwidgets/layoutmanagement/ 翻译:瓶哥 日期:2013年12月4日星期三 邮箱:414236069@qq.co ...

  5. UILabel字体加粗等属性和特效

    /* Accessing the Text Attributes text  property font  property textColor  property textAlignment  pr ...

  6. listview加载图片显示

    Adapter:   ---- //adapter的构造方法:   参数1 为url数组: public static String[] mList;// 讲url保村在静态的String[] 中 在 ...

  7. mysql按字段分组并获取每个分组按照某个字段排序的前三条

    这是原始数据 想按照brand_id分组 并获取每个分组total_num最高的前3位 SQL语句为: > (select count(*) from data where brand_id = ...

  8. ABAP SAPGUI_PROGRESS_INDICATOR 显示数据处理进度

    ABAP处理的数据量较大时,盯着一动不动的选择屏幕是不是很无聊?? LOOP AT I_TAB. DESCRIBE TABLE I_TAB[] LINES L_LIN. L_PERC = SY-TAB ...

  9. LeetCode153:Find Minimum in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  10. 【c++】虚函数描写叙述符override

    在C++11中为了帮助程序猿写继承结构复杂的类型,引入了虚函数描写叙述符override,假设派生类在虚函数声明时使用了override描写叙述符,那么该函数必须重载其基类中的同名函数,否则代码将无法 ...