标签:
基于excel9.h的excel处理;
#include "excel9.h" #include <iostream> using namespace std; class excel_lwn { public: excel_lwn() { CoInitialize(NULL); if(!app.CreateDispatch(_T("Excel.Application"),NULL)) { /*"启动Excel服务器失败!" */ } f_result = NULL; flag=-1; } ~excel_lwn() { book.SetSaved(TRUE); books.Close(); app.Quit(); } void OpenTable(CString excelName, int table=1) { books=app.GetWorkbooks(); COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); lpDisp = books.Open(excelName, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional); book.AttachDispatch( lpDisp ); sheets=book.GetSheets(); sheet=sheets.GetItem(COleVariant((short)table)); } void GetValue(int i=1,int lie=1) { char c_lie[2]; c_lie[0] =64+ lie; c_lie[1]= ‘\0‘; cs_lie=c_lie; char c_hang[11]; itoa(i,c_hang, 10); cs_hang=c_hang; Range range=sheet.GetRange(COleVariant(cs_lie+cs_hang),COleVariant(cs_lie+cs_hang)); COleVariant rValue= range.GetValue(); isSuccess = rValue.vt; if (rValue.vt ==VT_BSTR ) { //MessageBox(NULL,CString(rValue.bstrVal),TEXT("你好"),MB_ICONINFORMATION|MB_YESNO); cs_result = rValue.bstrVal; flag=1; } if (rValue.vt ==VT_R8 ) { //cout<<rValue.dblVal<<endl; f_result = rValue.dblVal; i_result = f_result; char temp[15]; itoa(f_result,temp,15); cs_result = temp; flag = 0; } } private: _Application app; Workbooks books; _Workbook book; Worksheets sheets; _Worksheet sheet; LPDISPATCH lpDisp; CString cs_lie; CString cs_hang; public: CString cs_result; double f_result; int i_result; int flag;// 0表示是数字,1表示是字符串 bool isSuccess; }; int main(int argc, char *argv[]) { //OnButton2(); excel_lwn my; my.OpenTable("C:\\d.xlsx",1); my.GetValue(1,1); cout<<my.i_result<<endl; my.GetValue(5,5); cout<<my.f_result<<endl; return true; }
标签:
原文地址:http://www.cnblogs.com/lwngreat/p/4508063.html