296 lines
6.0 KiB
C++
296 lines
6.0 KiB
C++
#include "stdafx.h"
|
|
#include "TExcelOperatorMuster.h"
|
|
#include "comdef.h"
|
|
|
|
#ifdef _DEBUG
|
|
#undef THIS_FILE
|
|
static char THIS_FILE[]=__FILE__;
|
|
#define new DEBUG_NEW
|
|
#endif
|
|
|
|
TExcelOperatorMuster::TExcelOperatorMuster()
|
|
{
|
|
m_bIsReadOpen = FALSE;
|
|
m_strPath = _T("");
|
|
}
|
|
|
|
TExcelOperatorMuster::~TExcelOperatorMuster()
|
|
{
|
|
|
|
}
|
|
|
|
// 设置excel表格路径
|
|
void TExcelOperatorMuster::SetExcelFilePath(LPCTSTR pszPath)
|
|
{
|
|
if (pszPath != NULL)
|
|
{
|
|
m_strPath = pszPath;
|
|
}
|
|
}
|
|
|
|
BOOL TExcelOperatorMuster::OpenExcelSheet(short nSheet,BOOL bRead) // 打开指定索引的页
|
|
{
|
|
if (m_strPath.IsEmpty())
|
|
{
|
|
return FALSE;
|
|
}
|
|
|
|
m_bIsReadOpen = bRead;
|
|
::CoInitialize(NULL);
|
|
|
|
LPDISPATCH pLpDisp = NULL;
|
|
COleVariant vResult;
|
|
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
|
|
COleVariant covOpenStyle((short)TRUE);
|
|
if (!bRead)
|
|
covOpenStyle = (short)FALSE;
|
|
Ex_Range range;
|
|
BOOL bSuccess = m_ExcelApp.CreateDispatch(_T("excel.application"));
|
|
if (bSuccess)
|
|
{
|
|
m_ExcelBooks.AttachDispatch(m_ExcelApp.GetWorkbooks());
|
|
pLpDisp = m_ExcelBooks.Open(m_strPath,covOptional, covOpenStyle,
|
|
covOptional, covOptional, covOptional,covOptional, covOptional,
|
|
covOptional, covOptional, covOptional,covOptional, covOptional);
|
|
//得到Workbook
|
|
if (pLpDisp != NULL)
|
|
{
|
|
m_ExcelBook.AttachDispatch(pLpDisp);
|
|
//得到Worksheets
|
|
m_ExcelSheets.AttachDispatch(m_ExcelBook.GetWorksheets());
|
|
//获取sheet 数量
|
|
long lCount = m_ExcelSheets.GetCount();
|
|
if (lCount > 0)
|
|
{
|
|
//获取指定索引的sheet
|
|
COleVariant varIndex(nSheet);
|
|
pLpDisp = m_ExcelSheets.GetItem(varIndex);
|
|
if (pLpDisp != NULL)
|
|
{
|
|
m_ExcelSheet.AttachDispatch(pLpDisp);
|
|
}
|
|
else
|
|
{
|
|
m_ExcelBooks.Close();
|
|
m_ExcelApp.Quit();
|
|
::CoUninitialize();
|
|
return FALSE;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
m_ExcelApp.Quit();
|
|
::CoUninitialize();
|
|
return FALSE;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
::CoUninitialize();
|
|
return FALSE;
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
void TExcelOperatorMuster::CloseExcelSheet() // 关闭excel
|
|
{
|
|
// 以写打开要保存
|
|
if (!m_bIsReadOpen)
|
|
{
|
|
m_ExcelBook.Save();
|
|
}
|
|
m_ExcelBooks.Close();
|
|
m_ExcelApp.Quit();
|
|
::CoUninitialize();
|
|
}
|
|
|
|
void TExcelOperatorMuster::Show()
|
|
{
|
|
Ex_Range range;
|
|
range.GetApplication();
|
|
range.Show();
|
|
}
|
|
|
|
// 读取数据
|
|
BOOL TExcelOperatorMuster::ReadDataFromExcelFile(CString& strValue,LPCTSTR pszSp,LPCTSTR pszEp)
|
|
{
|
|
if (pszSp == NULL)
|
|
{
|
|
return FALSE;
|
|
}
|
|
Ex_Range range;
|
|
if (pszEp == NULL)
|
|
{
|
|
range = m_ExcelSheet.GetRange(COleVariant(pszSp),COleVariant(pszSp));
|
|
}
|
|
else
|
|
{
|
|
range = m_ExcelSheet.GetRange(COleVariant(pszSp),COleVariant(pszEp));
|
|
}
|
|
COleVariant varText = range.GetValue();
|
|
strValue.Empty();
|
|
switch(varText.vt)
|
|
{
|
|
case VT_I2:
|
|
case VT_I4:
|
|
case VT_I8:
|
|
case VT_UI8:
|
|
case VT_INT:
|
|
case VT_UINT:
|
|
{
|
|
strValue.Format(_T("%d"),varText.intVal);
|
|
break;
|
|
}
|
|
case VT_R4:
|
|
case VT_R8:
|
|
{
|
|
strValue.Format(_T("%g"),varText.dblVal);
|
|
break;
|
|
}
|
|
case VT_LPSTR:
|
|
case VT_LPWSTR:
|
|
case VT_BSTR:
|
|
{
|
|
_bstr_t sText = (*((_variant_t*)&varText)).bstrVal;
|
|
strValue = (TCHAR*)sText;
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return !strValue.IsEmpty();
|
|
}
|
|
|
|
BOOL TExcelOperatorMuster::ReadDataFromXls(CString& strValue, CString strCol, int nRow)
|
|
{
|
|
if(nRow < 0)
|
|
return false;
|
|
CString strRow;
|
|
strRow.Format(_T("%d"),nRow);
|
|
if(!ReadDataFromExcelFile(strValue, strCol+strRow, strCol+strRow))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
// 写入数据
|
|
BOOL TExcelOperatorMuster::WriteDataToExcelFile(LPCTSTR pszValue,LPCTSTR pszSp,LPCTSTR pszEp)
|
|
{
|
|
if (pszSp == NULL)
|
|
{
|
|
return FALSE;
|
|
}
|
|
Ex_Range range;
|
|
if (pszEp == NULL)
|
|
{
|
|
range = m_ExcelSheet.GetRange(COleVariant(pszSp),COleVariant(pszSp));
|
|
}
|
|
else
|
|
{
|
|
range = m_ExcelSheet.GetRange(COleVariant(pszSp),COleVariant(pszEp));
|
|
}
|
|
COleVariant varText(pszValue);
|
|
range.SetValue(varText);
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL TExcelOperatorMuster::WriteDataToExcelFile(int nValue,LPCTSTR pszSp,LPCTSTR pszEp)
|
|
{
|
|
if (pszSp == NULL)
|
|
{
|
|
return FALSE;
|
|
}
|
|
Ex_Range range;
|
|
if (pszEp == NULL)
|
|
{
|
|
range = m_ExcelSheet.GetRange(COleVariant(pszSp),COleVariant(pszSp));
|
|
}
|
|
else
|
|
{
|
|
range = m_ExcelSheet.GetRange(COleVariant(pszSp),COleVariant(pszEp));
|
|
}
|
|
COleVariant varText((short)nValue);
|
|
range.SetValue(varText);
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL TExcelOperatorMuster::WriteDataToExcelFile(double dValue,LPCTSTR pszSp,LPCTSTR pszEp)
|
|
{
|
|
if (pszSp == NULL)
|
|
{
|
|
return FALSE;
|
|
}
|
|
Ex_Range range;
|
|
if (pszEp == NULL)
|
|
{
|
|
range = m_ExcelSheet.GetRange(COleVariant(pszSp),COleVariant(pszSp));
|
|
}
|
|
else
|
|
{
|
|
range = m_ExcelSheet.GetRange(COleVariant(pszSp),COleVariant(pszEp));
|
|
}
|
|
COleVariant varText(dValue);
|
|
range.SetValue(varText);
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
BOOL TExcelOperatorMuster::WriteDataToXls(LPCTSTR pszValue, CString strCol, int nRow)
|
|
{
|
|
if(nRow < 0)
|
|
return false;
|
|
CString strRow;
|
|
strRow.Format(_T("%d"),nRow);
|
|
if(!WriteDataToExcelFile(pszValue, strCol+strRow, strCol+strRow))
|
|
return false;
|
|
return true;
|
|
}
|
|
BOOL TExcelOperatorMuster::WriteDataToXls(int nValue, CString strCol, int nRow)
|
|
{
|
|
if(nRow < 0)
|
|
return false;
|
|
CString strRow;
|
|
strRow.Format(_T("%d"),nRow);
|
|
if(!WriteDataToExcelFile(nValue, strCol+strRow, strCol+strRow))
|
|
return false;
|
|
return true;
|
|
}
|
|
BOOL TExcelOperatorMuster::WriteDataToXls(double dValue, CString strCol, int nRow)
|
|
{
|
|
if(nRow < 0)
|
|
return false;
|
|
CString strRow;
|
|
strRow.Format(_T("%d"),nRow);
|
|
if(!WriteDataToExcelFile(dValue, strCol+strRow, strCol+strRow))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
// 获取当前页的用户行数
|
|
long TExcelOperatorMuster::GetUserTableRowNum()
|
|
{
|
|
//读取已经使用区域的信息
|
|
Ex_Range usedRange,range;
|
|
usedRange.AttachDispatch(m_ExcelSheet.GetUsedRange());
|
|
//已经使用的行数
|
|
range.AttachDispatch(usedRange.GetRows());
|
|
long nRowNum = range.GetCount();
|
|
|
|
return nRowNum;
|
|
}
|
|
|
|
// 获取当前页的用户列数
|
|
long TExcelOperatorMuster::GetUserTableColNum()
|
|
{
|
|
//读取已经使用区域的信息
|
|
Ex_Range usedRange,range;
|
|
usedRange.AttachDispatch(m_ExcelSheet.GetUsedRange());
|
|
//已经使用的列数
|
|
range.AttachDispatch(usedRange.GetColumns());
|
|
long nColNum = range.GetCount();
|
|
|
|
return nColNum;
|
|
} |