my latest C++ excel OLE automation code

L

Lynn McGuire

// fm2n_old.h


#include <iostream>
#include <string>
#include <vector>


extern std::string notebook;

extern std::string IniFileName;

extern int RunInTestMode;


const double uninitializedValue = -98765.4321;

const int defaultFirstColumnWidth = 25;
const int defaultOtherColumnWidth = 14;



// Excel Constants
const int xlCellTypeLastCell = 11;
const int xlThin = 2;
const int xlSolid = 1;
const int xlCenter = -4108;
const int xlBottom = -4107;
const int xlTop = -4160;
const int xlRight = -4152;
const int xlLeft = -4131;
const int xlNone = -4142;
const int xlContinuous = 1;
const int xlAutomatic = -4105;
const int xlExpression = 2;


extern std::string g_dimensionalUnits [MaxItemsInRow];
extern double * g_rows;
extern char g_aLine [100000];
extern char g_aLine2 [100000];
extern int g_numberOfRows;
extern int g_currentColumn;
extern const char * g_lineChunk [MaxLineChunks];
extern int g_numChunks;
extern char g_Fm2nbookTitle [];
extern int g_numberOfPages;

extern int RunInTestMode;

int MakeSureNotebookExists (std::string notebookName);
int ConnectToNotebook (std::string notebook, int runInTestMode, int firstTime);
int DisconnectFromNotebook (int runInTestMode);
void Fm2nbookGetStartupDir (void);
void ReportError (DWORD result, std::string job, std::string buffer);
void PutStringInServer (int row, int column, const char * str);
void PutStringInServer (int row, int column, std::string string);
void PutDoubleInServer (int row, int column, const double num);
void PutDoubleInServerVertical (int row, int column, int count, const double num []);
void PutDoubleInServerHorizontal (int row, int column, int count, const double num []);
void PutIntegerInServer (const int row, const int column, const int num);
int ConnectToNewSheet (std::string newSheetName, int createNewSheet, std::string baseSheetName, int deleteExistingSheet);
std::string GetCellEquivalent (const int row, const int column);
std::string GetCellRowEquivalent (const int row);
std::string GetCellColumnEquivalent (const int column);
void NameCellInServer (std::string cellName, std::string sheetName, const int row, const int column);
int ConnectToNewNoteBook (int caseNumber);
std::string GetExcelCurrentSelection (void);
void InitTable (int numberOfRows, int numberOfColumns);
void PutDoubleInTable (int numberOfDoubles, double * num);
void PutDoubleInTable (double num);
void PutStringInTable (std::string str);
void PutBlankInTable (int numberOfBlanks);
void PutIntegerInTable (int nunber);
void SendTableToServer (int row1, int column1, int row2, int column2);
double AsDouble (const char * str);
double AsDouble (std::string string);
void PutRowInTable (std::string title, std::string number, std::string units);
void PutRowInTable (std::string title, std::string number, std::string units, int offset);
void PutRow4InTable (std::string title, std::string units, std::string num1, std::string num2,
std::string num3, std::string num4, int offset);

std::string removeTrailingBlanks (std::string str);
int find (std::vector <int> aList, int val);
int StartExcelServer (void);

std::vector <std::string> getCellInfo (std::string sheetName, std::string column1, std::string column2,
std::string row1, std::string row2);
int putCellInfo (std::vector <std::string> results, std::string sheetName, std::string column1,
std::string column2, std::string row1, std::string row2,
std::string dimensionalUnitsValue, int dimensionalUnitsPlacement);
int setRangeFont (std::string rangeValue, std::string fontNameValue, int boldValue, int sizeValue, int colorValue);
int SetRangeWidthAlignment (std::string rangeValue, int columnWidthValue, int horizontalAlignmentValue);
 
L

Lynn McGuire

// asString.h


#ifndef _ASSTRING_H_
#define _ASSTRING_H_

#include <string>

std::string asString ();
std::string asString (void * val);
std::string asString (int val);
std::string asString (long val);
std::string asString (unsigned int val);
std::string asString (double val);
std::string asString (unsigned long val);
std::string asString (const char *val);
std::string asString (int val, const char * conversion);
std::string asString (long val, const char * conversion);
std::string asString (unsigned int val, const char * conversion);
std::string asString (unsigned long val, const char * conversion);
std::string asString (double val, const char * conversion);

#endif
 
L

Lynn McGuire

// asString.cpp



#include <stdio.h>
#include <string.h>
#include <string>

#include "asstring.h"



// Default asString is sysPrint.
std::string asString ()
{
return "";
}


std::string asString (void * val)
{
char buffer [100];
#ifdef _MSC_VER
sprintf_s (buffer, sizeof (buffer), "%p", val);
#else
sprintf (buffer, "%u", val);
#endif
return buffer;
}


std::string asString (int val)
{
char buffer [100];
#ifdef _MSC_VER
sprintf_s (buffer, sizeof (buffer), "%d", val);
#else
sprintf (buffer, "%d", val);
#endif
return buffer;
}


std::string asString (long val)
{
char buffer [100];
#ifdef _MSC_VER
sprintf_s (buffer, sizeof (buffer), "%ld", val);
#else
sprintf (buffer, "%ld", val);
#endif
return buffer;
}


std::string asString (unsigned int val)
{
char buffer [100];
#ifdef _MSC_VER
sprintf_s (buffer, sizeof (buffer), "%u", val);
#else
sprintf (buffer, "%u", val);
#endif
return buffer;
}


std::string asString (double val)
{
char buffer [100];
#ifdef _MSC_VER
sprintf_s (buffer, sizeof (buffer), "%.12g", val);
#else
sprintf (buffer, "%.12g", val);
#endif
return buffer;
}


std::string asString (unsigned long val)
{
char buffer [100];
#ifdef _MSC_VER
sprintf_s (buffer, sizeof (buffer), "%lu", val);
#else
sprintf (buffer, "%lu", val);
#endif
return buffer;
}


std::string asString (const char *val)
{
char buffer [100];
#ifdef _MSC_VER
sprintf_s (buffer, sizeof (buffer), "%s", val);
#else
sprintf (buffer, "%s", val);
#endif
return buffer;
}


std::string asString (int val, const char * conversion)
{
char buffer [100];
#ifdef _MSC_VER
sprintf_s (buffer, sizeof (buffer), conversion, val);
#else
sprintf (buffer, conversion, val);
#endif
return buffer;
}


std::string asString (long val, const char * conversion)
{
char buffer [100];
#ifdef _MSC_VER
sprintf_s (buffer, sizeof (buffer), conversion, val);
#else
sprintf (buffer, conversion, val);
#endif
return buffer;
}


std::string asString (unsigned int val, const char * conversion)
{
char buffer [100];
#ifdef _MSC_VER
sprintf_s (buffer, sizeof (buffer), conversion, val);
#else
sprintf (buffer, conversion, val);
#endif
return buffer;
}


std::string asString (unsigned long val, const char * conversion)
{
char buffer [100];
#ifdef _MSC_VER
sprintf_s (buffer, sizeof (buffer), conversion, val);
#else
sprintf (buffer, conversion, val);
#endif
return buffer;
}


std::string asString (double val, const char * conversion)
{
char buffer [100];
#ifdef _MSC_VER
sprintf_s (buffer, sizeof (buffer), conversion, val);
#else
sprintf (buffer, conversion, val);
#endif
return buffer;
}
 
L

Lynn McGuire

// fm2n_old.cpp


// The OLE Automation interface to Excel is mostly written using:
// "How to automate Excel from C++ without using MFC or #import"
// http://support.microsoft.com/kb/216686

// to call this code :
// if (ConnectToNotebook (notebook, RunInTestMode, true))
// {
// // do your excel data transfer here
// DisconnectFromNotebook (RunInTestMode);
// }

// see getCellInfo or putCellInfo for single cell data transfer examples

// see PutRowInTable for bulk transfer example, note that the more data can
// be transferred at one time, the faster the data transfer is

//void PutRowInTable (std::string title, std::string number, std::string units, int offset)
//{
// InitTable (1, 3);
// PutStringInTable (title);
// if (units.size () > 0)
// PutStringInTable (SetUnitsCase (units));
// else
// PutBlankInTable (1);
// double temp = AsDouble (number);
// PutDoubleInTable (temp);
// SendTableToServer (offset, 1, offset, 3);

Lynn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top