用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. 机器更换登录密码重启,然后SQL Server登录不了

    解决方法:

  2. POJ 3107

    #include<iostream> #include<cstdio> #include<cstring> #include<string> #incl ...

  3. CMD删除Mysql 服务

    用sc.exe这个命令可以删除Windows系统服务 开始 —> 运行 —> cmd.exe,然后输入sc就可以看到了. 使用办法很简单: sc delete "服务名" ...

  4. ARM学习笔记3——数据处理指令

    一.数据处理指令概述 1.概念 数据处理指令是指对存放在寄存器中的数据进行处理的指令.主要包括算术指令.逻辑指令.比较与测试指令以及乘法指令 如果在数据处理指令前使用S前缀,指令的执行结果将会影响CP ...

  5. uploadify上传图片(限制最多五张)

    项目中遇到图片上传的情况,好多都是使用服务器上传控件进行上传的,很是不爽. 然后在网上找到了uploadify的方法,自己总结和修改后分享给大家. 项目文档预览: 1.引用原有css和js    &l ...

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

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

  7. Java多线程异步调度程序分析(二)

    源自:http://blog.sina.com.cn/s/blog_4cc16fc50100c0uh.html public abstract class Result {   //抽象的结果类 pu ...

  8. poj 1149 Pigs 网络流-最大流 建图的题目(明天更新)-已更新

    题目大意:是有M个猪圈,N个顾客,顾客要买猪,神奇的是顾客有一些猪圈的钥匙而主人MIRKO却没有钥匙,多么神奇?顾客可以在打开的猪圈购买任意数量的猪,只要猪圈里有足够数量的猪.而且当顾客打开猪圈后mi ...

  9. 征服 Redis + Jedis + Spring (三)—— 列表操作【转】

    一开始以为Spring下操作哈希表,列表,真就是那么土.恍惚间发现“stringRedisTemplate.opsForList()”的强大,抓紧时间恶补下. 相关链接: 征服 Redis 征服 Re ...

  10. [TypeScript] Installing TypeScript and Running the TypeScript Compiler (tsc)

    This lesson shows you how to install TypeScript and run the TypeScript compiler against a .ts file f ...