全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:19305
推到 Plurk!
推到 Facebook!

BCB控制Excel彙整

 
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-04-30 10:43:26 IP:118.169.xxx.xxx 訂閱
小弟看了很多篇的文章,發現
http://www.programmer-club.com.tw/pc2020v5/forum/ShowSameTitleN.asp?board_pc2020=cb&id=13074&type_pc2020=sametitleLevel-2
這篇整理的很仔細,而且可以用

首先要 #include < ComObj.hpp >
若是遇上定義的問題,例如:xlLeft 則要 #include < excel_2k.h >

但假如遇上ambiguity的問題則自己去定義
xlLeft的數值, const int xlLeft = -4131;
定義在下面連結有說明
http://delphi.ktop.com.tw/board.php?cid=168&fid=913&tid=40545


[原文內容]
前陣子正好有用到BCB 來讀/存 Excel 檔, 加上這個問題好像滿多人在問的, 所以整理一下其他高手在本站的留言及經驗, 供大家參考一下.
//---------------------------------------------------------------------------
// 一些會用到的總體變數, 可以定義在 .h 檔內.
Variant Excel,Workbooks,Workbook;
Variant Worksheets,Worksheet, Range, Cell, FColumns, Format, Item;
//---------------------------------------------------------------------------
//建立一個新的Excel 檔.
void __fastcall TForm1::ButtonNewFileClick(TObject *Sender)
{
// 建立一個可見的 Excel 物件
Excel=CreateOleObject("Excel.Application");
Excel.OlePropertySet("Visible", true); // 設為 false 則不可見
bIsCreated=true;

// 產生 workbooks 跟 workbook 物件.
Workbooks=Excel.OlePropertyGet("Workbooks");
Workbook=Workbooks.OleFunction("Add");

// 產生 worksheets 跟 worksheet 物件.
Worksheets=Workbook.OlePropertyGet("Worksheets");
Worksheet=Worksheets.OlePropertyGet("Item", 1);
}
//---------------------------------------------------------------------------
// 開啟一個已存在的 Excel 檔
void __fastcall TForm1::ButtonOpenFileClick(TObject *Sender)
{
// 若先前尚未建立 Excel 物件, 則建立之.
if (!bIsCreated)
{
// 建立一個可見的 Excel 物件
Excel=CreateOleObject("Excel.Application");
Excel.OlePropertySet("Visible", true);
}

// 開啟某個已存在的 workbooks 跟 workbook.
Workbooks=Excel.OlePropertyGet("Workbooks");
Workbook=Workbooks.OleFunction("Open", "d:\\test1.xls");

// 將該Excel 檔的 Worksheets 設給變數 Worksheets.
Worksheets=Workbook.OlePropertyGet("Worksheets");
}
//---------------------------------------------------------------------------
// 將開啟中的Excel 存成檔案.
void __fastcall TForm1::ButtonSaveFileClick(TObject *Sender)
{
// 將目前開啟中的Excel 存成檔案.
Workbook.OleFunction("SaveAs", "d:\\test2.xls");
}

//---------------------------------------------------------------------------
// 關閉 Excel 程式.
void __fastcall TForm1::ButtonCloseFileClick(TObject *Sender)
{
Excel.OleFunction("Quit");
bIsCreated=false;
}
//---------------------------------------------------------------------------
// 新增一個 sheet.
void __fastcall TForm1::ButtonNewSheetClick(TObject *Sender)
{
Worksheet=Worksheets.OleFunction("Add");
}
//---------------------------------------------------------------------------
// 刪除一個 sheet.
void __fastcall TForm1::ButtonDeleteSheetClick(TObject *Sender)
{
// 取得第2 個 worksheet, 並刪除之.
Worksheet=Worksheets.OlePropertyGet("Item", 2);
Worksheet.OlePropertyGet("Delete");
}
//---------------------------------------------------------------------------
// 重新替 sheet 命名.
void __fastcall TForm1::ButtonRenameSheetClick(TObject *Sender)
{
// 最得第1 個 worksheet, 並將之命名為 CrazyStar.
Worksheet=Worksheets.OlePropertyGet("Item", 1);
Worksheet.OlePropertySet("Name", "CrazyStar");
}
//---------------------------------------------------------------------------
// 設定某個sheet 為作用中.
void __fastcall TForm1::ButtonActivateSheetClick(TObject *Sender)
{
// 設定第二個 sheet 為 active sheet.
Worksheet=Worksheets.OlePropertyGet("Item", 2);
Worksheet.OlePropertyGet("Activate");
}
//---------------------------------------------------------------------------
// 設定Excel 檔的版面格式
void __fastcall TForm1::ButtonSetPageClick(TObject *Sender)
{
// 將該Excel 檔的 Worksheets 設給變數 Worksheets.
Worksheets=Workbook.OlePropertyGet("Worksheets");

// 將 Worksheets 的第2 個 worksheet 設給變數 Worksheet.
Worksheet=Worksheets.OlePropertyGet("Item", 2);

// 版面設定
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); // 縮放比例
}
//---------------------------------------------------------------------------
// 設定 cell 的格式
void __fastcall TForm1::ButtonSetGridFormat1Click(TObject *Sender)
{
// 將該Excel 檔的 Worksheets 設給變數 Worksheets.
Worksheets=Workbook.OlePropertyGet("Worksheets");

// 將 Worksheets 的第2 個 worksheet 設給變數 Worksheet.
Worksheet=Worksheets.OlePropertyGet("Item", 2);

// 設定欄高
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);
}
void __fastcall TForm1::ButtonSetGridFormat2Click(TObject *Sender)
{
// 設定劃底線
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);

// 畫框
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);
}
void __fastcall TForm1::ButtonSetGridFormat3Click(...)
{
// 畫上框
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])");
}
//---------------------------------------------------------------------------
// 寫入某值到 Excel 的某格中.
void __fastcall TForm1::ButtonSetValueClick(TObject *Sender)
{
// 將該Excel 檔的 Worksheets 設給變數 Worksheets.
Worksheets=Workbook.OlePropertyGet("Worksheets");
// 將 Worksheets 的第2 個 worksheet 設給變數 Worksheet.
Worksheet=Worksheets.OlePropertyGet("Item", 2);

// 取得第 (2,3) 個 cell.
Cell=Worksheet.OlePropertyGet("Cells", 2, 3);
Cell.OlePropertySet("Value", "CrazyStar"); // 設定它的文字內容為 "CrazyStar".
}
//---------------------------------------------------------------------------
// 讀取Excel 某格的值到程式中.
void __fastcall TForm1::ButtonGetValueClick(TObject *Sender)
{
Variant Var;
int A;
float B;
String C;

// 將該Excel 檔的 Worksheets 設給變數 Worksheets.
Worksheets=Workbook.OlePropertyGet("Worksheets");
// 將 Worksheets 的第2 個 worksheet 設給變數 Worksheet.
Worksheet=Worksheets.OlePropertyGet("Item", 2);
// 取得第 (2,3) 個 cell.
Cell=Worksheet.OlePropertyGet("Cells", 2, 3);

// 讀取該 cell 的值到變數 Var 中
Var=Cell.OlePropertyGet("Value");
// 轉換取得的資料.
if (String(Var)!="")
{
A=StrToInt(Var);
B=StrTofloat(Var);
C=String(Var);
}
}

// 取得 sheet 的個數.
int count=Workbook.OlePropertyGet("Sheets").OlePropertyGet("Count");

// 新增一個 sheet 到 某個sheet 後面. (這邊例子為插入到第4 個sheet 後面).
Worksheet=Worksheets.OlePropertyGet("Item", 4);
Workbook.OlePropertyGet("Sheets").OleProcedure("Add", Worksheet.NoParam(), Worksheet);

reference:
http://www.itepub.net/html/kaifawendang/C__/OLE_COM_DLL_XML/2006/0429/6996.html




編輯記錄
taishyang 重新編輯於 2009-04-30 10:44:30, 註解 無‧
taishyang 重新編輯於 2009-04-30 10:45:24, 註解 無‧
eeric7777
一般會員


發表:0
回覆:1
積分:0
註冊:2007-05-03

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-07-07 00:45:55 IP:123.110.xxx.xxx 訂閱

補充一下,如何設定Cell 背景色 (使用 Color 屬性, 不用 ColorIndex)

#define PG OlePropertyGet
#define PS OlePropertySet

Variant clBackgnd = RGB(255,0,0); // 設定背景為紅色
WorkSheet.PG("Cells", iRow, iCol).PG("Interior").PS("Color",clBackgnd);
編輯記錄
eeric7777 重新編輯於 2009-07-07 00:48:20, 註解 無‧
系統時間:2024-04-26 23:38:21
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!