線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:4792
推到 Plurk!
推到 Facebook!

如何指定Excel圖表中,X軸的資料來源?

尚未結案
mnsf
初階會員


發表:104
回覆:90
積分:48
註冊:2003-11-25

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-06-10 16:00:38 IP:61.30.xxx.xxx 未訂閱
各位大大好,我利用以下程式產生一個Excel的圖表,產生出來的圖表如下 要 uses ActiveX; var lcid: integer; _Template, SheetType, _Type: OleVariant; begin lcid := GetUserDefaultLCID; try ExcelApplication1.Connect; ExcelApplication1.Visible[lcid]:=True; _Template:=xlWBATWorksheet; ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Add(_Template, lcid)); ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet); ExcelWorksheet1.Name := 'Data'; except messagedlg('無法開啟Excel!',mtInformation,[mbok],0); exit; end; //(1)將資料轉出至Excel中 ExcelWorksheet1.Cells.Item[1,2]:='A'; ExcelWorksheet1.Cells.Item[1,3]:='B'; ExcelWorksheet1.Cells.Item[2,1]:='P1'; ExcelWorksheet1.Cells.Item[2,2]:='10'; ExcelWorksheet1.Cells.Item[2,3]:='20'; ExcelWorksheet1.Cells.Item[3,1]:='P2'; ExcelWorksheet1.Cells.Item[3,2]:='20'; ExcelWorksheet1.Cells.Item[3,3]:='10'; ExcelWorksheet1.Cells.Item[4,1]:='P3'; ExcelWorksheet1.Cells.Item[4,2]:='50'; ExcelWorksheet1.Cells.Item[4,3]:='30'; //(2)產生Chart SheetType := TOleEnum(xlChart); // cast to avoid compiler range warnings ExcelChart1.ConnectTo(ExcelWorkbook1.Sheets.Add(EmptyParam, EmptyParam, 1, SheetType, lcid) as _Chart); ExcelChart1.Name := 'Chart'; //Sheet Name ExcelChart1.HasLegend[lcid]:= True; //圖例 Legend ExcelChart1.ChartType := TOleEnum(xlLineMarkers); //折線圖有Point,xlLine則是沒有Point ExcelChart1.SetSourceData(ExcelWorksheet1.Range['A2','C4'],TOleEnum(xlRows)); //資料來源 _type:=xlValue; //設定橫軸 (ExcelChart1.Axes(_type,xlPrimary,lcid) as Axis).HasMajorGridlines:=True; //主要格線 (ExcelChart1.Axes(_type,xlPrimary,lcid) as Axis).HasMinorGridlines:=False; //次要格線 (ExcelChart1.Axes(_type,xlPrimary,lcid) as Axis).HasTitle:=true; (ExcelChart1.Axes(_type,xlPrimary,lcid) as Axis).AxisTitle.Text:='Y軸'; _type:=xlCategory; (ExcelChart1.Axes(_type,xlPrimary,lcid) as Axis).HasTitle:=true; (ExcelChart1.Axes(_type,xlPrimary,lcid) as Axis).AxisTitle.Text:='X軸'; ExcelChart1.Disconnect; ExcelWorksheet1.Disconnect; ExcelWorkbook1.Disconnect; ExcelApplication1.Disconnect; end; 請問,要怎麼做才可以把X軸的資料改成我想要的型式?(由1,2改為A,B)
deity
尊榮會員


發表:90
回覆:876
積分:678
註冊:2003-05-09

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-06-10 16:43:53 IP:218.15.xxx.xxx 未訂閱
您好:  
//(2)產生Chart
SheetType := TOleEnum(xlChart); // cast to avoid compiler range warnings
ExcelChart1.ConnectTo(ExcelWorkbook1.Sheets.Add(EmptyParam, EmptyParam, 1, SheetType, lcid) as _Chart);
ExcelChart1.Name := 'Chart'; //Sheet Name
ExcelChart1.HasLegend[lcid]:= True; //圖例 Legend
ExcelChart1.ChartType := TOleEnum(xlLineMarkers); //折線圖有Point,xlLine則是沒有Point
ExcelChart1.SetSourceData(ExcelWorksheet1.Range['A2','C4'],TOleEnum(xlRows)); //資料來源
ExcelChart1.SetSourceData(ExcelWorksheet1.Range['A1','C4'],TOleEnum(xlRows)); //資料來源改为A1即可!

_type:=xlValue; //設定橫軸
(ExcelChart1.Axes(_type,xlPrimary,lcid) as Axis).HasMajorGridlines:=True; //主要格線
(ExcelChart1.Axes(_type,xlPrimary,lcid) as Axis).HasMinorGridlines:=False; //次要格線
(ExcelChart1.Axes(_type,xlPrimary,lcid) as Axis).HasTitle:=true;
(ExcelChart1.Axes(_type,xlPrimary,lcid) as Axis).AxisTitle.Text:='Y軸';
_type:=xlCategory;
(ExcelChart1.Axes(_type,xlPrimary,lcid) as Axis).HasTitle:=true;
(ExcelChart1.Axes(_type,xlPrimary,lcid) as Axis).AxisTitle.Text:='X軸';    ExcelChart1.Disconnect;
ExcelWorksheet1.Disconnect;
ExcelWorkbook1.Disconnect;
ExcelApplication1.Disconnect;    end;     
============================ 为什么经过多年以后,得失的过程如此冷漠 ============================
mnsf
初階會員


發表:104
回覆:90
積分:48
註冊:2003-11-25

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-06-10 17:05:42 IP:61.30.xxx.xxx 未訂閱
感謝deity大大,問題解決了,謝謝
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-06-11 10:32:22 IP:203.204.xxx.xxx 未訂閱
PO程式碼的方式請參考版規說明,煩請修改謝謝您的配合 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=58991 ********************* 如果您滿意答案,請結案 *********************
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
系統時間:2024-06-28 18:41:41
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!