头文件 //LibXL\3.4.2
#pragma once
#include "libxl.h"
using namespace libxl;
class CReadExcelFile
{
public:
CReadExcelFile();
CReadExcelFile(CString strFileName);
~CReadExcelFile();
public:
bool ReadAllSheetNames(CStringArray& arrSheetNm);
int GetExcelRowsCount(int index);
int GetExcelColumsCount(int index);
CString GetCellStrValue(CString strSheetName, int nRow, int nCol);
CString GetCellStrValue(int indexSheet, int nRow, int nCol);
double GetCellDouValue(CString strSheetName, int nRow, int nCol);
double GetCellDouValue(int indexSheet, int nRow, int nCol);
CellType GetCellType(CString strSheetName, int nRow, int nCol);
CellType GetCellType(int indexSheet,int nRow, int nCol);
int GetSheetIndexFromName(CString strSheetName);
bool SetActiveSheet(int index);
private:
Book* m_book;
};
源文件
#include "StdAfx.h"
#include "ReadExcelFile.h"
CReadExcelFile::CReadExcelFile()
{
}
CReadExcelFile::CReadExcelFile( CString strFileName )
{
//参考——HongYe book Tongxing
m_book = xlCreateBook();
m_book->setKey(_T(" ** ** Co.,Ltd"), _T("windows"));
if (NULL == m_book)
{
::MessageBox(acedGetAcadFrame()->GetSafeHwnd(), _T("打开EXCEL失败"), _T("提示"), MB_OK|MB_ICONINFORMATION);
return;
}
if (m_book)
{
m_book->load(strFileName);
}
}
bool CReadExcelFile::ReadAllSheetNames(CStringArray& arrSheetNm)
{
if (m_book)
{
for (int i = 0; i < m_book->sheetCount(); i++)
{
CString strName = m_book->getSheet(i)->name();
arrSheetNm.Add(strName);
}
}
return !arrSheetNm.IsEmpty();
}
CString CReadExcelFile::GetCellStrValue( CString strSheetName, int nRow, int nCol )
{
int index = GetSheetIndexFromName(strSheetName);
return GetCellStrValue(index, nRow, nCol);
}
CString CReadExcelFile::GetCellStrValue( int indexSheet, int nRow, int nCol )
{
CString strRet = _T("");
if(m_book)
{
if (indexSheet < m_book->sheetCount() && indexSheet >= 0)
{
strRet = m_book->getSheet(indexSheet)->readStr(nRow, nCol);
}
}
return strRet;
}
double CReadExcelFile::GetCellDouValue( CString strSheetName, int nRow, int nCol )
{
int index = GetSheetIndexFromName(strSheetName);
return GetCellDouValue(index, nRow, nCol);
}
double CReadExcelFile::GetCellDouValue( int indexSheet, int nRow, int nCol )
{
double dRet = 0;
if(m_book)
{
if (indexSheet < m_book->sheetCount() && indexSheet >= 0)
dRet = m_book->getSheet(indexSheet)->readNum(nRow, nCol);
}
return dRet;
}
CellType CReadExcelFile::GetCellType(CString strSheetName, int nRow, int nCol)
{
int index = GetSheetIndexFromName(strSheetName);
return GetCellType(index, nRow, nCol);
}
CellType CReadExcelFile::GetCellType(int indexSheet,int nRow, int nCol)
{
CellType CellType = libxl::CELLTYPE_STRING;
if(m_book)
{
if (indexSheet < m_book->sheetCount() && indexSheet >= 0)
CellType = m_book->getSheet(indexSheet)->cellType(nRow, nCol);
}
return CellType;
}
int CReadExcelFile::GetSheetIndexFromName( CString strSheetName )
{
int index = -1;
if (m_book)
{
for (int i = 0; i < m_book->sheetCount(); i++)
{
CString strName = m_book->getSheet(i)->name();
if (strSheetName.CompareNoCase(strName) == 0)
{
index = i;
break;
}
}
}
return index;
}
CReadExcelFile::~CReadExcelFile()
{
m_book->release();
}
int CReadExcelFile::GetExcelRowsCount(int index)
{
if (m_book)
{
return m_book->getSheet(index)->lastRow();
}
return 0;
}
int CReadExcelFile::GetExcelColumsCount(int index)
{
if (m_book)
{
return m_book->getSheet(index)->lastCol();
}
return 0;
}
bool CReadExcelFile::SetActiveSheet( int index )
{
m_book->setActiveSheet(index);
return true;
}
// 简单应用
bool CProjectDlg::CheckExcelFile(CReadExcelFile* pReadData, int indexSheet)
{
int nColumCount = pReadData->GetExcelColumsCount(indexSheet);
int nRowCount = pReadData->GetExcelRowsCount(indexSheet);
if (nColumCount != 9)
{
acutPrintf(_T("表格列数不正确!\n"));
return false;
}
}
XTPReportContrl控件常用样式设定:
m_xtpReportCtrl.ModifyStyle( 0, WS_CHILD|WS_TABSTOP|WS_VISIBLE|WM_VSCROLL|WM_HSCROLL|WS_BORDER|WS_CLIPSIBLINGS, 0 );
m_xtpReportCtrl.AddColumn(new CXTPReportColumn(0, _T("序号"), 40, FALSE, XTP_REPORT_NOICON, FALSE));
m_xtpReportCtrl.GetColumns()->GetAt(0)->SetEditable(FALSE);
m_xtpReportCtrl.GetColumns()->GetAt(0)->SetAlignment(DT_CENTER);
m_xtpReportCtrl.AddColumn(new CXTPReportColumn(1, _T("类型"), 70, TRUE, XTP_REPORT_NOICON));
m_xtpReportCtrl.AddColumn(new CXTPReportColumn(2, _T("直径"), 120, TRUE, ICON_CHECK));
m_xtpReportCtrl.AddColumn(new CXTPReportColumn(3, _T("个数"), 100, TRUE, ICON_CHECK));
m_xtpReportCtrl.AddColumn(new CXTPReportColumn(4, _T("路径"), 120, TRUE, ICON_CHECK));
CXTPWellTypeReportPaintManager* pPaintManager = new CXTPWellTypeReportPaintManager(this);
m_xtpReportCtrl.SetPaintManager(pPaintManager);
// 设置样式
m_xtpReportCtrl.SetGridStyle(TRUE, xtpReportGridSolid);
m_xtpReportCtrl.SetMultipleSelection(TRUE);
m_xtpReportCtrl.AllowEdit(TRUE);
m_xtpReportCtrl.EditOnClick(TRUE);
m_xtpReportCtrl.GetReportHeader()->AllowColumnSort(FALSE);
m_xtpReportCtrl.GetReportHeader()->AllowColumnReorder(FALSE);
m_xtpReportCtrl.GetReportHeader()->AllowColumnResize(TRUE);
m_xtpReportCtrl.GetReportHeader()->AllowColumnRemove(FALSE);
m_xtpReportCtrl.GetReportHeader()->SetAutoColumnSizing(FALSE);
m_xtpReportCtrl.AdjustScrollBars();
m_xtpReportCtrl.Populate();
// 读取上一次的EXCEL文件目录
CString strLastFolderPath = CPipePubUtils::getSzgxPz(HyGetUserDataDir() + _T("Data\\CustomStandard.ini"), _T("CustomStandard"), _T("StandardFilePath"), _T(""));
if (!strLastFolderPath.IsEmpty())
{
strPath = strLastFolderPath;
}
lpszOpenFile->m_ofn.lpstrInitialDir = strPath;