VC++实现对Excel操作(1)

时间:2021-08-19 19:55:28
前段时间老大让我做一个项目,要用到Excel操作,将大批十六进制数据分析完数据结构后提取到Excel中,再进行一定处理,然后写工具转换为一定格式的txt文件,最后转换为DB文件。我也是头一次接触这个,先在网上查了些资料,然后进行总结,经过一段时间后,我将原来的对Excel的操作封装成类,结合网上的资料和自己一段时间的实践,才有这些结果,还在进一步完善,因为我是根据需求一步一步完善的。
//下面是.h头文件中的内容
#include "excel.h"#include <comdef.h>//下划线种类#define xlUnderlineStyleDouble -4119#define xlUnderlineStyleDoubleAccounting 5#define xlUnderlineStyleNone -4142#define xlUnderlineStyleSingle 2#define xlUnderlineStyleSingleAccounting 4//边框方位#define xlDiagonalDown 5#define xlDiagonalUp 6#define xlEdgeBottom 9#define xlEdgeLeft 7#define xlEdgeRight 10#define xlEdgeTop 8#define xlInsideHorizontal 12#define xlInsideVertical 11//边框划线类型#define xlContinuous 1#define xlDash -4115#define xlDashDot 4#define xlDashDotDot 5#define xlDot -4118#define xlDouble -4119#define xlLineStyleNone -4142#define xlNone -4142#define xlSlantDashDot 13//字体水平对齐方式#define xlGeneral 1#define xlCenter -4108#define xlLeft -4131#define xlRight -4152#define xlFill 5#define xlJustify -4130//字体垂直对齐方式#define xlTop -4160#define xlBottom -4107//边框划线的粗细#define xlHairline 1#define xlMedium -4138#define xlThick 4#define xlThin 2//背景图案的类型#define xlSolid 1#define xlGray8 18#define xlGray75 -4126#define xlGray50 -4125#define xlGray25 -4124#define xlGray16 17#define xlHorizontal -4128#define xlVertical -4166#define xlDown -4121#define xlUp -4162#define xlChecker 9#define xlSemiGray75 10#define xlLightHorizontal 11#define xlLightVertical 12#define xlLightDown 13#define xlLightUp 14#define xlGrid 15#define xlCrissCross 16//边框设置参数类class MyBorder{public://线条形状short LineStyle;//粗线short Weight; //颜色long Color;//构造函数MyBorder();};//背景设置参数类class MyBackGroundStyle{public://背景颜色long Color;//背景图案short Pattern;//背景图案颜色long PatternColor;//透明与否BOOL transparent;//构造函数MyBackGroundStyle();};class MyFont{public:MyFont();//名字CString Name;long size;long ForeColor;BOOL Bold;//斜体BOOL Italic;//中间线BOOL StrikeThrough;BOOL Shadow;//下标BOOL Subscript;//上标BOOL Superscript;short UnderLine;};class MyAlignment{public:short HorizontalAlignment;short VerticalAlignment;MyAlignment();};class MyNumberFormat{public:MyNumberFormat();//设置参数CString strValue;CString GetTextSetup();CString GetNumberSetup(BOOL bSeparator,int iNumDecimal);//bInChinese:TRUE-"¥",FALSE-"$"CString GetMoneySetup(BOOL bInChinese,int iNumDecimal);CString GetDateSetup(BOOL bInChinese);CString GetTimeSetup(BOOL bInChinese);CString GetGeneralSetup();CString GetSpecialNumberSetup(BOOL bInChinese);CString GetPercentNumberSetup(int iNumDecimal);CString GetFractionNumberSetup(int DownSum,int DownNum);CString GetScientificNumberSetup(int iNumDecimal);CString GetPostalcodeSetup(int Num);CString GetDateTimeSetup(BOOL bInChinese);};class CMyExcel  {public:_Application MyApp;_Workbook MyBook;_Worksheet MySheet;Workbooks MyBooks;Worksheets MySheets;Range MyRange;CString strFilePath;CMyExcel();virtual ~CMyExcel();BOOL Open();BOOL Open(CString strFile);BOOL OpenSheet(CString strSheet);BOOL SetItemText(long Row,long Col,CString strText);CString GetItemText(long Row,long Col);void Exit();void SetVisible(BOOL bVisible);BOOL IsFileExist(CString strFn,BOOL bDir);void Save();void SaveAs(CString strPath);void AddSheet(CString strSheet);void DeleteSheet123();void GetRange(CString strBegin,CString strEnd);void AutoColFit();void AutoRowFit();void SetWrapText(BOOL bTrue);void SetFont(MyFont font);void GetAllRange();void SetMergeCells(BOOL bTrue);void SetBackGroundStyle(MyBackGroundStyle BackStyle);void SetBorderLine(short position,MyBorder Border);void SetAlignment(MyAlignment XMyAlignment);long GetRows();long GetCols();void SetNumberFormat(MyNumberFormat XNumberFormat);void SetColumnWidth(int iWidth);void SetRowHeight(int iHeight);void PrintOut(short numCopy);void PrePrintOut(BOOL bEnable);void InsertPicture(CString strFilePath);void SetBackPicture(CString strFilePath);long Find(CString str);};