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

BCB 轉 EXCEL 大發現

 
denise
一般會員


發表:2
回覆:5
積分:15
註冊:2002-10-09

發送簡訊給我
#1 引用回覆 回覆 發表時間:2002-11-05 11:26:28 IP:211.75.xxx.xxx 未訂閱
以下是我自己測試出來的 bcb 轉 excel 的一些控制碼. 只要在 form 上新增一個 button , 在 onclick 將下列程式 copy 過去即可. 我的環境為 win98 bcb5. 不過還有合併cell & 新增一個 worksheet 還沒試出來. 麻煩有那位前輩知道的,指點迷津,謝謝. // 轉EXCEL Variant Excel,Workbooks,Workbook; Variant Worksheets,Worksheet,Range,Cell,FColumns,Item; int i,j; // 新增一worksheet Excel=CreateOleObject("Excel.Application"); Excel.OlePropertySet("Visible",true); Workbooks=Excel.OlePropertyGet("Workbooks"); Workbook=Workbooks.OleFunction("Add"); Worksheets=Workbook.OlePropertyGet("Worksheets"); Worksheet=Worksheets.OlePropertyGet("Item",1); // 版面設定 Range=Worksheet.OlePropertyGet("PageSetup"); // 頁首 , 頁尾 Range.OlePropertySet("LeftHeader",""); Range.OlePropertySet("CenterHeader",""); Range.OlePropertySet("RightHeader",""); Range.OlePropertySet("LeftFooter",""); Range.OlePropertySet("CenterFooter","第 &P 頁,共 &N 頁"); Range.OlePropertySet("RightFooter",""); // 設定邊界 , 單位為 point , 一 point = 1 / 72 inches Range.OlePropertySet("LeftMargin",15); Range.OlePropertySet("RightMargin",15); Range.OlePropertySet("TopMargin",15); Range.OlePropertySet("BottomMargin",15); Range.OlePropertySet("HeaderMargin",15); Range.OlePropertySet("FooterMargin",15); Range.OlePropertySet("PrintTitleRows","$2:$2"); // 列印標題列 Range.OlePropertySet("PrintTitleColumns","$A:$A"); // 列印標題欄 Range.OlePropertySet("PrintArea","$A$1:$I$15"); // 列印範圍 Range.OlePropertySet("PrintHeadings",false); // 欄名列號 Range.OlePropertySet("PrintGridlines",false); // 列印格線 Range.OlePropertySet("PrintComments",xlPrintNoComments); // 註解"無" Range.OlePropertySet("PrintQuality",300); // 列印品質 Range.OlePropertySet("CenterHorizontally",true); // 水平置中 Range.OlePropertySet("CenterVertically",false); // 垂直置中 Range.OlePropertySet("Orientation",xlLandscape); // 橫向列印 (xlPortrait 縱向列印) Range.OlePropertySet("Draft",false); // 草稿品質 Range.OlePropertySet("PaperSize",xlPaperA4); // 紙張大小 Range.OlePropertySet("FirstPageNumber",xlAutomatic); // 起始頁碼 Range.OlePropertySet("Order",xlDownThenOver); // 循欄列印 (xlOverThenDown 循列列印) Range.OlePropertySet("BlackAndWhite",False); // 儲存格單色列印 Range.OlePropertySet("Zoom",100); // 縮放比例 // 設定欄高 FColumns = Worksheet.OlePropertyGet("Rows",1); FColumns.OlePropertySet("RowHeight",30); // 設定值 Range=Worksheet.OlePropertyGet("Cells",1,1); Range.OlePropertySet("Value","設定值"); // 設定底色 Range=Worksheet.OlePropertyGet("Cells",2,2); Range.OlePropertySet("Value","設定底色"); Cell=Range.OlePropertyGet("Interior"); Cell.OlePropertySet("ColorIndex",6); // 設定圖樣 Range=Worksheet.OlePropertyGet("Cells",4,2); Range.OlePropertySet("Value","設定圖樣"); Cell=Range.OlePropertyGet("Interior"); Cell.OlePropertySet("Pattern",xlGray50); // 調整欄寬 FColumns = Worksheet.OlePropertyGet("Columns",3); FColumns.OlePropertySet("ColumnWidth",18); // 設定字型&大小 Range=Worksheet.OlePropertyGet("Cells",1,3); Range.OlePropertySet("Value","設定字體大小"); Cell=Range.OlePropertyGet("Font"); Cell.OlePropertySet("Name","細明體"); Cell.OlePropertySet("Size",10); // 設定字體顏色 Range=Worksheet.OlePropertyGet("Cells",3,3); Range.OlePropertySet("Value","設定字體顏色"); Cell=Range.OlePropertyGet("Font"); Cell.OlePropertySet("ColorIndex",3); // 設定粗體字 Range=Worksheet.OlePropertyGet("Cells",5,3); Range.OlePropertySet("Value","設定粗體字"); Cell=Range.OlePropertyGet("Font"); Cell.OlePropertySet("Bold",true); // 設定斜體字 Range=Worksheet.OlePropertyGet("Cells",7,3); Range.OlePropertySet("Value","設定斜體字"); Cell=Range.OlePropertyGet("Font"); Cell.OlePropertySet("Italic",true); // 設定劃底線 Range=Worksheet.OlePropertyGet("Cells",9,3); Range.OlePropertySet("Value","設定劃底線"); Cell=Range.OlePropertyGet("Font"); Cell.OlePropertySet("Underline",xlUnderlineStyleSingle); // 設定向左靠 Range=Worksheet.OlePropertyGet("Cells",11,3); Range.OlePropertySet("Value","設定向左靠"); Range.OlePropertySet("HorizontalAlignment",xlLeft); Range.OlePropertySet("VerticalAlignment",xlCenter); // 設定向中靠 Range=Worksheet.OlePropertyGet("Cells",13,3); Range.OlePropertySet("Value","設定向中靠"); Range.OlePropertySet("HorizontalAlignment",xlCenter); Range.OlePropertySet("VerticalAlignment",xlCenter); // 設定向右靠 Range=Worksheet.OlePropertyGet("Cells",15,3); Range.OlePropertySet("Value","設定向右靠"); Range.OlePropertySet("HorizontalAlignment",xlRight); Range.OlePropertySet("VerticalAlignment",xlCenter); // 畫框 // XlBordersIndex 常數 // xlDiagonalDown , xlDiagonalUp , xlEdgeBottom , xlEdgeLeft , xlEdgeRight , xlEdgeTop , xlInsideHorizontal , xlInsideVertical // XlLineStyle 常數 // xlContinuous , xlDash , xlDashDot , xlDashDotDot , xlDot , xlDouble , xlSlantDashDot , xlLineStyleNone // XlBorderWeight 常數 // xlHairline , xlThin , xlMedium , xlThick // XlColorIndex 常數 // xlColorIndexAutomatic。 用以指定繪圖物件的自動填滿。 // xlColorIndexNone。用以指定無內部填滿。 Range=Worksheet.OlePropertyGet("Cells",2,4); Range.OlePropertySet("Value","設定框線"); Cell=Range.OlePropertyGet("Borders"); Cell.OlePropertySet("LineStyle",xlContinuous); Cell.OlePropertySet("Weight",xlThin); Cell.OlePropertySet("ColorIndex",xlAutomatic); // 畫左框 Range=Worksheet.OlePropertyGet("Cells",4,4); Range.OlePropertySet("Value","設定左框"); Cell=Range.OlePropertyGet("Borders"); Item=Cell.OlePropertyGet("Item",xlEdgeLeft); Item.OlePropertySet("LineStyle",xlContinuous); Item.OlePropertySet("Weight",xlThin); Item.OlePropertySet("ColorIndex",xlAutomatic); // 畫右框 Range=Worksheet.OlePropertyGet("Cells",6,4); Range.OlePropertySet("Value","設定右框"); Cell=Range.OlePropertyGet("Borders"); Item=Cell.OlePropertyGet("Item",xlEdgeRight); Item.OlePropertySet("LineStyle",xlContinuous); Item.OlePropertySet("Weight",xlThin); Item.OlePropertySet("ColorIndex",xlAutomatic); // 畫上框 Range=Worksheet.OlePropertyGet("Cells",8,4); Range.OlePropertySet("Value","設定上框"); Cell=Range.OlePropertyGet("Borders"); Item=Cell.OlePropertyGet("Item",xlEdgeTop); Item.OlePropertySet("LineStyle",xlContinuous); Item.OlePropertySet("Weight",xlThin); Item.OlePropertySet("ColorIndex",xlAutomatic); // 畫下框 Range=Worksheet.OlePropertyGet("Cells",10,4); Range.OlePropertySet("Value","設定下框"); Cell=Range.OlePropertyGet("Borders"); Item=Cell.OlePropertyGet("Item",xlEdgeBottom); Item.OlePropertySet("LineStyle",xlContinuous); Item.OlePropertySet("Weight",xlThin); Item.OlePropertySet("ColorIndex",xlAutomatic); // 設定儲存格格式 - 數字 Range=Worksheet.OlePropertyGet("Cells",1,5); Range.OlePropertySet("Value","1234.5"); Range.OlePropertySet("NumberFormatLocal","#,##0.00"); // 設定直的加總公式, -3 & -1 好像是從加總欄位往上數所得來的 Range=Worksheet.OlePropertyGet("Cells",2,6); Range.OlePropertySet("Value","123"); Range=Worksheet.OlePropertyGet("Cells",3,6); Range.OlePropertySet("Value","234"); Range=Worksheet.OlePropertyGet("Cells",4,6); Range.OlePropertySet("Value","345"); Range=Worksheet.OlePropertyGet("Cells",5,6); Range.OlePropertySet("FormulaR1C1","=SUM(R[-3]C:R[-1]C)"); // 設定橫的加總公式, -3 & -1 好像是從加總欄位往左數所得來的 Range=Worksheet.OlePropertyGet("Cells",2,7); Range.OlePropertySet("Value","234"); Range=Worksheet.OlePropertyGet("Cells",2,8); Range.OlePropertySet("Value","345"); Range=Worksheet.OlePropertyGet("Cells",2,9); Range.OlePropertySet("FormulaR1C1","=SUM(RC[-3]:RC[-1])"); // 設定直跟橫都有的加總公式, 先往上數,再往左邊數 Range=Worksheet.OlePropertyGet("Cells",3,7); Range.OlePropertySet("Value","234"); Range=Worksheet.OlePropertyGet("Cells",4,7); Range.OlePropertySet("Value","234"); Range=Worksheet.OlePropertyGet("Cells",5,8); Range.OlePropertySet("FormulaR1C1","=SUM(R[-3]C[-2]:R[-1]C[-1])");
領航天使
站長


發表:12216
回覆:4186
積分:4084
註冊:2001-07-25

發送簡訊給我
#2 引用回覆 回覆 發表時間:2002-11-05 12:51:29 IP:192.168.xxx.xxx 未訂閱
感謝您發表精彩的 "BCB 轉 EXCEL 大發現" 文章! ~~~Delphi K.Top討論區站長~~~
------
~~~Delphi K.Top討論區站長~~~
denise
一般會員


發表:2
回覆:5
積分:15
註冊:2002-10-09

發送簡訊給我
#3 引用回覆 回覆 發表時間:2002-11-26 17:14:53 IP:211.75.xxx.xxx 未訂閱
又找到了如何新增一個WORKSHEET , 而且可以設定名稱 Worksheet=Worksheets.OleFunction("Add"); // 新增一worksheet Worksheet.OlePropertySet("Name","test2") ; Worksheet=Worksheets.OleFunction("Add"); // 再新增一worksheet Worksheet.OlePropertySet("Name","test1") ;
Shunze
一般會員


發表:0
回覆:1
積分:0
註冊:2002-08-13

發送簡訊給我
#4 引用回覆 回覆 發表時間:2002-12-05 14:15:55 IP:61.13.xxx.xxx 未訂閱
合併CELL Cells=WorkSheet.OlePropertyGet("Cells"); Cell1=Cells.OlePropertyGet("Cells",x1,y1); Cell2=Cells.OlePropertyGet("Cells",x2,y2); Range=WorkSheet.OlePropertyGet("Range",Cell1,Cell2); Range.OleFunction("Merge");
lo
初階會員


發表:8
回覆:69
積分:25
註冊:2003-01-12

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-05-10 12:13:27 IP:211.76.xxx.xxx 未訂閱
請教denise君 各先進 我已完成StringGrid用途.現在想轉excel. 我依denise君說明並copy程式貼入form內轉excelbuttonClick 然後標頭檔加入了 #include "vcl\utilcls.h" #define PG OlepropertyGet #define PS OlepropertySet #define FN OleFunction #define PG OleProcdure 操作後感覺#define有問題.請教denise君 各先進我該如何正確#include #define 還是少了甚麼? 謝謝
ruhtra
一般會員


發表:1
回覆:2
積分:0
註冊:2004-02-21

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-02-22 23:54:49 IP:218.18.xxx.xxx 未訂閱
我完全照您的步驟做 絕大部分的語法都試的出來 但是在對齊這部分 我遇到了問題    // 設定向中靠 Range=Worksheet.OlePropertyGet("Cells",13,3); Range.OlePropertySet("Value","設定向中靠"); Range.OlePropertySet("HorizontalAlignment",xlCenter); Range.OlePropertySet("VerticalAlignment",",xlCenter); BCB5的錯誤訊息 [C Error] Unit1.cpp(107): E2451 Undefined symbol 'xlCenter' [C Error] Unit1.cpp(107): E2285 Could not find a match for 'Variant::OlePropertySet(char *,undefined)' Compile時停在 sysvari.h的 template void Variant::OlePropertySet(const String& name, P1 p1) { TAutoArgs<1> args; args[1] = p1; OlePropertySet(name, static_cast(args)); } 請問各位 我是少加了什麼東西嗎?
wivern
初階會員


發表:31
回覆:63
積分:28
註冊:2002-07-31

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-02-23 10:50:01 IP:61.219.xxx.xxx 未訂閱
試試加上    #include  看看~
引言: 我完全照您的步驟做 絕大部分的語法都試的出來 但是在對齊這部分 我遇到了問題 // 設定向中靠 Range=Worksheet.OlePropertyGet("Cells",13,3); Range.OlePropertySet("Value","設定向中靠"); Range.OlePropertySet("HorizontalAlignment",xlCenter); Range.OlePropertySet("VerticalAlignment",",xlCenter); BCB5的錯誤訊息 [C Error] Unit1.cpp(107): E2451 Undefined symbol 'xlCenter' [C Error] Unit1.cpp(107): E2285 Could not find a match for 'Variant::OlePropertySet(char *,undefined)' Compile時停在 sysvari.h的 template void Variant::OlePropertySet(const String& name, P1 p1) { TAutoArgs<1> args; args[1] = p1; OlePropertySet(name, static_cast(args)); } 請問各位 我是少加了什麼東西嗎?
ruhtra
一般會員


發表:1
回覆:2
積分:0
註冊:2004-02-21

發送簡訊給我
#8 引用回覆 回覆 發表時間:2004-02-23 16:50:07 IP:218.18.xxx.xxx 未訂閱
感謝您 加上  #include 〈excel_2k.h〉 就可以用了 不過我是在打算回覆您的文章時才看到寫的文字 上篇看不到您寫的〈excel_2k.h〉
jackkao2000
一般會員


發表:0
回覆:1
積分:0
註冊:2004-03-04

發送簡訊給我
#9 引用回覆 回覆 發表時間:2004-03-04 09:25:25 IP:61.222.xxx.xxx 未訂閱
請教為什麼我的會出現如下錯誤 [C++ Error] Filectrl.hpp(70): E2015 Ambiguity between 'Windows' and 'Excel_2k::Windows' 我是用BCB6.0 跟XP 我也有#include  期盼您的指教 Thanks and Best regard
Derek馬桶
一般會員


發表:14
回覆:11
積分:5
註冊:2003-02-08

發送簡訊給我
#10 引用回覆 回覆 發表時間:2004-10-08 17:26:50 IP:211.20.xxx.xxx 未訂閱
Dear Denise 大大 and All here 想請教一下,小弟我想寫個轉檔程式,以讀取EXCEL內的資料,然後轉換到SQL中,但拜讀過您的大作之後,卻找不到欄位讀取的指令或函數,不知是否可以請大大再傾囊相授一下,謝謝。 商祈 Derek馬桶
Derek馬桶
一般會員


發表:14
回覆:11
積分:5
註冊:2003-02-08

發送簡訊給我
#11 引用回覆 回覆 發表時間:2004-10-08 17:27:55 IP:211.20.xxx.xxx 未訂閱
Dear Denise 大大 and All here 想請教一下,小弟我想寫個轉檔程式,以讀取EXCEL內的資料,然後轉換到SQL中,但拜讀過您的大作之後,卻找不到欄位讀取的指令或函數,不知是否可以請大大再傾囊相授一下,謝謝。 (是否也可順便告知,這樣的技巧是從哪本書上可以參考,小弟也想拜讀一下,謝謝) 商祈 Derek馬桶
暗黑破壞神
版主


發表:9
回覆:2301
積分:1627
註冊:2004-10-04

發送簡訊給我
#12 引用回覆 回覆 發表時間:2004-10-08 23:07:17 IP:221.169.xxx.xxx 未訂閱
這方面的資料可以在 VBA 上找到。 因為它們也是一種 COM, DCOM. 而 word 要叫 excel 或是其它的程式要這樣叫用。 也都是把這個 COM/DCOM 叫起來。建立通訊管道,再利用 VBA 的函數去操作。 把 excel 想成是一個 server 而你是一個 client 向它下指令就是了。 只是。你必須要知道它的那些 object & method 相對應到 bcb or delphi.... 是什麼 data type。 很謝謝 denise 分享這些實做的經驗。 同樣的經驗也可以用在 word 上。^_^ 我就曾經用 BCB 去叫 word 產生報表。^_^
daniel__lee
高階會員


發表:18
回覆:124
積分:113
註冊:2002-11-10

發送簡訊給我
#13 引用回覆 回覆 發表時間:2004-10-11 16:38:00 IP:61.218.xxx.xxx 未訂閱
to:Denise 大大 大約在兩年前在深度論壇就看到你的這篇作品 但礙於不是自己的作品也不好轉貼..... 反正在這見到你 真好就是了 ^_^ ~ 勿在浮沙上面築高塔 ~
------
~ 勿在浮沙上面築高塔 ~
justdo
高階會員


發表:2
回覆:359
積分:222
註冊:2004-08-17

發送簡訊給我
#14 引用回覆 回覆 發表時間:2004-10-11 20:55:11 IP:221.169.xxx.xxx 未訂閱
在 Borland C++ Builder 4 Unleashed 一書也有看到這樣的用法 由於手邊沒有電子檔,所以我擷取部份的程式碼如下,如果有打錯字還請包涵    
//in TForm1 class:
TCOM_Application m_XLApp; //Excel's Application Object
WorksheetPtr m_Worksheet;
RangePrt m_Cells;
RangePtr m_Columns;    //function
void __fastcall TForm1::button1Click(Tobject *Sender)
{
  //Launch EXCEL
  if (!m_XLApp)
    m_XLApp=CoApplication_::Create();
  //MAKE excel VISIBLE
  m_XLApp->Set_Visible(0, true);
  //Add a Worksheet
  m_XLApp->WorkBooks->Add(xlWBATWorksheet);
  //Get the newly added Worksheet object
  m_Worksheet=m_XLApp->Workbooks->Get_Item(1)->Worksheets->get_Item(1);
  m_Worksheet->Name=WideString("C  Builder Data");
  InsertData();
  HandleRange();
  ChangeColumns();
}    void TForm1::InsertData()
{
  int i;
  for (i=1; i<=10; i  )
    m_Workksheet->Cells->Set__Default(i, 1, i);
  m_Worksheet->Cells->Set_Default(i, 1, "=SUM(A1:A10)");
}    void TForm1::HandleRange()
{
  RangePtr range=m_Worksheet->Get_Range("C1:F25");
  range->Formula="=RAND()";
  range->Columns->Interior->ColorIndex=3;
  range->Borders->LineStyle=xlContinuous;
}    void TForm1::ChangeColumns()
{
  //Get interface to first column
  RangePtr firstCol=m_Worksheet->Columns->get_Default(1);
  firstCol->ColumnWidth=20;
  firstCool->Font->Bold=true;
  firstCool->Font->Italic=true;
  firstCool->Font->Color=clBlue;
}    
上面示範了讀寫資料,請各位先試試看...
系統時間:2024-03-29 23:59:15
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!