#ifndef EXCEL_OP_H #define EXCEL_OP_H #include <vcl.h> class ExcelOp { Variant s_vExcelApp,s_vSheet; bool s_bOpen; int s_nRowCount,s_nColCount; protected: Variant GetCell(int row,int col) { if(s_bOpen) { return s_vSheet.OlePropertyGet("Cells",row,col).OlePropertyGet("Value"); } return Null; } void SetCell(int row,int col,Variant &var) { if(!s_bOpen) { return ; } String temp=var; s_vSheet.OlePropertyGet("Cells",row,col).OlePropertySet("Value",temp.c_str()); } int GetRowCount() { if(!s_bOpen) return 0; int i=1; String strValue=Cells[i][1]; if(!strValue.IsEmpty()) { do { strValue=Cells[++i][1]; }while(!strValue.IsEmpty()); --i; } s_nRowCount=i; return s_nRowCount; } int GetColCount() { if(!s_bOpen) return 0; int i=1; String strValue=Cells[1][i]; if(!strValue.IsEmpty()) { do { strValue=Cells[1][++i]; }while(!strValue.IsEmpty()); --i; } s_nColCount=i; return s_nColCount; } void SetColumnWidth(int col,int width) { if(s_bOpen) s_vExcelApp.OlePropertyGet("Columns",col).OlePropertySet("ColumnWidth",width); } int GetColumnWidth(int col) { if(s_bOpen) return s_vExcelApp.OlePropertyGet("Columns",col).OlePropertyGet("ColumnWidth"); return 0; } public: const enum Op { Create_Op, Load_Op }; ExcelOp():s_vExcelApp(Null()), s_vSheet(Null()), s_bOpen(false), s_nRowCount(0), s_nColCount(0) {} ~ExcelOp() { try { if(!s_vExcelApp.IsNull()) s_vExcelApp.OleFunction("Quit"); s_vSheet=Unassigned; s_vExcelApp=Unassigned; } catch(...) {} } void Close() { if(s_bOpen) { s_vExcelApp.OlePropertyGet("ActiveWorkbook").OleFunction("Close"); s_bOpen=false; } } void Save() { if(s_bOpen) { s_vExcelApp.OlePropertyGet("ActiveWorkbook").OleFunction("Save"); } } void Save(const char* lpszFileName) { if(s_bOpen) { s_vExcelApp.OlePropertyGet("ActiveWorkbook").OleFunction("SaveAs",lpszFileName); } } bool Open(const char* szFileName,const Op op) { if(op!=Create_Op) { if(!FileExists(szFileName)) return false; } Close(); do { try { s_vExcelApp=Variant::CreateObject("Excel.Application"); } catch(...) { Application->MessageBoxA("CreateObject出错,请确认本机是否安装了MS-EXCEL。","错误",MB_ICONINFORMATION); break; } try { s_vExcelApp.OlePropertySet("Visible", false); if(op==Load_Op) { s_vExcelApp.OlePropertyGet("Workbooks").OleProcedure("Open",szFileName); // 工作表 s_vSheet=s_vExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertyGet("ActiveSheet"); } else if(op==Create_Op) { s_vExcelApp.OlePropertyGet("Workbooks").OleFunction("Add", 1); // 工作表 s_vSheet=s_vExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertyGet("Sheets", 1); } } catch(...) { break; } s_bOpen=true; }while(0); return s_bOpen; } void Select() { if(!s_bOpen) return; s_vSheet.OleProcedure("Select"); s_vSheet.OlePropertyGet("Cells").OleProcedure("Select"); } void SetSelectTextFormat() { if(s_bOpen) s_vExcelApp.OlePropertyGet("Selection").OlePropertySet("NumberFormatLocal", "@"); } void SetCellFont(int row,int col,int nFontSize,const char *szFontName,const char* szFontStyle) { if(s_bOpen) { s_vSheet.OlePropertyGet("Cells",row,col).OlePropertyGet("Font").OlePropertySet("Size",nFontSize); s_vSheet.OlePropertyGet("Cells",row,col).OlePropertyGet("Font").OlePropertySet("Name",szFontName); s_vSheet.OlePropertyGet("Cells",row,col).OlePropertyGet("Font").OlePropertySet("FontStyle",szFontStyle); } } __property Variant Cells[int row][int col]={read=GetCell,write=SetCell}; __property int RowCount={read=GetRowCount}; __property int ColCount={read=GetColCount}; __property int ColumnWidth[int col]={read=GetColumnWidth,write=SetColumnWidth}; }; #endif