C++ 操作EXCEL

要在應用程序中控制Excel的運行,首先必須在編制自動化客戶程序時包含Comobj.hpp
#include "Comobj.hpp"

C++ Builder把Excel自動化對象的功能包裝在下面的四個Ole Object Class函數中,應用人員可以很方便地進行調用。
設置對象屬性:void        OlePropertySet(屬性名,參數……);
獲得對象屬性:Variant     OlePropertyGet(屬性名,參數……);
調用對象方法:1) Variant  OleFunction(函數名,參數……);
             2) void     OleProcedure(過程名,參數……);

在程序中可以用宏定義來節省時間:

#define   PG   OlePropertyGet
#define   PS   OlePropertySet
#define   FN   OleFunction
#define   PR   OleProcedure

舉例:
ExcelApp.OlePropertyGet("workbooks").OleFunction("Add");
可寫爲
ExcelApp.PG("workbooks").FN("Add");

C++ Builder中使用OLE控制Excel2000,必須掌握Excel2000的自動化對象及Microsoft Word Visual Basic幫助文件中的關於Excel的對象、方法和屬性。對象是一個Excel元素,屬性是對象的一個特性或操作的一個方面,方法是對象可以進行的動作。
首先定義以下幾個變量:
Variant ExcelApp,Workbook1,Sheet1,Range1;

1、Excel中常用的對象是:Application,Workbooks,Worksheets等。
  
  ★創建應用對象★
       Variant ExcelApp;
       ExcelApp = Variant::CreateObject ("Excel.Application");
       或者
       ExcelApp = CreateOleObject ("Excel.Application");

  ★創建工作簿對象★
       Variant WorkBook1;
       WorkBook1 = ExcelApp.PG("ActiveWorkBook");

  ★創建工作表對象★
       Variant Sheet1;
       Sheet1 = WorkBook1.PG("ActiveSheet");

  ★創建區域對象★
       Variant Range;
       Range = Sheet1.PG("Range","A1:A10");    
       或者使用
       Excel.Exec(PropertyGet("Range")<<"A1:C1").Exec(Procedure("Select"));

2、常用的屬性操作:
  
  ★使Excel程序不可見★
       ExcelApp.PS("Visible", (Variant)false);
        
  ★新建EXCEL文件★
    
    ◎ 新建系統模板的工作簿
       ExcelApp.PG("workbooks").FN("Add")      //默認工作簿
       ExcelApp.PG("workbooks").FN("Add", 1)   //單工作表
       ExcelApp.PG("workbooks").FN("Add", 2)   //圖表  
       ExcelApp.PG("workbooks").FN("Add", 3)   //宏表
       ExcelApp.PG("workbooks").FN("Add", 4)   //國際通用宏表
       ExcelApp.PG("workbooks").FN("Add", 5)   //與默認的相同
       ExcelApp.PG("workbooks").FN("Add", 6)   //工作簿且只有一個表
       或者使用ExcelApp的Exec方法
       Excel.Exec(PropertyGet("Workbooks")).Exec(Procedure("Add"));
    ◎ 新建自己創建的模板的工作簿
       ExcelApp.PG("workbooks").FN("Add", "C:\\Temp\\result.xlt");
回答人的補充   2011-04-08 12:33
★打開工作簿★
       ExcelApp.PG("workbooks").FN("open", "路徑名.xls")  
        
  ★保存工作簿★
       WorkBook1.FN("Save");            //保存工作簿
       WorkBook1.FN("SaveAs", "文件名");//工作簿保存爲,路徑注意用"\\"
        
  ★退出EXCEL★
       ExcelApp.FN ("Quit");
       ExcelApp = Unassigned;
       或者
       ExcelApp.Exec(Procedure("Quit"));
        
  ★操作工作表★
    
    ◎ 選擇選擇工作表中第一個工作表
       Workbook1.PG("Sheets", 1).PR("Select");
       Sheet1 = Workbook1.PG("ActiveSheet");
    
    ◎ 重命名工作表
       Sheet1.PS("Name", "Sheet的新名字");
    
    ◎ 當前工作簿中的工作表總數
       int nSheetCount=Workbook1.PG("Sheets").PG("Count");       
        
  ★操作行和列★
   
    ◎ 獲取當前工作表中有多少行和多少列:
       Sheet1.PG("UsedRange").PG("Columns").PG("Count"); //列數
       Sheet1.PG("UsedRange").PG("Rows").PG("Count");    //行數
    
    ◎ 設置列寬
       ExcelApp.PG("Columns", 1).PS("ColumnWidth", 22);
       或者
       Range = ExcelApp.PG("Cells", 1, 3);
       Range.PS("ColumnWidth", 22);      
    
    ◎ 設置行高
       ExcelApp.PG("Rows", 2).PS("RowHeight", 25);
       或者
       Range = ExcelApp.PG("Cells", 2, 1);        
       Range.PS("RowHeight", 25);     
    
    ◎ 在工作表最前面插入一行
       Sheet1.PG("Rows", 1).PR("Insert");        
    
    ◎ 刪除一行
       ExcelApp.PG("Rows", 2).PR("Delete"); //將第2行刪除

       // 本文作者:ccrun ,如轉載請保證本文檔的完整性,並註明出處。
// 歡迎光臨 C++ Builder 研究 www.ccrun.com
// 摘自: http://www.ccrun.com/doc/go.asp?id=529
        
  ★操作單元格★
  
    ◎ 設置單元格字體
        Sheet1.PG("Cells", 1, 1).PG("Font").PS("Name", "隸書"); //字體
        Sheet1.PG("Cells", 2, 3).PG("Font").PS("size", 28);     //大小
        
    ◎ 設置所選區域字體
       Range.PG("Cells").PG("Font").PS("Size", 28);
       Range.PG("Cells").PG("Font").PS("Color", RGB(0, 0, 255));
       其中參數的設置:
       Font   Name : "隸書"               //字體名稱
              Size : 12                   //字體大小
             Color : RGB(*,*,*)           //顏色
         Underline : true/false           //下劃線
             Italic: true/false           //斜體
    
    ◎ 設置單元格格式爲小數百分比
       Sheet1.PG("Cells", 1, 1).PS("NumberFormatLocal", "0.00%");

    ◎ 設定單元格的垂直對齊方式
       Range = ExcelApp.PG("Cells", 3, 4);
       // 1=靠上 2=居中 3=靠下對齊 4=兩端對齊 5=分散對齊
       Range.PS("VerticalAlignment", 2);       
    
    ◎ 設定單元格的文本爲自動換行
       Range = ExcelApp.PG("Cells", 3, 4);
       Range.PS("WrapText", true);
       
  ★單元格的合併★

    ◎ Range = Sheet1.PG("Range", "A1:A2");          //A1和A2單元格合併
       String strRange = "A" + IntToStr(j) + ":" + "C" + IntToStr(j); //比如:A1:C5
       Range1=Sheet1.PG("Range", strRange.c_str()); //可以用變量控制單元格合併
       Range1.FN("Merge", false);
        
  ★讀寫單元格★

    ◎ 指定單元格賦值
       String strValue = "abcdefg";
       Sheet1.PG("Cells", 3, 6).PS("Value", strValue.c_str());
       Sheet1.PG("Cells", j, 1).PS("Value", "總記錄:" + String(j-6));
       或者使用
       Excel.Exec(PropertyGet("Cells")<<1<<3).Exec(PropertySet("Value")<<15);

    ◎ 所選區域單元格賦值
       Range.PG("Cells").PS("Value", 10);     

    ◎ 所選區域行賦值
       Range.PG("Rows",1).PS("Value", 1234);  

    ◎ 工作表列賦值
       Sheet1.PG("Columns",1).PS("Value", 1234);

    ◎ 讀取取值語句:
       String strValue = Sheet1.PG("Cells", 3, 5).PG("Value");

  ★窗口屬性★
    
    ◎ 顯示屬性
       ExcelApp.PS("Windowstate", 3);       //最大化顯示
              1---------xlNormal            //正常顯示
              2---------xlMinimized         //最小化顯示
              3---------xlMaximized         //最大化顯示

    ◎ 狀態欄屬性
       ExcelApp.PS("StatusBar", "您好,請您稍等。正在查詢!");
       ExcelApp.PS("StatusBar", false);     //還原成默認值

    ◎ 標題屬性:
       ExcelApp.PS("Caption", "查詢系統");
回答人的補充   2011-04-08 12:34
3、操作圖表
    
  ★添加圖表
     
     Variant Chart;  
     Chart = ExcelApp.Exec(PropertyGet("Charts")).Exec(Function("Add"));
     ExcelApp.Exec(PropertySet("Visible") << true);
     Chart.Exec(PropertySet("Type") << -4100);
  
  ★滾動圖表
  
     for(int nRotate=5; nRotate <= 180; nRotate += 5)
     {
          Chart.Exec(PropertySet("Rotation") << nRotate);
     }
     for (int nRotate = 175; nRotate >= 0; nRotate -= 5)
     {
          Chart.Exec(PropertySet("Rotation") << nRotate);
     }
     
另外,爲保證程序能正常運行,需要在程序中判斷目標機器是否安裝了Office;
try
{
    ExcelApp = Variant::CreateObject ("Excel.Application");
}
catch(...)
{
    ShowMessage("運行Excel出錯,請確認安裝了Office");
    return;
}


#include "comobj.hpp"
//---------------------------------------------------------------------------
// 對指定Excel文件中的指定列進行排序
// strExcelFileName : excel文件
// nCol : 指定的列號
// nSortStyle : 1:升序,2:降序
void SortExcelColumn(String strExcelFileName, int nCol, int nSortStyle)
{
    Variant vExcelApp, vWorkbook, vRange;
    vExcelApp = Variant::CreateObject("Excel.Application");
    vExcelApp.OlePropertySet("Visible", false);
    vExcelApp.OlePropertyGet("WorkBooks").OleProcedure("Open", strExcelFileName.c_str());
    vWorkbook = vExcelApp.OlePropertyGet("ActiveWorkbook");
    vExcelApp.OlePropertyGet("Columns", nCol).OleProcedure("Select");
    vExcelApp.OlePropertyGet("ActiveSheet").OlePropertyGet("Cells", 1, nCol).OleProcedure("Select");
    vRange =  vExcelApp.OlePropertyGet("Selection");
    vRange.Exec(Function("Sort")<<vExcelApp.OlePropertyGet("Selection")<<nSortStyle);
    vWorkbook.OleProcedure("Save");
    vWorkbook.OleProcedure("Close");
    vExcelApp.OleFunction("Quit");
    vWorkbook = Unassigned;
    vExcelApp = Unassigned;
    ShowMessage("ok");
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    // 對C:\123\123.xls文件中第一個Sheet的第四列進行升序排序
    SortExcelColumn("C:\\123\\123.xls", 4, 1);
}
//---------------------------------------------------------------------------











在C++ Builder中用Ole控制Excel表
原文:在C++ Builder中用Ole控制Excel表
 

  筆者在實際工作中經常用Excel表做數據報表,大多數表格的數據都要從數據庫中讀取,這樣我就用C++Builder做了一個報表程序,方便了很多,現在把它共享給C++Builder愛好者們,就算爲豐富C++Builder的文檔資料做點事情吧。

  首先把Excel報表文件保存到一個指定目錄下,最好放在可執行程序的子目錄下,作爲模板文件。可以把報表標題、表頭等設置好。這裏是保存在trpt子目錄下。

  然後建一個report目錄,作爲報表目標文件夾,存放填好數據的報表,可以由用戶直接操作。

  首先確定在你的機器中裝有Office。這裏一Office2000爲例。

  在C++Builder中新建一個工程,在窗體Form1上面放一個兩個按鈕SaveButton和ReadButton,分別用來保存數據到Excel表和顯示剛剛保存的Excel表。

  在SaveButton按鈕的單擊事件中把從數據庫中取到的數據放入到指定的Excel表中並將改文件拷貝到report目錄下。在ReadButto按鈕的單擊事件中顯示report目錄下的報表文件,方便用戶修改和另外保存。

  在Form1.h頭文件中定義幾個變量:

private:
Variant Ex,Wb,Sheet,ERange,EBorders; 

  並在文件頭中包含如下語句:

#include "Excel_2K_SRVR.h"
#include  

  在Form1.cpp的文件頭中加入

#pragma link "Excel_2K_SRVR" 

  主要代碼如下:

void __fastcall TForm1:: SaveButtonClick(TObject *Sender)
{
try
{
SaveButton->Enabled = false;
ReadButton->Enabled = false;//使兩個按鈕無效

file://取報表文件CardSend.xls的完整目錄名
AnsiString ExcelFileName = GetCurrentDir()+"\\trpt\\table.xls"; 

if(!FileExists(ExcelFileName))
{
Application->MessageBox("報表模板文件不存在,無法打開!",
"錯誤",MB_ICONSTOP|MB_OK);
return;
}
file://建立Excel的Ole對象Ex
try
{
Ex = Variant::CreateObject("Excel.Application");
}
catch(...)
{
Application->MessageBox("無法啓動Excel","錯誤",MB_ICONSTOP|MB_OK);
return;
}
file://設置Excel爲不可見
Ex.OlePropertySet("Visible",false);
file://打開指定的Excel報表文件。報表文件中最好設定只有一個Sheet。
Ex.OlePropertyGet("WorkBooks").OleProcedure("Open",ExcelFileName.c_str());
Wb = Ex.OlePropertyGet("ActiveWorkBook");
Sheet = Wb.OlePropertyGet("ActiveSheet");//獲得當前默認的Sheet

file://清空Excel表,這裏是用循環清空到第300行。對於一般的表格已經足夠了。
AnsiString strRowTemp; 
AnsiString strRange;
int iCols,iRows;//記錄列數和行數

/*從第三行開始,到第300行止。一般第一行是表標題,第二行是副標題或者製表日期。*/
for(iRows=3;iRows<300;iRows++)
{ file://假設只有6列。
for (iCols = 1;iCols < 7; iCols++)
{
file://清空行
Sheet.OlePropertyGet("Cells",iRows,iCols).OlePropertySet("Value","");
}
file://去掉表格邊框
strRange = "A"+IntToStr(iRows)+":F"+IntToStr(iRows);//獲取操作範圍
ERange = Sheet.OlePropertyGet("Range",strRange.c_str());
EBorders = ERange.OlePropertyGet("Borders");//獲取邊框對象
EBorders.OlePropertySet("linestyle",xlNone);
}

AnsiString strPtrDate; file://存放當前日期,作爲製表日期
DateSeparator = '-';
ShortDateFormat = "yyyy/m/d";//設置爲年/月/日格式

strPtrDate = DateToStr(Date());//取當前日期

AnsiString strYear = strPtrDate.SubString(1,4);
strPtrDate = strPtrDate.SubString(6,strPtrDate.Length()-5);
AnsiString strMonth = strPtrDate.SubString(1,strPtrDate.Pos("-")-1);
AnsiString strDay = 
strPtrDate.SubString(strPtrDate.Pos("-")+1,
strPtrDate.Length()-strPtrDate.Pos("-"));
strPtrDate = strYear+"年"+strMonth+"月"+strDay+"日";

AnsiString strData = "報表標題";//報表標題
file://將報表標題置於第一行第一列。在此之前,應將報表文件的標題格式設定好。
Sheet.OlePropertyGet("Cells",1,1).OlePropertySet("Value",
strData.c_str());
file://將製表日期置於表格第二行的右側。
Sheet.OlePropertyGet("Cells",2,5).OlePropertySet("Value",
strPtrDate.c_str());

iRows = 3;//在第三行放置表格的列名
Sheet.OlePropertyGet("Cells",iRows,1).OlePropertySet("Value","列名1");
Sheet.OlePropertyGet("Cells",iRows,2).OlePropertySet("Value","列名2");
Sheet.OlePropertyGet("Cells",iRows,3).OlePropertySet("Value","列名3");
Sheet.OlePropertyGet("Cells",iRows,4).OlePropertySet("Value","列名4");
Sheet.OlePropertyGet("Cells",iRows,5).OlePropertySet("Value","列名5");
Sheet.OlePropertyGet("Cells",iRows,6).OlePropertySet("Value","列名6");
file://畫表格邊框,在A3:F3之間取範圍
strRange = "A"+IntToStr(iRows)+":F"+IntToStr(iRows);
ERange = Sheet.OlePropertyGet("Range",strRange.c_str());
EBorders = ERange.OlePropertyGet("Borders");
EBorders.OlePropertySet("linestyle",xlContinuous);
EBorders.OlePropertySet("weight",xlThin);
EBorders.OlePropertySet("colorindex",xlAutomatic);
iRows++;
file://從數據庫中取數據(略),假設數據集放入Query1中。
Query1->Open();//打開數據集
file://循環取數
while(!Query1->Eof)

file://循環取字段的數據放到Excel表對應的行列中
for(iCols=1;iCols<7;iCols++)

strRowTemp = Query1->Fields->Fields[iCols-1]->AsString;
Sheet.OlePropertyGet("Cells",iRows,iCols).OlePropertySet("Value",
strRowTemp.c_str());
}
file://畫該行的表格邊框
strRange = "A"+IntToStr(iRows)+":F"+IntToStr(iRows);
ERange = Sheet.OlePropertyGet("Range",strRange.c_str());
EBorders = ERange.OlePropertyGet("Borders");
EBorders.OlePropertySet("linestyle",xlContinuous);
EBorders.OlePropertySet("weight",xlThin);
EBorders.OlePropertySet("colorindex",xlAutomatic);
iRows++;
Query1->Next();
}//while結束

Wb.OleProcedure("Save");//保存表格
Wb.OleProcedure("Close");關閉表格
Ex.OleFunction("Quit");退出Excel
file://定義目標文件名
AnsiString DestinationFile =
GetCurrentDir()+"\\report\\table.xls";
file://將剛剛修改的Excel表格文件table.xls拷貝到report目錄下
if(!CopyFile(ExcelFileName.c_str(),DestinationFile.c_str(),false))
{
Application->MessageBox("複製文件操作失敗,Excel文件可能正在使用中!",
"錯誤",MB_ICONSTOP|MB_OK);
return;
}
Application->MessageBox("成功完成報表保存!\n可以按\'打開Excel文件\'
按鈕進行報表工作","提示",MB_ICONINFORMATION|MB_OK);

SaveButton ->Enabled = true;
ReadButton ->Enabled=true;
}//try結束
catch(...)
{
Application->MessageBox("操作Excel表格失敗!",
"錯誤",MB_ICONSTOP|MB_OK);
Wb.OleProcedure("Close");
Ex.OleFunction("Quit");
SaveButton ->Enabled = true;
ReadButton ->Enabled=false;
}


  至此,完成報表數據的寫入工作。如果要對完成的Excel表進行操作,可以點擊"打開Excel表文件按鈕"(ReadButton),進行修改,保存,打印等操作。ReadButton的單擊事件如下實現:

void __fastcall TForm1:: ReadButtonClick(TObject *Sender)
{
try
{
file://指定report目錄下的報表文件用於用戶操作
AnsiString ExcelFileName = 
GetCurrentDir();+"\\report\\table.xls";

if(!FileExists(ExcelFileName))
{
Application->MessageBox("Excel表文件不存在,無法打開!",
"錯誤",MB_ICONSTOP|MB_OK);
return;
}

try
{
Ex = Variant::CreateObject("Excel.Application");
}
catch(...)
{
Application->MessageBox("無法啓動Excel","錯誤",MB_ICONSTOP|MB_OK);
return;
}
file://使Excel可見
Ex.OlePropertySet("Visible",true);
file://打開Excel表格文件Table.xls
Ex.OlePropertyGet("WorkBooks").OleProcedure("Open",ExcelFileName.c_str());
}
catch(...)
{
Application->MessageBox("操作Excel表格錯誤!","錯誤",MB_ICONSTOP|MB_OK);
Ex.OleFunction("Quit");
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章