Re: Excel
От: Jakop Россия https://wmspanel.com
Дата: 09.04.04 00:02
Оценка:
Может подойдет....здесь правда Ado не используется....



// RSdn_eXCEL.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"
#include "excelautoclass.h"
#include "comutil.h"
#include <fstream>
#include <iostream>

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    ::CoInitialize(NULL);

        
    ofstream ofs("Data.txt");    
        
     {
         ExcelAutoClass Ex;
         try{
         
             if(Ex.OpenDocument("c:\\ps20209.xls")!=0){
                   cout<<"Can't file open....."<<endl;                  
                   throw "clear";
                   return false;
                   ofs.close();
                 }
                 cout<<"Excel file open.....";
                              
                    _bstr_t Str;
                 _bstr_t temp;
                  


                 for(long i=Ex.GetLBoundI();i<=Ex.GetUBoundI();i++){
                     temp="";
                 for(long j=Ex.GetLBoundJ();j<=Ex.GetUBoundJ();j++){
                        Str=Ex.GetValue(i,j);
                     temp+=Str;
                       CharToOem(LPCTSTR(temp),temp);
                     
                     }
                    ofs<<LPTSTR(temp)<<endl;
                 }
             throw "clear";
         }catch(...){
    
            Ex.CloseFile();
            
               ofs.flush();
            ofs.close();
    
         }

     }    
        
    ::CoUninitialize();
    return 0;
}

///////////////ExcelAutoClass.cpp

#include "StdAfx.h"
#include "excelautoclass.h"

ExcelAutoClass::ExcelAutoClass()
{
     try{  
       excel=new _ApplicationPtr("Excel.Application");
         (*excel)->Application->EnableEvents=false;
       (*excel)->Visible= false;
       FileOpened=false;
    }
    catch(...){
        ExcelApplicationCreate=false;
        return;
    }
        ExcelApplicationCreate=true;
}

ExcelAutoClass::~ExcelAutoClass(void)
{
    if(FileOpened)CloseFile(); 
    if(ExcelApplicationCreate)
    (*excel)->Quit();
    if(excel)delete excel;
}
int ExcelAutoClass::OpenDocument(const char* filename)
{
    try{
     book  = (*excel)->Workbooks->Open(filename);
     sheet = book->Worksheets->Item[1L];
     Rect=sheet->UsedRange;
     Array=Rect->Value;
      
     HRESULT hr= SafeArrayAccessData((SAFEARRAY*)(Array.pparray), (void HUGEP* FAR*)&pVar);
     if(hr!=S_OK){
         FileOpened=false;
         book->Close();return -1;
     }
     UINT DimCount=SafeArrayGetDim((SAFEARRAY*)(Array.pparray));
     if(DimCount!=2){
        FileOpened=false;
        book->Close();return -1;
     }
     
     SafeArrayGetLBound((SAFEARRAY*)(Array.pparray),1,&LBoundI); 
     SafeArrayGetLBound((SAFEARRAY*)(Array.pparray),2,&LBoundJ); 
     SafeArrayGetUBound((SAFEARRAY*)(Array.pparray),1,&UBoundI); 
     SafeArrayGetUBound((SAFEARRAY*)(Array.pparray),2,&UBoundJ); 
     
    }
    catch (_com_error& er) {
      FileOpened=false;
      return -1;
    }
    FileOpened=true;
    return 0;
}
void ExcelAutoClass::CloseFile(){
    if(FileOpened){
      SafeArrayUnaccessData((SAFEARRAY*)(Array.pparray));//Освобождаем ссылку на Safe массив*/
      book->Close();
      FileOpened=false;
    }
}
unsigned long ExcelAutoClass::GetIJ(int I,int J){
     return (I-1)+(J-1)*((UBoundI-LBoundI+1));
}

_variant_t ExcelAutoClass::GetValue(int I,int J){
    
    if(!FileOpened)return _variant_t(0L);
      return pVar[GetIJ(I,J)];
}

////////////////////////#include "excelautoclass.h"
#pragma once
#include "office.h"
#include "comutil.h"
using namespace Excel;

typedef bool(*CallBackEx)(int,int,const _variant_t&);

class ExcelAutoClass
{
private:
    bool FileOpened;
    bool ExcelApplicationCreate;
    _ApplicationPtr* excel;
    _WorkbookPtr book;
    _WorksheetPtr sheet;
     RangePtr Rect;//
    _variant_t Array;
    _variant_t* pVar;
    long LBoundI,UBoundI,LBoundJ,UBoundJ;
public:
    ExcelAutoClass();
    ~ExcelAutoClass(void);
    int OpenDocument(const char* filename);
    void CloseFile();
    unsigned long GetIJ(int I,int J);
    long GetLBoundI(){return LBoundI;};
    long GetLBoundJ(){return LBoundJ;};
    long GetUBoundI(){return UBoundI;};
    long GetUBoundJ(){return UBoundJ;};
    _variant_t GetValue(int I,int J);
};
/////////////office.h
//#define Uses_MSO2000

#ifdef Uses_MSO2000
// for MS Office 2000
#import "C:\Program Files\Microsoft Office\Office\MSO9.DLL"
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB"
#import "C:\Program Files\Microsoft Office\Office\MSWORD9.OLB" \
        rename("ExitWindows","_ExitWindows")
#import "C:\Program Files\Microsoft Office\Office\EXCEL9.OLB" \
        rename("DialogBox","_DialogBox") \
        rename("RGB","_RGB") \
        exclude("IFont","IPicture")
#import "C:\Program Files\Common Files\Microsoft Shared\DAO\DAO360.DLL" \
        rename("EOF","EndOfFile") rename("BOF","BegOfFile")
#else
#import "D:\Program Files\Microsoft Office\Office\MSO97.DLL"
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBEEXT1.OLB"
#import "D:\Program Files\Microsoft Office\Office\MSWORD8.OLB" \
        rename("ExitWindows","_ExitWindows")
#import "D:\Program Files\Microsoft Office\Office\EXCEL8.OLB" \
        rename("DialogBox","_DialogBox") \
        rename("RGB","_RGB") \
        exclude("IFont","IPicture")
#endif


Тут лежит проект на MSVC 7
https://wmspanel.com/nimble — Nimble Streamer media server for live and VOD HLS, RTMP, HTTP streaming

https://wmspanel.com/ — Control and reporting panel for Wowza and Nimble Streamer
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.