【MFC/C++操作Excel】Excel篇 (OLE/COM)
阿新 • • 發佈:2019-01-04
MFC操作Excel
下面的操作基於Excel2003
一.初始化操作
1.匯入類庫
點選檢視->建立類嚮導-> Add Class...\From a type Library...-> C:\Program Files\Microsoft Office\Office\EXCEL.EXE,接下來就可以看到匯入的類excel.h, excel.cpp。
2.初始化COM
找到App的InitInstance()函式,在其中新增 AfxOleInit()函式的呼叫,如:
[cpp] view plain copy print?- if (!AfxOleInit())
- {
- AfxMessageBox("註冊COM出錯!");
- return FALSE;
- }
if (!AfxOleInit())
{
AfxMessageBox("註冊COM出錯!");
return FALSE;
}
二.我自己寫的Excel操作類
ExcelOperate.h
[cpp] view plain copy print?- #include "atlbase.h"
- #include "excel.h"
- usingnamespace
- class CExcelOperate
- {
- private:
- myexcel::_Application m_ecApp;
- Workbooks m_ecBooks;
- _Workbook m_ecBook;
- Worksheets m_ecSheets;
- _Worksheet m_ecSheet;
- myexcel::Range m_ecRange;
- VARIANT ret;//儲存單元格的值
- public:
- CExcelOperate();
- virtual ~CExcelOperate();
- public:
- //操作
- //**********************建立新EXCEL*******************************************
- BOOL CreateApp();
- BOOL CreateWorkbooks(); //建立一個新的EXCEL工作簿集合
- BOOL CreateWorkbook(); //建立一個新的EXCEL工作簿
- BOOL CreateWorksheets(); //建立一個新的EXCEL工作表集合
- BOOL CreateWorksheet(short index); //建立一個新的EXCEL工作表
- BOOL CreateSheet(short index);
- BOOL Create(short index = 1); //建立新的EXCEL應用程式並建立一個新工作簿和工作表
- void ShowApp(); //顯示EXCEL文件
- void HideApp(); //隱藏EXCEL文件
- //**********************開啟文件*********************************************
- BOOL OpenWorkbook(CString fileName, short index = 1);
- BOOL Open(CString fileName); //建立新的EXCEL應用程式並開啟一個已經存在的文件。
- BOOL SetActiveWorkbook(short i); //設定當前啟用的文件。
- //**********************儲存文件*********************************************
- BOOL SaveWorkbook(); //Excel是以開啟形式,儲存。
- BOOL SaveWorkbookAs(CString fileName);//Excel以建立形式,儲存。
- BOOL CloseWorkbook();
- void CloseApp();
- //**********************讀資訊********************************
- BOOL GetRangeAndValue(CString begin, CString end);//得到begin到end的Range並將之間的值設定到ret中
- void GetRowsAndCols(long &rows, long &cols);//得到ret的行,列數
- BOOL GetTheValue(int rows, int cols, CString &dest);//返回第rows,cols列的值,注意只返回文字型別的,到dest中
- BOOL SetTextFormat(CString &beginS, CString &endS);//將beginS到endS設定為文字格式(數字的還要用下面的方法再轉一次)
- BOOL SetRowToTextFormat(CString &beginS, CString &endS);//將beginS到endS(包括數字型別)設定為文字格式
- };
#include "atlbase.h"
#include "excel.h"
using namespace myexcel;
class CExcelOperate
{
private:
myexcel::_Application m_ecApp;
Workbooks m_ecBooks;
_Workbook m_ecBook;
Worksheets m_ecSheets;
_Worksheet m_ecSheet;
myexcel::Range m_ecRange;
VARIANT ret;//儲存單元格的值
public:
CExcelOperate();
virtual ~CExcelOperate();
public:
//操作
//**********************建立新EXCEL*******************************************
BOOL CreateApp();
BOOL CreateWorkbooks(); //建立一個新的EXCEL工作簿集合
BOOL CreateWorkbook(); //建立一個新的EXCEL工作簿
BOOL CreateWorksheets(); //建立一個新的EXCEL工作表集合
BOOL CreateWorksheet(short index); //建立一個新的EXCEL工作表
BOOL CreateSheet(short index);
BOOL Create(short index = 1); //建立新的EXCEL應用程式並建立一個新工作簿和工作表
void ShowApp(); //顯示EXCEL文件
void HideApp(); //隱藏EXCEL文件
//**********************開啟文件*********************************************
BOOL OpenWorkbook(CString fileName, short index = 1);
BOOL Open(CString fileName); //建立新的EXCEL應用程式並開啟一個已經存在的文件。
BOOL SetActiveWorkbook(short i); //設定當前啟用的文件。
//**********************儲存文件*********************************************
BOOL SaveWorkbook(); //Excel是以開啟形式,儲存。
BOOL SaveWorkbookAs(CString fileName);//Excel以建立形式,儲存。
BOOL CloseWorkbook();
void CloseApp();
//**********************讀資訊********************************
BOOL GetRangeAndValue(CString begin, CString end);//得到begin到end的Range並將之間的值設定到ret中
void GetRowsAndCols(long &rows, long &cols);//得到ret的行,列數
BOOL GetTheValue(int rows, int cols, CString &dest);//返回第rows,cols列的值,注意只返回文字型別的,到dest中
BOOL SetTextFormat(CString &beginS, CString &endS);//將beginS到endS設定為文字格式(數字的還要用下面的方法再轉一次)
BOOL SetRowToTextFormat(CString &beginS, CString &endS);//將beginS到endS(包括數字型別)設定為文字格式
};
ExcelOperate.cpp
[cpp] view plain copy print?- CExcelOperate::CExcelOperate()
- {
- }
- CExcelOperate::~CExcelOperate()
- {
- }
- BOOL CExcelOperate::CreateApp()
- {
- //if (FALSE == m_wdApp.CreateDispatch("Word.Application"))
- COleException pe;
- if (!m_ecApp.CreateDispatch(_T("Excel.Application"), &pe))
- {
- AfxMessageBox("Application建立失敗,請確保安裝了word 2000或以上版本!", MB_OK|MB_ICONWARNING);
- pe.ReportError();
- throw &pe;
- return FALSE;
- }
- return TRUE;
- }
- BOOL CExcelOperate::CreateWorkbooks() //建立一個新的EXCEL工作簿集合
- {
- if (FALSE == CreateApp())
- {
- return FALSE;
- }
- m_ecBooks = m_ecApp.GetWorkbooks();
- if (!m_ecBooks.m_lpDispatch)
- {
- AfxMessageBox("WorkBooks建立失敗!", MB_OK|MB_ICONWARNING);
- return FALSE;
- }
- return TRUE;
- }
- BOOL CExcelOperate::CreateWorkbook() //建立一個新的EXCEL工作簿
- {
- if(!m_ecBooks.m_lpDispatch)
- {
- AfxMessageBox("WorkBooks為空!", MB_OK|MB_ICONWARNING);
- return FALSE;
- }
- COleVariant vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
- m_ecBook = m_ecBooks.Add(vOptional);
- if(!m_ecBook.m_lpDispatch)
- {
- AfxMessageBox("WorkBook為空!", MB_OK|MB_ICONWARNING);
- return FALSE;
- }
- /*
- //得到document變數
- m_wdDoc = m_wdApp.GetActiveDocument();
- if (!m_wdDoc.m_lpDispatch)
- {
- AfxMessageBox("Document獲取失敗!", MB_OK|MB_ICONWARNING);
- return FALSE;
- }
- //得到selection變數
- m_wdSel = m_wdApp.GetSelection();
- if (!m_wdSel.m_lpDispatch)
- {
- AfxMessageBox("Select獲取失敗!", MB_OK|MB_ICONWARNING);
- return FALSE;
- }
- //得到Range變數
- m_wdRange = m_wdDoc.Range(vOptional,vOptional);
- if(!m_wdRange.m_lpDispatch)
- {
- AfxMessageBox("Range獲取失敗!", MB_OK|MB_ICONWARNING);
- return FALSE;
- }
- */
- return TRUE;
- }
- BOOL CExcelOperate::CreateWorksheets() //建立一個新的EXCEL工作表集合
- {
- if(!m_ecBook.m_lpDispatch)
- {
- AfxMessageBox("WorkBook為空!", MB_OK|MB_ICONWARNING);
- return FALSE;
- }
- m_ecSheets = m_ecBook.GetSheets();
- if(!m_ecSheets.m_lpDispatch)
- {
- AfxMessageBox("WorkSheets為空!", MB_OK|MB_ICONWARNING);
- return FALSE;
- }
- return TRUE;
- }
- BOOL CExcelOperate::CreateWorksheet(short index) //建立一個新的EXCEL工作表
- {
- if(!m_ecSheets.m_lpDispatch)
- {
- AfxMessageBox("WorkSheets為空!", MB_OK|MB_ICONWARNING);
- return FALSE;
- }
- m_ecSheet = m_ecSheets.GetItem(COleVariant(index));
- if(!m_ecSheet.m_lpDispatch)
- {
- AfxMessageBox("WorkSheet為空!", MB_OK|MB_ICONWARNING);
- return FALSE;
- }
- return TRUE;
- }
- BOOL CExcelOperate::CreateSheet(short index)
- {
- if(CreateWorksheets() == FALSE)
- {
- return FALSE;
- }
- if(CreateWorksheet(index) == FALSE)
- {
- return FALSE;
- }
- return TRUE;
- }
- BOOL CExcelOperate::Create(short index) //建立新的EXCEL應用程式並建立一個新工作簿和工作表
- {
- if(CreateWorkbooks() == FALSE)
- {
- return FALSE;
- }
- if(CreateWorkbook() == FALSE)
- {
- return FALSE;
- }
- if(CreateSheet(index) == FALSE)
- {
- return FALSE;
- }
- return TRUE;
- }
- void CExcelOperate::ShowApp() //顯示WORD文件
- {
- m_ecApp.SetVisible(TRUE);
- }
- void CExcelOperate::HideApp() //隱藏word文件
- {
- m_ecApp.SetVisible(FALSE);
- }
- //**********************開啟文件*********************************************
- BOOL CExcelOperate::OpenWorkbook(CString fileName, short index)
- {
- if(!m_ecBooks.m_lpDispatch)
- {
- AfxMessageBox("WorkSheets為空!", MB_OK|MB_ICONWARNING);
- return FALSE;
- }
- //COleVariant vFileName(_T(fileName));
- COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
- m_ecBook = m_ecBooks.Open(fileName,VOptional, VOptional, VOptional, VOptional,VOptional, VOptional, VOptional, VOptional,VOptional, VOptional, VOptional, VOptional, VOptional, VOptional);
- if(!m_ecBook.m_lpDispatch)
- {
- AfxMessageBox("WorkSheet獲取失敗!", MB_OK|MB_ICONWARNING);
- return FALSE;
- }
- if(CreateSheet(index) == FALSE)
- {
- return FALSE;
- }
- return TRUE;
- }
- BOOL CExcelOperate::Open(CString fileName) //建立新的EXCEL應用程式並開啟一個已經存在的文件。
- {
- if(CreateWorkbooks() == FALSE)
- {
- return FALSE;
- }
- return OpenWorkbook(fileName);
- }
- /*BOOL CExcelOperate::SetActiveWorkbook(short i) //設定當前啟用的文件。
- {
- }*/
- //**********************儲存文件*********************************************
- BOOL CExcelOperate::SaveWorkbook() //文件是以開啟形式,儲存。
- {
- if (!m_ecBook.m_lpDispatch)
- {
- AfxMessageBox("Book獲取失敗!", MB_OK|MB_ICONWARNING);
- return FALSE;
- }
- m_ecBook.Save();
- return TRUE;
- }
- BOOL CExcelOperate::SaveWorkbookAs(CString fileName)//文件以建立形式,儲存。
- {
- if (!m_ecBook.m_lpDispatch)
- {
- AfxMessageBox("Book獲取失敗!", MB_OK|MB_ICONWARNING);
- return FALSE;
- }
- COleVariant vTrue((short)TRUE),
- vFalse((short)FALSE),
- vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
- COleVariant vFileName(_T(fileName));
- m_ecBook.SaveAs(
- vFileName, //VARIANT* FileName
- vOptional, //VARIANT* FileFormat
- vOptional, //VARIANT* LockComments
- vOptional, //VARIANT* Password
- vOptional, //VARIANT* AddToRecentFiles
- vOptional, //VARIANT* WritePassword
- 0, //VARIANT* ReadOnlyRecommended
- vOptional, //VARIANT* EmbedTrueTypeFonts
- vOptional, //VARIANT* SaveNativePictureFormat
- vOptional, //VARIANT* SaveFormsData
- vOptional, //VARIANT* SaveAsAOCELetter
- vOptional //VARIANT* ReadOnlyRecommended
- /* vOptional, //VARIANT* EmbedTrueTypeFonts
- vOptional, //VARIANT* SaveNativePictureFormat
- vOptional, //VARIANT* SaveFormsData
- vOptional //VARIANT* SaveAsAOCELetter*/
- );
- return TRUE;
- }
- BOOL CExcelOperate::CloseWorkbook()
- {
- COleVariant vTrue((short)TRUE),
- vFalse((short)FALSE),
- vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
- m_ecBook.Close(vFalse, // SaveChanges.
- vTrue, // OriginalFormat.
- vFalse // RouteDocument.
- );
- m_ecBook = m_ecApp.GetActiveWorkbook();
- if(!m_ecBook.m_lpDispatch)
- {
- AfxMessageBox("Book獲取失敗!", MB_OK|MB_ICONWARNING);
- return FALSE;
- }
- if(CreateSheet(1) == FALSE)
- {
- return FALSE;
- }
- return TRUE;
- }
- void CExcelOperate::CloseApp()
- {
- SaveWorkbook();
- COleVariant vTrue((short)TRUE),
- vFalse((short)FALSE),
- vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
- //m_ecDoc.Save();
- m_ecApp.Quit();
- if(m_ecRange.m_lpDispatch)
- m_ecRange.ReleaseDispatch();
- if(m_ecSheet.m_lpDispatch)
- m_ecSheet.ReleaseDispatch();
- if(m_ecSheets.m_lpDispatch)
- m_ecSheets.ReleaseDispatch();
- if(m_ecBook.m_lpDispatch)
- m_ecBook.ReleaseDispatch();
- if(m_ecBooks.m_lpDispatch)
- m_ecBooks.ReleaseDispatch();
- if(m_ecApp.m_lpDispatch)
- m_ecApp.ReleaseDispatch();
- }
- BOOL CExcelOperate::GetRangeAndValue(CString begin, CString end)
- {
- if(!m_ecSheet.m_lpDispatch)
- {
- AfxMessageBox("Sheet獲取失敗!", MB_OK|MB_ICONWARNING);
- return FALSE;
- }
- m_ecRange = m_ecSheet.GetRange(COleVariant(begin), COleVariant(end));
- if(!m_ecRange.m_lpDispatch)
- {
- AfxMessageBox("Range獲取失敗!", MB_OK|MB_ICONWARNING);
- return FALSE;
- }
- ret = m_ecRange.GetValue2();//得到表格中的值
- return TRUE;
- }
- void CExcelOperate::GetRowsAndCols(long &rows, long &cols)
- {
- COleSafeArray sa(ret);
- sa.GetUBound(1,&rows);
- sa.GetUBound(2,&cols);
- }
- //只返回CString型別的,其他型別概視為錯誤
- BOOL CExcelOperate::GetTheValue(int rows, int cols, CString &dest)
- {
- long rRows, rCols;
- long index[2];
- VARIANT val;
- COleSafeArray sa(ret);
- sa.GetUBound(1,&rRows);
- sa.GetUBound(2,&rCols);
- if(rows < 1 || cols < 1 || rRows < rows || rCols < cols)
- {
- AfxMessageBox("出錯點1");
- return FALSE;
- }
- index[0]=rows;
- index[1]=cols;
- sa.GetElement(index,&val);
- if(val.vt != VT_BSTR)
- {
- CString str;
- str.Format("出錯點2, %d",val.vt);
- AfxMessageBox(str);
- return FALSE;
- }
- dest = val.bstrVal;
- return TRUE;
- }
- //將beginS到endS之間設定為文字格式
- BOOL CExcelOperate::SetTextFormat(CString &beginS, CString &endS)
- {
- if(GetRangeAndValue(beginS, endS))
- {
- m_ecRange.Select();
- m_ecRange.SetNumberFormatLocal(COleVariant("@"));
- return TRUE;
- }
- return FALSE;
- }
- //將beginS到endS之間(必須是一列)設定為真正的文字格式
- BOOL CExcelOperate::SetRowToTextFormat(CString &beginS, CString &endS)
- {
- if(GetRangeAndValue(beginS, endS))
- {
- m_ecRange.Select();
- Range m_tempRange = m_ecSheet.GetRange(COleVariant(beginS), COleVariant(beginS));
- if(!m_tempRange.m_lpDispatch) return FALSE;
- COleVariant vTrue((short)TRUE),
- vFalse((short)FALSE);
- //int tempArray[2] = {1, 2};
- COleSafeArray saRet;
- DWORD numElements = {2};
- saRet.Create(VT_I4, 1, &numElements);
- long index = 0;
- int val = 1;
- saRet.PutElement(&index, &val);
- index++;
- val = 2;
- saRet.PutElement(&index, &val);
- //m_tempRange.GetItem(COleVariant((short)5),COleVariant("A"));
- m_ecRange.TextToColumns(m_tempRange.GetItem(COleVariant((short)1),COleVariant((short)1)), 1, 1, vFalse, vTrue, vFalse, vFalse, vFalse, vFalse, vFalse, saRet, vFalse, vFalse, vTrue);
- m_tempRange.ReleaseDispatch();
- return TRUE;
- }
- return FALSE;
- }
CExcelOperate::CExcelOperate()
{
}
CExcelOperate::~CExcelOperate()
{
}
BOOL CExcelOperate::CreateApp()
{
//if (FALSE == m_wdApp.CreateDispatch("Word.Application"))
COleException pe;
if (!m_ecApp.CreateDispatch(_T("Excel.Application"), &pe))
{
AfxMessageBox("Application建立失敗,請確保安裝了word 2000或以上版本!", MB_OK|MB_ICONWARNING);
pe.ReportError();
throw &pe;
return FALSE;
}
return TRUE;
}
BOOL CExcelOperate::CreateWorkbooks() //建立一個新的EXCEL工作簿集合
{
if (FALSE == CreateApp())
{
return FALSE;
}
m_ecBooks = m_ecApp.GetWorkbooks();
if (!m_ecBooks.m_lpDispatch)
{
AfxMessageBox("WorkBooks建立失敗!", MB_OK|MB_ICONWARNING);
return FALSE;
}
return TRUE;
}
BOOL CExcelOperate::CreateWorkbook() //建立一個新的EXCEL工作簿
{
if(!m_ecBooks.m_lpDispatch)
{
AfxMessageBox("WorkBooks為空!", MB_OK|MB_ICONWARNING);
return FALSE;
}
COleVariant vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
m_ecBook = m_ecBooks.Add(vOptional);
if(!m_ecBook.m_lpDispatch)
{
AfxMessageBox("WorkBook為空!", MB_OK|MB_ICONWARNING);
return FALSE;
}
/*
//得到document變數
m_wdDoc = m_wdApp.GetActiveDocument();
if (!m_wdDoc.m_lpDispatch)
{
AfxMessageBox("Document獲取失敗!", MB_OK|MB_ICONWARNING);
return FALSE;
}
//得到selection變數
m_wdSel = m_wdApp.GetSelection();
if (!m_wdSel.m_lpDispatch)
{
AfxMessageBox("Select獲取失敗!", MB_OK|MB_ICONWARNING);
return FALSE;
}
//得到Range變數
m_wdRange = m_wdDoc.Range(vOptional,vOptional);
if(!m_wdRange.m_lpDispatch)
{
AfxMessageBox("Range獲取失敗!", MB_OK|MB_ICONWARNING);
return FALSE;
}
*/
return TRUE;
}
BOOL CExcelOperate::CreateWorksheets() //建立一個新的EXCEL工作表集合
{
if(!m_ecBook.m_lpDispatch)
{
AfxMessageBox("WorkBook為空!", MB_OK|MB_ICONWARNING);
return FALSE;
}
m_ecSheets = m_ecBook.GetSheets();
if(!m_ecSheets.m_lpDispatch)
{
AfxMessageBox("WorkSheets為空!", MB_OK|MB_ICONWARNING);
return FALSE;
}
return TRUE;
}
BOOL CExcelOperate::CreateWorksheet(short index) //建立一個新的EXCEL工作表
{
if(!m_ecSheets.m_lpDispatch)
{
AfxMessageBox("WorkSheets為空!", MB_OK|MB_ICONWARNING);
return FALSE;
}
m_ecSheet = m_ecSheets.GetItem(COleVariant(index));
if(!m_ecSheet.m_lpDispatch)
{
AfxMessageBox("WorkSheet為空!", MB_OK|MB_ICONWARNING);
return FALSE;
}
return TRUE;
}
BOOL CExcelOperate::CreateSheet(short index)
{
if(CreateWorksheets() == FALSE)
{
return FALSE;
}
if(CreateWorksheet(index) == FALSE)
{
return FALSE;
}
return TRUE;
}
BOOL CExcelOperate::Create(short index) //建立新的EXCEL應用程式並建立一個新工作簿和工作表
{
if(CreateWorkbooks() == FALSE)
{
return FALSE;
}
if(CreateWorkbook() == FALSE)
{
return FALSE;
}
if(CreateSheet(index) == FALSE)
{
return FALSE;
}
return TRUE;
}
void CExcelOperate::ShowApp() //顯示WORD文件
{
m_ecApp.SetVisible(TRUE);
}
void CExcelOperate::HideApp() //隱藏word文件
{
m_ecApp.SetVisible(FALSE);
}
//**********************開啟文件*********************************************
BOOL CExcelOperate::OpenWorkbook(CString fileName, short index)
{
if(!m_ecBooks.m_lpDispatch)
{
AfxMessageBox("WorkSheets為空!", MB_OK|MB_ICONWARNING);
return FALSE;
}
//COleVariant vFileName(_T(fileName));
COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
m_ecBook = m_ecBooks.Open(fileName,VOptional, VOptional, VOptional, VOptional,VOptional, VOptional, VOptional, VOptional,VOptional, VOptional, VOptional, VOptional, VOptional, VOptional);
if(!m_ecBook.m_lpDispatch)
{
AfxMessageBox("WorkSheet獲取失敗!", MB_OK|MB_ICONWARNING);
return FALSE;
}
if(CreateSheet(index) == FALSE)
{
return FALSE;
}
return TRUE;
}
BOOL CExcelOperate::Open(CString fileName) //建立新的EXCEL應用程式並開啟一個已經存在的文件。
{
if(CreateWorkbooks() == FALSE)
{
return FALSE;
}
return OpenWorkbook(fileName);
}
/*BOOL CExcelOperate::SetActiveWorkbook(short i) //設定當前啟用的文件。
{
}*/
//**********************儲存文件*********************************************
BOOL CExcelOperate::SaveWorkbook() //文件是以開啟形式,儲存。
{
if (!m_ecBook.m_lpDispatch)
{
AfxMessageBox("Book獲取失敗!", MB_OK|MB_ICONWARNING);
return FALSE;
}
m_ecBook.Save();
return TRUE;
}
BOOL CExcelOperate::SaveWorkbookAs(CString fileName)//文件以建立形式,儲存。
{
if (!m_ecBook.m_lpDispatch)
{
AfxMessageBox("Book獲取失敗!", MB_OK|MB_ICONWARNING);
return FALSE;
}
COleVariant vTrue((short)TRUE),
vFalse((short)FALSE),
vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
COleVariant vFileName(_T(fileName));
m_ecBook.SaveAs(
vFileName, //VARIANT* FileName
vOptional, //VARIANT* FileFormat
vOptional, //VARIANT* LockComments
vOptional, //VARIANT* Password
vOptional, //VARIANT* AddToRecentFiles
vOptional, //VARIANT* WritePassword
0, //VARIANT* ReadOnlyRecommended
vOptional, //VARIANT* EmbedTrueTypeFonts
vOptional, //VARIANT* SaveNativePictureFormat
vOptional, //VARIANT* SaveFormsData
vOptional, //VARIANT* SaveAsAOCELetter
vOptional //VARIANT* ReadOnlyRecommended
/* vOptional, //VARIANT* EmbedTrueTypeFonts
vOptional, //VARIANT* SaveNativePictureFormat
vOptional, //VARIANT* SaveFormsData
vOptional //VARIANT* SaveAsAOCELetter*/
);
return TRUE;
}
BOOL CExcelOperate::CloseWorkbook()
{
COleVariant vTrue((short)TRUE),
vFalse((short)FALSE),
vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
m_ecBook.Close(vFalse, // SaveChanges.
vTrue, // OriginalFormat.
vFalse // RouteDocument.
);
m_ecBook = m_ecApp.GetActiveWorkbook();
if(!m_ecBook.m_lpDispatch)
{
AfxMessageBox("Book獲取失敗!", MB_OK|MB_ICONWARNING);
return FALSE;
}
if(CreateSheet(1) == FALSE)
{
return FALSE;
}
return TRUE;
}
void CExcelOperate::CloseApp()
{
SaveWorkbook();
COleVariant vTrue((short)TRUE),
vFalse((short)FALSE),
vOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
//m_ecDoc.Save();
m_ecApp.Quit();
if(m_ecRange.m_lpDispatch)
m_ecRange.ReleaseDispatch();
if(m_ecSheet.m_lpDispatch)
m_ecSheet.ReleaseDispatch();
if(m_ecSheets.m_lpDispatch)
m_ecSheets.ReleaseDispatch();
if(m_ecBook.m_lpDispatch)
m_ecBook.ReleaseDispatch();
if(m_ecBooks.m_lpDispatch)
m_ecBooks.ReleaseDispatch();
if(m_ecApp.m_lpDispatch)
m_ecApp.ReleaseDispatch();
}
BOOL CExcelOperate::GetRangeAndValue(CString begin, CString end)
{
if(!m_ecSheet.m_lpDispatch)
{
AfxMessageBox("Sheet獲取失敗!", MB_OK|MB_ICONWARNING);
return FALSE;
}
m_ecRange = m_ecSheet.GetRange(COleVariant(begin), COleVariant(end));
if(!m_ecRange.m_lpDispatch)
{
AfxMessageBox("Range獲取失敗!", MB_OK|MB_ICONWARNING);
return FALSE;
}
ret = m_ecRange.GetValue2();//得到表格中的值
return TRUE;
}
void CExcelOperate::GetRowsAndCols(long &rows, long &cols)
{
COleSafeArray sa(ret);
sa.GetUBound(1,&rows);
sa.GetUBound(2,&cols);
}
//只返回CString型別的,其他型別概視為錯誤
BOOL CExcelOperate::GetTheValue(int rows, int cols, CString &dest)
{
long rRows, rCols;
long index[2];
VARIANT val;
COleSafeArray sa(ret);
sa.GetUBound(1,&rRows);
sa.GetUBound(2,&rCols);
if(rows < 1 || cols < 1 || rRows < rows || rCols < cols)
{
AfxMessageBox("出錯點1");
return FALSE;
}
index[0]=rows;
index[1]=cols;
sa.GetElement(index,&val);
if(val.vt != VT_BSTR)
{
CString str;
str.Format("出錯點2, %d",val.vt);
AfxMessageBox(str);
return FALSE;
}
dest = val.bstrVal;
return TRUE;
}
//將beginS到endS之間設定為文字格式
BOOL CExcelOperate::SetTextFormat(CString &beginS, CString &endS)
{
if(GetRangeAndValue(beginS, endS))
{
m_ecRange.Select();
m_ecRange.SetNumberFormatLocal(COleVariant("@"));
return TRUE;
}
return FALSE;
}
//將beginS到endS之間(必須是一列)設定為真正的文字格式
BOOL CExcelOperate::SetRowToTextFormat(CString &beginS, CString &endS)
{
if(GetRangeAndValue(beginS, endS))
{
m_ecRange.Select();
Range m_tempRange = m_ecSheet.GetRange(COleVariant(beginS), COleVariant(beginS));
if(!m_tempRange.m_lpDispatch) return FALSE;
COleVariant vTrue((short)TRUE),
vFalse((short)FALSE);
//int tempArray[2] = {1, 2};
COleSafeArray saRet;
DWORD numElements = {2};
saRet.Create(VT_I4, 1, &numElements);
long index = 0;
int val = 1;
saRet.PutElement(&index, &val);
index++;
val = 2;
saRet.PutElement(&index, &val);
//m_tempRange.GetItem(COleVariant((short)5),COleVariant("A"));
m_ecRange.TextToColumns(m_tempRange.GetItem(COleVariant((short)1),COleVariant((short)1)), 1, 1, vFalse, vTrue, vFalse, vFalse, vFalse, vFalse, vFalse, saRet, vFalse, vFalse, vTrue);
m_tempRange.ReleaseDispatch();
return TRUE;
}
return FALSE;
}