基于excel9.h的excel处理

时间:2023-01-10 11:29:26

基于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=-;
}
~excel_lwn()
{
book.SetSaved(TRUE);
books.Close();
app.Quit();
} void OpenTable(CString excelName, int table=)
{
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=,int lie=)
{
char c_lie[];
c_lie[] =+ lie;
c_lie[]= '\0';
cs_lie=c_lie; char c_hang[];
itoa(i,c_hang, );
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=;
}
if (rValue.vt ==VT_R8 )
{
//cout<<rValue.dblVal<<endl;
f_result = rValue.dblVal;
i_result = f_result;
char temp[];
itoa(f_result,temp,);
cs_result = temp;
flag = ;
}
}
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",);
my.GetValue(,);
cout<<my.i_result<<endl;
my.GetValue(,);
cout<<my.f_result<<endl;
return true;
}

相关文件:http://pan.baidu.com/s/1hqfBn72

c#中关于excel的处理: http://www.codeproject.com/Tips/813187/Csharp-Read-and-write-Excel-xls-and-xlsx-files-con