// GridCtrlView.cpp : implementation file
//
#include "stdafx.h"
#include "iDART_UI.h"
#include "iDART_UIDoc.h"
#include "GridCtrlView.h"
//////////////////////////////////////////////////////////////////////////
// GridCtrlView
IMPLEMENT_DYNCREATE(GridCtrlView, CView)
//有消息时循环暂停和挂起
BOOL PeekAndPump()
{
static MSG msg;
while (::PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)) {
if (!AfxGetApp()->PumpMessage()) {
::PostQuitMessage(0);
return FALSE;
}
}
return TRUE;
}
GridCtrlView::GridCtrlView()
{
m_pGridCtrl = NULL;
CString str = theApp.m_curFileListPath;
int last=str.ReverseFind('.');
if(
str.Mid(last+1, str.GetLength()-last).MakeLower()=="csv"||
str.Mid(last+1, str.GetLength()-last).MakeLower()=="xls")
{
m_curFilePath = str;
}else
{
m_curFilePath = "";
MessageBox("无法读取文件");
}
}
GridCtrlView::~GridCtrlView()
{
if (m_pGridCtrl)
delete m_pGridCtrl;
if (m_pEx)
{
delete m_pEx;
}
}
BEGIN_MESSAGE_MAP(GridCtrlView, CView)
ON_WM_SIZE()
ON_WM_ERASEBKGND()
ON_WM_TIMER()
END_MESSAGE_MAP()
// GridCtrlView drawing
void GridCtrlView::OnDraw(CDC* pDC)
{
CDocument* pDoc = GetDocument();
// TODO: add draw code here
ASSERT_VALID(pDoc);
if (!pDoc)
return;
}
// GridCtrlView diagnostics
#ifdef _DEBUG
void GridCtrlView::AssertValid() const
{
CView::AssertValid();
}
CiDART_UIDoc* GridCtrlView::GetDocument() const // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CiDART_UIDoc)));
return (CiDART_UIDoc*)m_pDocument;
}
#ifndef _WIN32_WCE
void GridCtrlView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
#endif
#endif //_DEBUG
// GridCtrlView message handlers
void GridCtrlView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
CDocument* pDoc = GetDocument();
// TODO: add draw code here
ASSERT_VALID(pDoc);
if (!pDoc)
return;
if (m_curFilePath == "")
{
return;
}
fillGridView();
}
void GridCtrlView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if (m_pGridCtrl->GetSafeHwnd())
{
CRect rect;
GetClientRect(rect);
m_pGridCtrl->MoveWindow(rect);
}
}
BOOL GridCtrlView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return CView::OnEraseBkgnd(pDC);
}
BOOL GridCtrlView::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
// TODO: Add your specialized code here and/or call the base class
return CView::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}
void GridCtrlView::OnTimer(UINT_PTR nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CView::OnTimer(nIDEvent);
}
void GridCtrlView::fillGridView(void)
{
if (m_curFilePath == "")
{
return;
}
COleSafeArray strXls;
m_pEx = new excel();
int _row = 0;
int _low = 0;
int nPos = 0;
bool bRead, bColumnName;
bRead = m_pEx->ReadExcelToString(m_curFilePath, strXls, &_row, &_low, true);
if (!bRead)
{
MessageBox("读取表%s失败...", m_curFilePath);
return ;
}
long lNumRows;
long lNumCols;
strXls.GetUBound(1, &lNumRows);
strXls.GetUBound(2, &lNumCols);
BeginWaitCursor();
if (m_pGridCtrl == NULL)
{
m_pGridCtrl = new CGridCtrl;
if (!m_pGridCtrl)
{
return ;
}
//创建
CRect rect;
GetClientRect(rect);
m_pGridCtrl->Create(rect, this, 100);
// 画表格填充数据
m_pGridCtrl->SetEditable(TRUE);
m_pGridCtrl->EnableDragAndDrop(TRUE);
try {
m_pGridCtrl->SetRowCount(lNumRows);
m_pGridCtrl->SetColumnCount(lNumCols+1);
m_pGridCtrl->SetFixedRowCount(1);
m_pGridCtrl->SetFixedColumnCount(1);
}
catch (CMemoryException* e)
{
e->ReportError();
e->Delete();
return ;
}
long maxNum = (lNumRows*lNumCols)/1000;
long i = 0;
int j = 0;
long index[2];
VARIANT val;
COleVariant rValue;
// fill rows/cols with text
for (int row = 0; row <= lNumRows; row++)
for (int col = 0; col <=lNumCols; col++)
{
PeekAndPump();
i++;
GV_ITEM Item;
Item.mask = GVIF_TEXT|GVIF_FORMAT;
Item.row = row;
Item.col = col;
if (row < 1) {
Item.nFormat = DT_LEFT|DT_WORDBREAK;
if (col == 0)
{
Item.strText= " ";
}else
{
Item.strText.Format("%c",'A'+col-1);
}
} else if (col < 1) {
Item.nFormat = DT_RIGHT|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS|DT_NOPREFIX;
Item.strText.Format(_T("%d"),row);
} else {
Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS|DT_NOPREFIX;
CString str ;
index[0]=row;
index[1]=col;
strXls.GetElement(index, &val);
rValue = COleVariant(val);
rValue.ChangeType(VT_BSTR);
str = rValue.bstrVal;
Item.strText =str;
}
m_pGridCtrl->SetItem(&Item);
if (i%maxNum == 0)
{
j++;
theApp.m_pMainWnd->SendMessage(MYWM_PROGRESS,j,0);
}
}
m_pGridCtrl->AutoSize();
theApp.m_pMainWnd->SendMessage(MYWM_PROGRESS,0,0);
}
EndWaitCursor();
GetParent()->SetWindowText(theApp.m_FileListItem);
}