/******************************************************************************
*
* 这是上面VBAReportDemo的对应程序
* 演示使用VC操作Excel 2003
* 功能:设置单元格,风格,冻结窗格。以及如何将对应的VB代码翻译成VC
*
* 时间:2007-04-16 09:31
* 作者:高宏伟(DukeJoe)
* QQ:21807822
* Blog:http://dukejoe.yeah.net
* 注释:本代码可单独研究,但最好和上面的VBAReportDemo对应看比较好
*
* 开发环境:Visual Studio.NET 2003
* 操作系统:Windows XP Home Edition Service Pack 2
*
*
*****************************************************************************/
#include <iostream>
using namespace std ;
#import "C:\Program Files\Common Files\Microsoft Shared\OFFICE11\mso.dll" rename("RGB", "MSRGB")
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" raw_interfaces_only, \
rename("Reference", "ignorethis"), rename("VBE", "JOEVBE")
#import "C:\Program Files\Microsoft Office\OFFICE11\excel.exe" exclude("IFont", "IPicture") \
rename("RGB", "ignorethis"), rename("DialogBox", "ignorethis"), rename("VBE", "JOEVBE"), \
rename("ReplaceText", "JOEReplaceText"), rename("CopyFile","JOECopyFile"), \
rename("FindText", "JOEFindText"), rename("NoPrompt", "JOENoPrompt")
using namespace Office;
using namespace VBIDE;
using namespace Excel ;
int ExportExcelFile() ;
int main(int argc, char* argv[])
{
if(FAILED(::CoInitialize(NULL)))
return 1 ;
ExportExcelFile() ;
::CoUninitialize();
return 0;
}
int ExportExcelFile()
{
_ApplicationPtr pApplication = NULL ;
_WorkbookPtr pThisWorkbook = NULL ;
_WorksheetPtr pThisWorksheet = NULL ;
SheetsPtr pThisSheets = NULL ;
RangePtr pThisRange = NULL ;
_variant_t vt ;
Excel::XlFileFormat vFileFormat ;
Excel::XlSaveAsAccessMode vSaveAsAccessMode ;
Excel::XlSaveConflictResolution vSaveConflictResolution ;
pApplication.CreateInstance("Excel.Application");
pApplication->PutVisible (0,VARIANT_TRUE);
pThisWorkbook = pApplication->GetWorkbooks()->Add() ;
pThisSheets = pThisWorkbook->GetWorksheets() ;
pThisWorksheet = pThisSheets->GetItem((short)1);
// 设置整个sheet的填充色为白色
pThisWorksheet->GetCells()->GetInterior()->PutColor((long)0xFFFFFF);
pThisWorksheet->GetCells()->ClearContents() ;
pThisRange = pThisWorksheet->GetRange("A1:C5") ;
pThisRange->ClearFormats() ;
// 如果有不会的,可以在debug文件夹的excel.tlh里找找
//pThisRange->GetItem(1,1) ;
pThisRange->PutItem((long)1, (long)1, _variant_t("哈尔滨市平房区")) ;
pThisRange->PutItem((long)1, (long)2, _variant_t("高宏伟")) ;
pThisRange->PutItem((long)1, (long)3, _variant_t("=B1+B2")) ;
pThisRange->PutItem((long)2, (long)1, _variant_t("1")) ;
pThisRange->PutItem((long)3, (long)1, _variant_t("2")) ;
pThisRange->PutItem((long)4, (long)1, _variant_t("3")) ;
pThisRange->PutItem((long)5, (long)1, _variant_t("4")) ;
// 为Range的四周和内部加上边框
pThisRange->GetBorders()->GetItem(xlEdgeLeft)->PutLineStyle((long)xlContinuous) ;
pThisRange->GetBorders()->GetItem(xlEdgeTop)->PutLineStyle((long)xlContinuous) ;
pThisRange->GetBorders()->GetItem(xlEdgeRight)->PutLineStyle((long)xlContinuous) ;
pThisRange->GetBorders()->GetItem(xlEdgeBottom)->PutLineStyle((long)xlContinuous) ;
pThisRange->GetBorders()->GetItem(xlInsideHorizontal)->PutLineStyle((long)xlContinuous) ;
pThisRange->GetBorders()->GetItem(xlInsideVertical)->PutLineStyle((long)xlContinuous);
// 设置第一行的格式(背景色、粗体、颜色、列宽)
pThisRange->GetRange("A1:C1")->GetInterior()->ColorIndex = (long)47 ;
pThisRange->GetRange("A1:C1")->GetInterior()->Pattern = (long)xlPatternSolid ;
pThisRange->GetRange("A1:C1")->GetFont()->ColorIndex = (long)6 ;
pThisRange->GetRange("A1:C1")->GetFont()->Bold = (bool)TRUE ;
pThisRange->GetEntireColumn()->ColumnWidth = (long)18.63 ;
pThisRange->GetRange("A2:C5")->GetInterior()->ColorIndex = (long)16 ;
pThisRange->GetRange("A2:C5")->GetInterior()->Pattern = (long)xlPatternSolid ;
pThisRange->GetRange("A2:C5")->GetFont()->ColorIndex = (long)2 ;
// 冻结窗格
pApplication->ActiveWindow->FreezePanes = (bool)FALSE ;
pApplication->Range["A2"]->Select() ;
pApplication->ActiveWindow->FreezePanes = (bool)TRUE ;
// 存盘退出
vSaveAsAccessMode = xlNoChange ;
vFileFormat = xlWorkbookNormal ;
vSaveConflictResolution = xlLocalSessionChanges ;
pThisWorkbook->SaveAs(_variant_t("D:\\joe.xls"), (long)vFileFormat, _variant_t(""),_variant_t(""), _variant_t((bool)false),
_variant_t((bool)false), vSaveAsAccessMode, (long)vSaveConflictResolution, _variant_t((bool)false)) ;
pThisWorkbook->Close();
pApplication->Quit();
return 0 ;
}