InsertExcel.cpp
上传用户:haiweijt
上传日期:2018-02-23
资源大小:8195k
文件大小:9k
- #include "InsertExcel.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <ole2.h>
- #ifdef UNICODE
- #define _T(x) L##x
- #else
- #define _T(x) x
- #endif
- InsertExcel::InsertExcel()
- {
- m_Offset=0;
- m_str=0;
- m_zhi=0;//判断执行了命令
- m_zhizhen=0;//用于判断一条记录占多行
- filename.vt = VT_BSTR;
- filename.bstrVal = SysAllocString(L"c:\test.xls");
- flag=1;
- m_flag=10;
- for(int k=0;k<420;k++)
- { for(int t=0;t<6;t++)
- {
- strcpy(v_str[k][t]," ");
- }
-
- }
- }
- InsertExcel::~InsertExcel()
- {
- //pXlRange->Release();
- AutoWrap(DISPATCH_METHOD, NULL, pXlSheet, L"SaveAs", 1, filename);
- pXlSheet->Release();
- pXlBook->Release();
- pXlBooks->Release();
- pXlApp->Release();
- VariantClear(&arr);
- // 注销COM库
- CoUninitialize();
- }
- void InsertExcel::Insert(char* buf, int size)
- {
- if (size == 0)
- return;
- unsigned char tmpc = *(unsigned char *)buf;
- switch(tmpc)
- {
- case 8:
- if (m_Offset > 0)
- m_Offset --;
- break;
- case 13:
- m_Buffer[m_Offset++] = 'x0d';
- break;
- case 10:
- m_Buffer[m_Offset++] = 'x0a';
- m_Buffer[m_Offset] = ' ';
- InsertInto(m_Buffer);
- //dwStr(m_Buffer);
- strcpy(m_Buffer,"");
- m_Offset = 0;
- break;
- default:
- m_Buffer[m_Offset++] = buf[0];
- break;
- }
- Insert(buf+1, size-1);
- }
- int InsertExcel::InsertInto(char* str)
- {
- char ** p;
- if(flag==1)
- {
- // 初始化COM库
- CoInitialize(NULL);
- // 获得EXCEL的CLSID
- CLSID clsid;
- HRESULT hr = CLSIDFromProgID(L"Excel.Application", &clsid);
- if(FAILED(hr)) {
- ::MessageBox(NULL, _T("CLSIDFromProgID() 函数调用失败!"),_T("错误"), 0x10010);
- return -1;
- }
- // 创建实例
- //IDispatch *pXlApp;
- hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void **)&pXlApp);
- if(FAILED(hr)) {
- ::MessageBox(NULL, _T("请检查是否已经安装EXCEL!"), _T("错误"), 0x10010);
- return -2;
- }
- // 显示,将Application.Visible属性置1
- VARIANT x;
- x.vt = VT_I4;
- x.lVal = 1;
- AutoWrap(DISPATCH_PROPERTYPUT, NULL, pXlApp, L"Visible", 1, x);
- // 获取Workbooks集合
- //IDispatch *pXlBooks;
- {
- VARIANT result;
- VariantInit(&result);
- AutoWrap(DISPATCH_PROPERTYGET, &result, pXlApp, L"Workbooks", 0);
- pXlBooks = result.pdispVal;
- }
- // 调用Workbooks.Add()方法,创建一个新的Workbook
- //IDispatch *pXlBook;
- {
- VARIANT result;
- VariantInit(&result);
- AutoWrap(DISPATCH_PROPERTYGET, &result, pXlBooks, L"Add", 0);
- pXlBook = result.pdispVal;
- }
- // 从Application.ActiveSheet属性获得Worksheet对象
- //IDispatch *pXlSheet;
- {
- VARIANT result;
- VariantInit(&result);
- AutoWrap(DISPATCH_PROPERTYGET, &result, pXlApp, L"ActiveSheet", 0);
- pXlSheet = result.pdispVal;
- }
- flag=0;
- }
- // 创建一个15x15的数组,用于填充表格
- //VARIANT arr;
- WCHAR szTmp[170];
- arr.vt = VT_ARRAY | VT_VARIANT;
- SAFEARRAYBOUND sab[2];
- sab[0].lLbound = 1; sab[0].cElements = 420;
- sab[1].lLbound = 1; sab[1].cElements = 15;
- arr.parray = SafeArrayCreate(VT_VARIANT, 2, sab);
-
- if(str[0]==13)
- {
- return 1;
- }else{
- p=dwStr(str);
- if((str[0]=='T')&&(str[9]=='h')&&(str[4]=='S'))//每次执行一个命令,Excel会空
- { //出一行显示(“>show ip int br”)所执行的命令。
- char Field[30];
- int ps=0;
- int fi=10;
- while(str[fi]!=13)
- {
- Field[ps++]=str[fi++];
- }
- Field[ps]=' ';
- strcpy(v_str[m_str][0],Field);
- ps=1;
- while(ps<6)
- {
- strcpy(v_str[m_str][ps]," ");
- ps++;
- }
- }else
- {
- int pt=0;
- int pflag=0;
- while(pt<6)
- {
- if(strcmp(p[pt],"")==0)
- pflag++;
- pt++;
- }
- if(pflag==6) return 1;
-
- for(int ms=0;ms<6;ms++)
- {
- strcpy(v_str[m_str][ms],p[ms]);
- }
- }
- m_str++;
- for(int i=1; i<=420; i++) {
- for(int j=1; j<=6; j++) {
- VARIANT tmp;
- tmp.vt = VT_BSTR;
- wsprintfW(szTmp,L"%S",v_str[i-1][j-1]);
- tmp.bstrVal = SysAllocString(szTmp);
- // 添加数据到数组中
- long indices[] = {i,j};
- SafeArrayPutElement(arr.parray, indices, (void *)&tmp);
- }
- }
- }
-
- IDispatch *pXlRange;
- {
- VARIANT parm;
- parm.vt = VT_BSTR;
- parm.bstrVal = ::SysAllocString(L"A1:O400");//L"A1:O1"
- VARIANT result;
- VariantInit(&result);
- AutoWrap(DISPATCH_PROPERTYGET, &result, pXlSheet, L"Range", 1, parm);
- VariantClear(&parm);
- pXlRange = result.pdispVal;
- }
- //::MessageBox(NULL,_T("我要填充数据了哈!"), _T("通知"), 0x10000);
- // 用我们的数组填充这个Range
- AutoWrap(DISPATCH_PROPERTYPUT, NULL, pXlRange, L"Value", 1, arr);
- pXlRange->Release();
- return 1;
- }
- char** InsertExcel::dwStr(char *str)
- {
-
- char Fie[6][300]={{""}};
- char ff[6][200]={{""}};
- //char Fe[6][200]={{""}};
- char **Fe;
- char Do[50]; //一个字段还有多个分离的字符串
- char str1[500]="";
- char path[256];
- char cmd[100];
- int zd; //标记字段起始点
- int m_duozd=0; //标记遇到上条记录余下的记录
-
- //一行一行的写入硬盘文件中
- FILE* fp = fopen("c:\Telnet-xls.txt","at");
- fprintf(fp, "%s",str);
- fclose(fp);
- int i=0;
- int j=0;
- Fe=(char **)malloc(6*sizeof(char *)); //首先动态构建第一维
- for(int t=0;t<6;t++)
- {
- *(Fe+t)=ff[t];
- }
-
- while((str[i]!=' ')&&(str[i]!='>')&&(str[i]!=13)&&(str[i]!='#'))
- {
- cmd[j++]=str[i++];
- }
- cmd[j]=str[i];
- cmd[j+1]=' ';
- int k=1;
- while(cmd[k]!=' ')
- {
- if(((cmd[k]=='>')||(cmd[k]=='#'))&&(cmd[k+1]==' ')) //strcmp(cmd,"TCS_Switch")==0找到根标记是以'#'或'>'结尾的
- {
- //判断命令属于配置文件中的第几条,并用m_flag标注
- m_flag=1;
- m_zhi=1;
- for(int fo=0;fo<6;fo++)
- {
- ziduan[fo]=0;
- }
- return Fe;
- }
- k++;
- }
- i=0;j=0;
- while((i<strlen(str))&&(m_flag!=10)&&(str[i]!=13))
- {
- if((str[i]!=' '))//(str[i]!=' ')&&
- {
- while((str[i]=='-')&&(str[i+1]=='-'))
- return Fe;
- zd=i;
- char zid[20]="";
-
- for(int c=0;c<6;c++)
- {
- j=0;
- if(ziduan[c]==0){
- while((str[i]!=' ')&&(str[i+1]!=' '))
- {
- Fie[c][j++]=str[i++];
- }
- }else{
- if((c+1<=5)&&(ziduan[c+1]==0))
- {
- ziduan[c]=40;
- m_zdshu=c;
- }
- while(((i-zd)<ziduan[c])&&(str[i]!=13))//若不加str[i]!=13条件,单元格高度加倍
- {
- Fie[c][j++]=str[i++];
- if((str[i]==',')&&(str[i+1]==13))//遇到,加回车符即可判断下行还是此记录内容
- {
- m_duozd=1;
- }
- if(str[i]==13) break;
- }
- }
- Fie[c][j]=' ';//Status
- if(Fie[c][0]==' ')//Excel存在这样一种情况:若该单元格无数据时,该单元格与上一单元格会合并成一个单元格。
- { //该处理就为解决这一问题。
- Fie[c][0]=' ';
- Fie[c][1]=' ';
- }
- if(m_zhi==1)
- {
- while((str[i]==' ')&&(str[i]!=' '))
- {
- i++;
- }
- ziduan[c]=i-zd;
- zd=i;
- }
- zd=i;
- }
- while(i<strlen(str))
- {
- i++;
- }
- if(m_zhi==1)
- m_zhi=0;
- }
- i++;
- }
-
- // printf("------------n|%s|-----",Fie[0]);
- if(m_duozd==1){ //遇到一条记录占多行的情况 插入累加到数据库中
- if((Fie[0][0]==' ')&&(Fie[0][1]==' '))
- {
- strcat(v_duozd[m_zdshu],Fie[m_zdshu]);
- }else{
- for(int k=0;k<6;k++){
- strcpy(v_duozd[k],Fie[k]);
- }
- }
-
- }else
- {
- if((Fie[0][0]==' ')&&(Fie[0][1]==' '))
- {
- strcat(v_duozd[m_zdshu],Fie[m_zdshu]);
- for(int t=0;t<6;t++)
- {
- *(Fe+t)=v_duozd[t];
- }
- }else{
-
- for(int t=0;t<6;t++)
- {
- *(Fe+t)=Fie[t];
- }
- for(int k=0;k<6;k++){
-
- strcpy(v_duozd[k],Fie[k]);
- }
- }
- }
- return Fe;
- }
- HRESULT InsertExcel::AutoWrap(int autoType, VARIANT *pvResult, IDispatch *pDisp, LPOLESTR ptName, int cArgs...) {
- // Begin variable-argument list...
- va_list marker;
- va_start(marker, cArgs);
- if(!pDisp) {
- MessageBox(NULL,_T("NULL IDispatch passed to AutoWrap()"),_T("Error"), 0x10010);
- _exit(0);
- }
- // Variables used...
- DISPPARAMS dp = { NULL, NULL, 0, 0 };
- DISPID dispidNamed = DISPID_PROPERTYPUT;
- DISPID dispID;
- HRESULT hr;
- char buf[200];
- char szName[200];
- // Convert down to ANSI
- WideCharToMultiByte(CP_ACP, 0, ptName, -1, szName, 256, NULL, NULL);
- // Get DISPID for name passed...
- hr = pDisp->GetIDsOfNames(IID_NULL, &ptName, 1, LOCALE_USER_DEFAULT, &dispID);
- if(FAILED(hr)) {
- sprintf(buf, "IDispatch::GetIDsOfNames("%s") failed w/err 0x%08lx", szName, hr);
- //LPCSTR lbuf=(LPCSTR)buf;
- MessageBox(NULL, buf,_T("AutoWrap()"), 0x10010);
- _exit(0);
- return hr;
- }
- // Allocate memory for arguments...
- VARIANT *pArgs = new VARIANT[cArgs+1];
- // Extract arguments...
- for(int i=0; i<cArgs; i++) {
- pArgs[i] = va_arg(marker, VARIANT);
- }
- // Build DISPPARAMS
- dp.cArgs = cArgs;
- dp.rgvarg = pArgs;
- // Handle special-case for property-puts!
- if(autoType & DISPATCH_PROPERTYPUT) {
- dp.cNamedArgs = 1;
- dp.rgdispidNamedArgs = &dispidNamed;
- }
- // Make the call!
- hr = pDisp->Invoke(dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT, autoType, &dp, pvResult, NULL, NULL);
- if(FAILED(hr)) {
- sprintf(buf, "IDispatch::Invoke("%s"=%08lx) failed w/err 0x%08lx", szName, dispID, hr);
- //LPCSTR lbuf=(LPCSTR)buf;
- MessageBox(NULL, buf,_T("AutoWrap()"), 0x10010);
- _exit(0);
- return hr;
- }
- // End variable-argument section...
- va_end(marker);
- delete [] pArgs;
- return hr;
- }