www.pudn.com > ExcelChoose.zip > ExcelChooseDlg.cpp, change:2016-09-12,size:29461b


// ExcelChooseDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "ExcelChoose.h" 
#include "ExcelChooseDlg.h" 
 
#include "excel.h" 
#include <comdef.h> 
#include <comutil.h> 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CExcelChooseDlg dialog 
 
CExcelChooseDlg::CExcelChooseDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CExcelChooseDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CExcelChooseDlg) 
	m_bCheck = FALSE; 
	m_strFilePath = _T(""); 
	m_nLen = 16; 
	m_dMoney = 0.0; 
	m_nNumb = 0; 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CExcelChooseDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CExcelChooseDlg) 
	DDX_Control(pDX, IDC_LIST1, m_list1); 
	DDX_Control(pDX, IDC_COMBO_TYPE, m_combType); 
	DDX_Control(pDX, IDC_COMBO_NAME, m_combName); 
	DDX_Control(pDX, IDC_COMBO_DPNAME, m_combDpName); 
	DDX_Check(pDX, IDC_CHECK1, m_bCheck); 
	DDX_Text(pDX, IDC_EDIT_FILEPATH, m_strFilePath); 
	DDX_Text(pDX, IDC_EDIT_LEN, m_nLen); 
	DDX_Text(pDX, IDC_EDIT_TOTALMONEY, m_dMoney); 
	DDX_Text(pDX, IDC_EDIT_TOTALNUM, m_nNumb); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CExcelChooseDlg, CDialog) 
	//{{AFX_MSG_MAP(CExcelChooseDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BUTTON_CHOOSE, OnButtonChoose) 
	ON_BN_CLICKED(IDC_BUTTON_EMPORT, OnButtonEmport) 
	ON_BN_CLICKED(IDC_CHECK1, OnCheck1) 
	ON_CBN_SELCHANGE(IDC_COMBO_DPNAME, OnSelchangeComboDpname) 
	ON_CBN_SELCHANGE(IDC_COMBO_NAME, OnSelchangeComboName) 
	ON_CBN_SELCHANGE(IDC_COMBO_TYPE, OnSelchangeComboType) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CExcelChooseDlg message handlers 
 
BOOL CExcelChooseDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	// TODO: Add extra initialization here 
	DWORD dwStyle = m_list1.GetExtendedStyle(); 
	dwStyle |= LVS_EX_FULLROWSELECT;				//	选中某行使整行高亮(只适用与report风格的listctrl) 
	dwStyle |= LVS_EX_GRIDLINES;					//	网格线(只适用与report风格的listctrl) 
	 
	m_list1.SetExtendedStyle(dwStyle);				//	设置扩展风格 
    m_list1.InsertColumn( 0, "序号", LVCFMT_LEFT, 70 ); 
	m_list1.InsertColumn( 1, "商品名称", LVCFMT_LEFT, 380 ); 
	m_list1.InsertColumn( 2, "商品类型", LVCFMT_LEFT, 180 ); 
	m_list1.InsertColumn( 3, "店铺名称", LVCFMT_LEFT, 180 ); 
	m_list1.InsertColumn( 4, "金额", LVCFMT_LEFT, 60 ); 
	m_list1.InsertColumn( 5, "销售数量", LVCFMT_LEFT, 60 ); 
 
	m_combDpName.AddString("全部"); 
	m_combName.AddString("全部"); 
	m_combType.AddString("全部"); 
	m_combType.SetCurSel(0); 
	m_combName.SetCurSel(0); 
	m_combDpName.SetCurSel(0); 
 
	SYSTEMTIME st; 
	GetLocalTime(&st); 
	CString strDate; 
	strDate.Format("%4d%02d%02d",st.wYear,st.wMonth,st.wDay);  
	MessageBox("此版本为测试版,有使用次数限制!", "注意", MB_ICONWARNING | MB_OK); 
	if (strDate != "20160912" ) 
	{ 
		exit(0); 
 	} 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CExcelChooseDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CExcelChooseDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CExcelChooseDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CExcelChooseDlg::OnButtonChoose()  
{ 
	// TODO: Add your control notification handler code here 
	CFileDialog dlg(TRUE, 
        NULL,NULL, 
        OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
        (LPCTSTR)_TEXT("xls Files (*.xls)|*.xls|xlsx Files (*.xlsx)|*.xlsx|All Files (*.*)|*.*||"), 
        NULL); 
	 
    if(dlg.DoModal()==IDOK)	 
    {	 
        m_strFilePath = dlg.GetPathName(); 
		UpdateData(FALSE); 
		GetAllData(); 
    } 
	else 
		return; 
} 
 
 
void CExcelChooseDlg::GetAllData() 
{ 
	UpdateData(TRUE); 
	if (m_strFilePath == "") 
	{ 
		MessageBox("请先选择excel文件!"); 
		return; 
	} 
	m_data1.RemoveAll(); 
	m_data2.RemoveAll(); 
	m_dataList.RemoveAll(); 
	my_Map1.RemoveAll(); 
	my_Map2.RemoveAll(); 
	my_Map3.RemoveAll(); 
	// TODO: Add your control notification handler code here 
	excelnamespace::_Application app; 
	excelnamespace::Workbooks books; 
	excelnamespace::_Workbook book; 
	excelnamespace::Worksheets sheets; 
	excelnamespace::_Worksheet sheet; 
	excelnamespace::Range range; 
	LPDISPATCH lpDisp;     
	COleVariant vResult; 
 
 
	CString str = ""; 
	 
	COleVariant 
		covTrue((short)TRUE), 
		covFalse((short)FALSE), 
		covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); 
	 
	//创建Excel 2003服务器(启动Excel) 
	if (!app.CreateDispatch("Excel.Application",NULL))  
	{  
		AfxMessageBox("Create Excel service failure!"); 
		return; 
	} 
 
	// 设置为FALSE时,后面的app.Quit();注释要打开 
	// 否则EXCEL.EXE进程会一直存在,并且每操作一次就会多开一个进程 
	app.SetVisible(TRUE); 
	books.AttachDispatch(app.GetWorkbooks(),true); 
	 
	/* 
	*	打开一个工作簿。 
	*	Excel 2000 只需要13个参数就行,Excel 2003需要15个参数 
	*/ 
	lpDisp = books.Open(m_strFilePath, 
		covOptional, covOptional, covOptional, covOptional, 
		covOptional, covOptional, covOptional, covOptional, 
		covOptional, covOptional, covOptional, covOptional, 
		covOptional, covOptional); 
	ASSERT(lpDisp); 
 
	book.AttachDispatch(lpDisp); 
	 
	// 得到Worksheets   
	sheets.AttachDispatch(book.GetWorksheets(),true);  
	// 得到Worksheet 
	sheet.AttachDispatch(sheets.GetItem(_variant_t((short)(1)))); 	 
	// 得到全部Cells  
	range = sheet.GetUsedRange(); 
	range = range.GetRows(); 
	long usedRows = range.GetCount(); 
	range = sheet.GetUsedRange(); 
	range = range.GetColumns(); 
	long usedCols = range.GetCount(); 
	 
 	CString tempStr; 
// 	tempStr.Format("row = %d clo = %d", usedRows, usedCols); 
// 	MessageBox(tempStr); 
	 
	int i, j; 
	m_dMoney = 0; 
	m_nNumb = 0; 
	for (i = 2; i <= usedRows; i++) 
	{ 
		Data1 tempData; 
		memset(&tempData, 0, sizeof(Data1)); 
		range.AttachDispatch(sheet.GetCells(),true); 
		range.AttachDispatch(range.GetItem(_variant_t((long)(i)),_variant_t((long)(1))).pdispVal ); 
		vResult =range.GetValue(covOptional); 
		switch (vResult.vt) 
		{ 
		case VT_BSTR:		// 字符串 
			str=vResult.bstrVal; 
			break; 
		case VT_R8:			// 8字节的数字 
			str.Format("%.2lf",vResult.dblVal); 
			break; 
		case VT_DATE:		// 时间格式 
			SYSTEMTIME st; 
			VariantTimeToSystemTime(vResult.date, &st); 
			str.Format("%4d/%d/%d", st.wYear, st.wMonth, st.wDay); 
			break; 
		case VT_EMPTY:		// 单元格空的  
			str=""; 
			break; 
		} 
		if (vResult.vt == VT_BSTR) 
		{ 
			str=vResult.bstrVal; 
		} 
		str.TrimRight(); 
		str.TrimLeft(); 
		memcpy(tempData.id, str, str.GetLength()); 
 
		range.AttachDispatch(sheet.GetCells(),true); 
		range.AttachDispatch(range.GetItem(_variant_t((long)(i)),_variant_t((long)(4))).pdispVal ); 
		vResult =range.GetValue(covOptional); 
		switch (vResult.vt) 
		{ 
		case VT_BSTR:		// 字符串 
			str=vResult.bstrVal; 
			break; 
		case VT_R8:			// 8字节的数字 
			str.Format("%.2lf",vResult.dblVal); 
			break; 
		case VT_DATE:		// 时间格式 
			SYSTEMTIME st; 
			VariantTimeToSystemTime(vResult.date, &st); 
			str.Format("%4d/%d/%d", st.wYear, st.wMonth, st.wDay); 
			break; 
		case VT_EMPTY:		// 单元格空的  
			str=""; 
			break; 
		} 
		if (vResult.vt == VT_BSTR) 
		{ 
			str=vResult.bstrVal; 
		} 
		str.TrimLeft('¥'); 
		str.TrimLeft(); 
		str.TrimRight(); 
		tempData.price = atof(str); 
 
		range.AttachDispatch(sheet.GetCells(),true); 
		range.AttachDispatch(range.GetItem(_variant_t((long)(i)),_variant_t((long)(5))).pdispVal ); 
		vResult =range.GetValue(covOptional); 
		switch (vResult.vt) 
		{ 
		case VT_BSTR:		// 字符串 
			str=vResult.bstrVal; 
			break; 
		case VT_R8:			// 8字节的数字 
			str.Format("%.2lf",vResult.dblVal); 
			break; 
		case VT_DATE:		// 时间格式 
			SYSTEMTIME st; 
			VariantTimeToSystemTime(vResult.date, &st); 
			str.Format("%4d/%d/%d", st.wYear, st.wMonth, st.wDay); 
			break; 
		case VT_EMPTY:		// 单元格空的  
			str=""; 
			break; 
		} 
		if (vResult.vt == VT_BSTR) 
		{ 
			str=vResult.bstrVal; 
		} 
		str.TrimLeft('¥'); 
		str.TrimLeft(); 
		str.TrimRight(); 
		double zf = atof(str); 
 
		if (strcmp(tempData.id, "")  != 0 && zf >= 0) 
		{ 
			m_data1.Add(tempData); 
		} 
		 
		//memcpy(tempData.startNumb, str, str.GetLength()); 
		 
 
		Data2 tempData2; 
		memset(&tempData2, 0, sizeof(Data2)); 
		range.AttachDispatch(sheet.GetCells(),true); 
		range.AttachDispatch(range.GetItem(_variant_t((long)(i)),_variant_t((long)(8))).pdispVal ); 
		vResult =range.GetValue(covOptional); 
		switch (vResult.vt) 
		{ 
		case VT_BSTR:		// 字符串 
			str=vResult.bstrVal; 
			break; 
		case VT_R8:			// 8字节的数字 
			str.Format("%.2lf",vResult.dblVal); 
			break; 
		case VT_DATE:		// 时间格式 
			SYSTEMTIME st; 
			VariantTimeToSystemTime(vResult.date, &st); 
			str.Format("%4d/%d/%d", st.wYear, st.wMonth, st.wDay); 
			break; 
		case VT_EMPTY:		// 单元格空的  
			str=""; 
			break; 
		} 
		if (vResult.vt == VT_BSTR) 
		{ 
			str=vResult.bstrVal; 
		} 
		str.TrimRight(); 
		str.TrimLeft(); 
		memcpy(tempData2.id, str, str.GetLength()); 
 
		range.AttachDispatch(sheet.GetCells(),true); 
		range.AttachDispatch(range.GetItem(_variant_t((long)(i)),_variant_t((long)(9))).pdispVal ); 
		vResult =range.GetValue(covOptional); 
		switch (vResult.vt) 
		{ 
		case VT_BSTR:		// 字符串 
			str=vResult.bstrVal; 
			break; 
		case VT_R8:			// 8字节的数字 
			str.Format("%.2lf",vResult.dblVal); 
			break; 
		case VT_DATE:		// 时间格式 
			SYSTEMTIME st; 
			VariantTimeToSystemTime(vResult.date, &st); 
			str.Format("%4d/%d/%d", st.wYear, st.wMonth, st.wDay); 
			break; 
		case VT_EMPTY:		// 单元格空的  
			str=""; 
			break; 
		} 
		if (vResult.vt == VT_BSTR) 
		{ 
			str=vResult.bstrVal; 
		} 
		str.TrimLeft('¥'); 
		str.TrimLeft(); 
		str.TrimRight(); 
		tempData2.price = atof(str); 
		m_dMoney += tempData2.price; 
		//memcpy(tempData.date, str, str.GetLength()); 
 
		range.AttachDispatch(sheet.GetCells(),true); 
		range.AttachDispatch(range.GetItem(_variant_t((long)(i)),_variant_t((long)(12))).pdispVal ); 
		vResult =range.GetValue(covOptional); 
		switch (vResult.vt) 
		{ 
		case VT_BSTR:		// 字符串 
			str=vResult.bstrVal; 
			break; 
		case VT_R8:			// 8字节的数字 
			str.Format("%.2lf",vResult.dblVal); 
			break; 
		case VT_DATE:		// 时间格式 
			SYSTEMTIME st; 
			VariantTimeToSystemTime(vResult.date, &st); 
			str.Format("%4d/%d/%d", st.wYear, st.wMonth, st.wDay); 
			break; 
		case VT_EMPTY:		// 单元格空的  
			str=""; 
			break; 
		} 
		if (vResult.vt == VT_BSTR) 
		{ 
			str=vResult.bstrVal; 
		} 
		str.TrimRight(); 
		str.TrimLeft(); 
		tempData2.num = atoi(str); 
		m_nNumb += tempData2.num; 
		//memcpy(tempData.id, str, str.GetLength()); 
 
		range.AttachDispatch(sheet.GetCells(),true); 
		range.AttachDispatch(range.GetItem(_variant_t((long)(i)),_variant_t((long)(13))).pdispVal ); 
		vResult =range.GetValue(covOptional); 
		switch (vResult.vt) 
		{ 
		case VT_BSTR:		// 字符串 
			str=vResult.bstrVal; 
			break; 
		case VT_R8:			// 8字节的数字 
			str.Format("%.2lf",vResult.dblVal); 
			break; 
		case VT_DATE:		// 时间格式 
			SYSTEMTIME st; 
			VariantTimeToSystemTime(vResult.date, &st); 
			str.Format("%4d/%d/%d", st.wYear, st.wMonth, st.wDay); 
			break; 
		case VT_EMPTY:		// 单元格空的  
			str=""; 
			break; 
		} 
		if (vResult.vt == VT_BSTR) 
		{ 
			str=vResult.bstrVal; 
		} 
		//m_comb1.insert(str); 
		str.TrimRight(); 
		str.TrimLeft(); 
		my_Map1.SetAt(str, str); 
		memcpy(tempData2.name, str, str.GetLength()); 
 
		range.AttachDispatch(sheet.GetCells(),true); 
		range.AttachDispatch(range.GetItem(_variant_t((long)(i)),_variant_t((long)(14))).pdispVal ); 
		vResult =range.GetValue(covOptional); 
		switch (vResult.vt) 
		{ 
		case VT_BSTR:		// 字符串 
			str=vResult.bstrVal; 
			break; 
		case VT_R8:			// 8字节的数字 
			str.Format("%.2lf",vResult.dblVal); 
			break; 
		case VT_DATE:		// 时间格式 
			SYSTEMTIME st; 
			VariantTimeToSystemTime(vResult.date, &st); 
			str.Format("%4d/%d/%d", st.wYear, st.wMonth, st.wDay); 
			break; 
		case VT_EMPTY:		// 单元格空的  
			str=""; 
			break; 
		} 
		if (vResult.vt == VT_BSTR) 
		{ 
			str=vResult.bstrVal; 
		} 
		str.TrimRight(); 
		str.TrimLeft(); 
		my_Map2.SetAt(str, str); 
		memcpy(tempData2.type, str, str.GetLength()); 
 
		range.AttachDispatch(sheet.GetCells(),true); 
		range.AttachDispatch(range.GetItem(_variant_t((long)(i)),_variant_t((long)(15))).pdispVal ); 
		vResult =range.GetValue(covOptional); 
		switch (vResult.vt) 
		{ 
		case VT_BSTR:		// 字符串 
			str=vResult.bstrVal; 
			break; 
		case VT_R8:			// 8字节的数字 
			str.Format("%.2lf",vResult.dblVal); 
			break; 
		case VT_DATE:		// 时间格式 
			SYSTEMTIME st; 
			VariantTimeToSystemTime(vResult.date, &st); 
			str.Format("%4d/%d/%d", st.wYear, st.wMonth, st.wDay); 
			break; 
		case VT_EMPTY:		// 单元格空的  
			str=""; 
			break; 
		} 
		if (vResult.vt == VT_BSTR) 
		{ 
			str=vResult.bstrVal; 
		} 
		str.TrimRight(); 
		str.TrimLeft(); 
		my_Map3.SetAt(str, str); 
		memcpy(tempData2.dpName, str, str.GetLength()); 
		if (strcmp(tempData2.id, "") != 0) 
		{ 
			m_data2.Add(tempData2); 
		} 
		 
		 
	} 
// 	m_boolArray1.RemoveAll(); 
// 	for (i = 0; i < g_data1.size(); i++) 
// 		m_boolArray1.Add(FALSE); 
 
	app.Quit();						// 退出 
	//释放对象   
	range.ReleaseDispatch(); 
	sheet.ReleaseDispatch(); 
	sheets.ReleaseDispatch(); 
	book.ReleaseDispatch(); 
	books.ReleaseDispatch(); 
	app.ReleaseDispatch(); 
 
	m_dataCheck.RemoveAll(); 
	Data2 tempData; 
	for (i = 0; i < m_data1.GetSize(); i++) 
	{ 
		CString tempStr = m_data1[i].id; 
		 
		tempStr = tempStr.Right(m_nLen); 
		for (j = 0; j < m_data2.GetSize(); j++) 
		{ 
			CString tempSS = m_data2[j].id; 
			if (tempSS == tempStr) 
			{ 
				memset(&tempData, 0, sizeof(Data2)); 
				tempData = m_data2[j]; 
				tempData.price =  m_data1[i].price; 
				m_dataCheck.Add(tempData); 
				break; 
			} 
		} 
	} 
	OnCheck1(); 
	 
// 	if (m_bCheck == TRUE) 
// 	{ 
// 		m_dataList.Append(m_dataCheck); 
// 	} 
// 	else 
// 		m_dataList.Append(m_data2); 
//  
//  
// 	UpdateList(); 
//  
// 	m_combDpName.ResetContent(); 
// 	m_combName.ResetContent(); 
// 	m_combType.ResetContent(); 
//  
// 	m_combType.AddString("全部"); 
// 	m_combDpName.AddString("全部"); 
// 	m_combName.AddString("全部"); 
// 	CString strKey, strValue; 
// 	POSITION pos = my_Map1.GetStartPosition(); 
// 	while(pos) 
// 	{ 
// 		my_Map1.GetNextAssoc(pos, strKey,strValue); 
// 		m_combName.AddString(strKey); 
// 	} 
//  
// 	pos = my_Map2.GetStartPosition(); 
// 	while(pos) 
// 	{ 
// 		my_Map2.GetNextAssoc(pos, strKey,strValue); 
// 		m_combType.AddString(strKey); 
// 	} 
//  
// 	pos = my_Map3.GetStartPosition(); 
// 	while(pos) 
// 	{ 
// 		my_Map3.GetNextAssoc(pos, strKey,strValue); 
// 		m_combDpName.AddString(strKey); 
// 	} 
	 
} 
 
 
void CExcelChooseDlg::UpdateList() 
{ 
	m_list1.DeleteAllItems(); 
	for(int i = m_dataList.GetSize() - 1; i >= 0; i--) 
	{ 
		CString temp; 
		temp.Format("%d", i+1); 
		int nRow = m_list1.InsertItem(0, temp); 
		m_list1.SetItemText(nRow, 1, m_dataList[i].name); 
		m_list1.SetItemText(nRow, 2, m_dataList[i].type); 
		m_list1.SetItemText(nRow, 3, m_dataList[i].dpName); 
		temp.Format("%g", m_dataList[i].price); 
		m_list1.SetItemText(nRow, 4, temp); 
		temp.Format("%d", m_dataList[i].num); 
		m_list1.SetItemText(nRow, 5, temp); 
	} 
	UpdateData(FALSE); 
 
} 
 
void CExcelChooseDlg::OnCheck1()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(TRUE); 
	int i, j; 
	m_dataList.RemoveAll(); 
	my_Map1.RemoveAll(); 
	my_Map2.RemoveAll(); 
	my_Map3.RemoveAll(); 
	m_dMoney = 0; 
	m_nNumb = 0; 
	 
	if (m_bCheck == TRUE) 
	{ 
		for (j = 0; j < m_dataCheck.GetSize(); j++) 
		{ 
			my_Map1.SetAt(m_dataCheck[j].name, m_dataCheck[j].name); 
			my_Map2.SetAt(m_dataCheck[j].type, m_dataCheck[j].type); 
			my_Map3.SetAt(m_dataCheck[j].dpName, m_dataCheck[j].dpName); 
			m_dMoney += m_dataCheck[j].price; 
			m_nNumb += m_dataCheck[j].num; 
			 
		} 
		m_dataList.Append(m_dataCheck); 
	} 
	else 
	{ 
		for (j = 0; j < m_data2.GetSize(); j++) 
		{ 
			my_Map1.SetAt(m_data2[j].name, m_data2[j].name); 
			my_Map2.SetAt(m_data2[j].type, m_data2[j].type); 
			my_Map3.SetAt(m_data2[j].dpName, m_data2[j].dpName); 
			m_dMoney += m_data2[j].price; 
			m_nNumb += m_data2[j].num; 
 
		} 
		m_dataList.Append(m_data2); 
		 
	} 
 
	m_combDpName.ResetContent(); 
	m_combName.ResetContent(); 
	m_combType.ResetContent(); 
	 
	m_combType.AddString("全部"); 
	m_combDpName.AddString("全部"); 
	m_combName.AddString("全部"); 
	CString strKey, strValue; 
	POSITION pos = my_Map1.GetStartPosition(); 
	while(pos) 
	{ 
		my_Map1.GetNextAssoc(pos, strKey,strValue); 
		m_combName.AddString(strKey); 
	} 
	 
	pos = my_Map2.GetStartPosition(); 
	while(pos) 
	{ 
		my_Map2.GetNextAssoc(pos, strKey,strValue); 
		m_combType.AddString(strKey); 
	} 
	 
	pos = my_Map3.GetStartPosition(); 
	while(pos) 
	{ 
		my_Map3.GetNextAssoc(pos, strKey,strValue); 
		m_combDpName.AddString(strKey); 
	} 
	m_combType.SetCurSel(0); 
	m_combName.SetCurSel(0); 
	m_combDpName.SetCurSel(0); 
	UpdateList(); 
 
} 
 
void CExcelChooseDlg::CheckChange() 
{ 
	int index1 = m_combName.GetCurSel(); 
	int index2 = m_combType.GetCurSel(); 
	int index3 = m_combDpName.GetCurSel(); 
	CString str1, str2, str3; 
	m_combName.GetLBText(index1, str1); 
	m_combType.GetLBText(index2, str2); 
	m_combDpName.GetLBText(index3, str3); 
 
 
	m_dataList.RemoveAll(); 
	int j; 
	m_dMoney = 0; 
	m_nNumb = 0; 
	if (index1 == 0 && index2 == 0 && index3 == 0) 
	{ 
		if (m_bCheck == TRUE) 
		{ 
			for (j = 0; j < m_dataCheck.GetSize(); j++) 
			{ 
				m_dataList.Add(m_dataCheck[j]); 
				m_dMoney += m_dataCheck[j].price; 
				m_nNumb += m_dataCheck[j].num; 
 
			} 
		} 
		else 
		{ 
			for (j = 0; j < m_data2.GetSize(); j++) 
			{ 
				m_dataList.Add(m_data2[j]); 
				m_dMoney += m_data2[j].price; 
				m_nNumb += m_data2[j].num; 
				 
			} 
		} 
	} 
	else if (index1 == 0 && index2 == 0 && index3 > 0) 
	{ 
		if (m_bCheck == TRUE) 
		{ 
			for (j = 0; j < m_dataCheck.GetSize(); j++) 
			{ 
				CString tempS = m_dataCheck[j].dpName; 
				if (tempS == str3) 
				{ 
					m_dataList.Add(m_dataCheck[j]); 
					m_dMoney += m_dataCheck[j].price; 
					m_nNumb += m_dataCheck[j].num; 
				} 
				 
			} 
		} 
		else 
		{ 
			for (j = 0; j < m_data2.GetSize(); j++) 
			{ 
				CString tempS = m_data2[j].dpName; 
				if (tempS == str3) 
				{ 
					m_dataList.Add(m_data2[j]); 
					m_dMoney += m_data2[j].price; 
					m_nNumb += m_data2[j].num; 
				} 
				 
			} 
		} 
	} 
	else if (index1 == 0 && index2 > 0 && index3 == 0) 
	{ 
		if (m_bCheck == TRUE) 
		{ 
			for (j = 0; j < m_dataCheck.GetSize(); j++) 
			{ 
				CString tempS = m_dataCheck[j].type; 
				if (tempS == str2) 
				{ 
					m_dataList.Add(m_dataCheck[j]); 
					m_dMoney += m_dataCheck[j].price; 
					m_nNumb += m_dataCheck[j].num; 
				} 
				 
			} 
		} 
		else 
		{ 
			for (j = 0; j < m_data2.GetSize(); j++) 
			{ 
				CString tempS = m_data2[j].type; 
				if (tempS == str2) 
				{ 
					m_dataList.Add(m_data2[j]); 
					m_dMoney += m_data2[j].price; 
					m_nNumb += m_data2[j].num; 
				} 
				 
			} 
		} 
	} 
	else if (index1 == 0 && index2 > 0 && index3 > 0) 
	{ 
		if (m_bCheck == TRUE) 
		{ 
			for (j = 0; j < m_dataCheck.GetSize(); j++) 
			{ 
				CString tempS = m_dataCheck[j].type; 
				CString tempS1 = m_dataCheck[j].dpName; 
				if (tempS == str2 && tempS1 == str3) 
				{ 
					m_dataList.Add(m_dataCheck[j]); 
					m_dMoney += m_dataCheck[j].price; 
					m_nNumb += m_dataCheck[j].num; 
				} 
				 
			} 
		} 
		else 
		{ 
			for (j = 0; j < m_data2.GetSize(); j++) 
			{ 
				CString tempS = m_data2[j].type; 
				CString tempS1 = m_data2[j].dpName; 
				if (tempS == str2 && tempS1 == str3) 
				{ 
					m_dataList.Add(m_data2[j]); 
					m_dMoney += m_data2[j].price; 
					m_nNumb += m_data2[j].num; 
				} 
				 
			} 
		} 
	} 
	else if (index1 > 0 && index2 == 0 && index3 == 0) 
	{ 
		if (m_bCheck == TRUE) 
		{ 
			for (j = 0; j < m_dataCheck.GetSize(); j++) 
			{ 
				CString tempS = m_dataCheck[j].name; 
				if (tempS == str1) 
				{ 
					m_dataList.Add(m_dataCheck[j]); 
					m_dMoney += m_dataCheck[j].price; 
					m_nNumb += m_dataCheck[j].num; 
				} 
				 
			} 
		} 
		else 
		{ 
			for (j = 0; j < m_data2.GetSize(); j++) 
			{ 
				CString tempS = m_data2[j].name; 
				if (tempS == str1) 
				{ 
					m_dataList.Add(m_data2[j]); 
					m_dMoney += m_data2[j].price; 
					m_nNumb += m_data2[j].num; 
				} 
				 
			} 
		} 
	} 
	else if (index1 > 0 && index2 == 0 && index3 > 0) 
	{ 
		if (m_bCheck == TRUE) 
		{ 
			for (j = 0; j < m_dataCheck.GetSize(); j++) 
			{ 
				CString tempS = m_dataCheck[j].name; 
				CString tempS1 = m_dataCheck[j].dpName; 
				if (tempS == str1 && tempS1 == str3) 
				{ 
					m_dataList.Add(m_dataCheck[j]); 
					m_dMoney += m_dataCheck[j].price; 
					m_nNumb += m_dataCheck[j].num; 
				} 
				 
			} 
		} 
		else 
		{ 
			for (j = 0; j < m_data2.GetSize(); j++) 
			{ 
				CString tempS = m_data2[j].name; 
				CString tempS1 = m_data2[j].dpName; 
				if (tempS == str1 && tempS1 == str3) 
				{ 
					m_dataList.Add(m_data2[j]); 
					m_dMoney += m_data2[j].price; 
					m_nNumb += m_data2[j].num; 
				} 
				 
			} 
		} 
	} 
	else if (index1 > 0 && index2 > 0 && index3 == 0) 
	{ 
		if (m_bCheck == TRUE) 
		{ 
			for (j = 0; j < m_dataCheck.GetSize(); j++) 
			{ 
				CString tempS = m_dataCheck[j].name; 
				CString tempS1 = m_dataCheck[j].type; 
				if (tempS == str1 && tempS1 == str2) 
				{ 
					m_dataList.Add(m_dataCheck[j]); 
					m_dMoney += m_dataCheck[j].price; 
					m_nNumb += m_dataCheck[j].num; 
				} 
				 
			} 
		} 
		else 
		{ 
			for (j = 0; j < m_data2.GetSize(); j++) 
			{ 
				CString tempS = m_data2[j].name; 
				CString tempS1 = m_data2[j].type; 
				if (tempS == str1 && tempS1 == str2) 
				{ 
					m_dataList.Add(m_data2[j]); 
					m_dMoney += m_data2[j].price; 
					m_nNumb += m_data2[j].num; 
				} 
				 
			} 
		} 
	} 
	else if (index1 > 0 && index2 > 0 && index3 > 0) 
	{ 
		if (m_bCheck == TRUE) 
		{ 
			for (j = 0; j < m_dataCheck.GetSize(); j++) 
			{ 
				CString tempS = m_dataCheck[j].name; 
				CString tempS1 = m_dataCheck[j].type; 
				CString tempS2 = m_dataCheck[j].dpName; 
				if (tempS == str1 && tempS1 == str2 && tempS2 == str3) 
				{ 
					m_dataList.Add(m_dataCheck[j]); 
					m_dMoney += m_dataCheck[j].price; 
					m_nNumb += m_dataCheck[j].num; 
				} 
				 
			} 
		} 
		else 
		{ 
			for (j = 0; j < m_data2.GetSize(); j++) 
			{ 
				CString tempS = m_data2[j].name; 
				CString tempS1 = m_data2[j].type; 
				CString tempS2 = m_data2[j].dpName; 
				if (tempS == str1 && tempS1 == str2 && tempS2 == str3) 
				{ 
					m_dataList.Add(m_data2[j]); 
					m_dMoney += m_data2[j].price; 
					m_nNumb += m_data2[j].num; 
				} 
				 
			} 
		} 
	} 
	UpdateList(); 
 
} 
 
void CExcelChooseDlg::OnSelchangeComboDpname()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(TRUE); 
	CheckChange(); 
 
} 
 
void CExcelChooseDlg::OnSelchangeComboName()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(TRUE); 
	CheckChange(); 
} 
 
void CExcelChooseDlg::OnSelchangeComboType()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(TRUE); 
	CheckChange(); 
} 
 
 
void CExcelChooseDlg::OnButtonEmport()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(TRUE); 
// 	if (m_strName == "") 
// 	{ 
// 		MessageBox("请先输入主管姓名!"); 
// 		return; 
// 	} 
	excelnamespace::_Application app; 
	excelnamespace::Workbooks books; 
	excelnamespace::_Workbook book; 
	excelnamespace::Worksheets sheets; 
	excelnamespace::_Worksheet sheet; 
	excelnamespace::Range range; 
	LPDISPATCH lpDisp;     
	COleVariant vResult; 
 
	CString str = ""; 
	 
	COleVariant 
		covTrue((short)TRUE), 
		covFalse((short)FALSE), 
		covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); 
	 
	//创建Excel 2003服务器(启动Excel) 
	if (!app.CreateDispatch("Excel.Application",NULL))  
	{  
		AfxMessageBox("Create Excel service failure!"); 
		return; 
	} 
 
	// 设置为FALSE时,后面的app.Quit();注释要打开 
	// 否则EXCEL.EXE进程会一直存在,并且每操作一次就会多开一个进程 
	app.SetVisible(FALSE); 
	app.SetDisplayAlerts(FALSE); 
	/////////////////////////////////////////////// 
	 
	books.AttachDispatch(app.GetWorkbooks(),true); 
 
	book = books.Add(covOptional); 
	 
	// 得到Worksheets   
	sheets.AttachDispatch(book.GetWorksheets(),true);  
	 
	// 得到Worksheet 
	sheet.AttachDispatch(sheets.GetItem(_variant_t((short)(1)))); 	 
	 
 
	// 设置单元格的列宽为12 
	range.AttachDispatch(sheet.GetRange(_variant_t("A1"), _variant_t("A1")),true); 
	range.SetColumnWidth(_variant_t((long)40)); 
	 
	range.AttachDispatch(sheet.GetRange(_variant_t("B1"), _variant_t("B1")),true); 
	range.SetColumnWidth(_variant_t((long)10)); 
 
	range.AttachDispatch(sheet.GetRange(_variant_t("C1"), _variant_t("C1")),true); 
	range.SetColumnWidth(_variant_t((long)10)); 
	// 所有单元格居中显示 
	range.AttachDispatch(sheet.GetCells(),true); 
	range.SetNumberFormat(_variant_t("@")); //将单元格设置为文本类型    
	range.SetItem(_variant_t((LONG)1),_variant_t((LONG)1),_variant_t("商品名称")); 
	range.SetItem(_variant_t((LONG)1),_variant_t((LONG)2),_variant_t("金额")); 
	range.SetItem(_variant_t((LONG)1),_variant_t((LONG)3),_variant_t("数量")); 
 
 
	int column = 2; 
	int i; 
	CString temp; 
	for (i = 0; i < m_dataList.GetSize(); i++) 
	{ 
		temp.Format("%s", m_dataList[i].name); 
		range.SetItem(_variant_t((LONG)column),_variant_t((LONG)1),_variant_t(temp)); 
		temp.Format("%g", m_dataList[i].price); 
		range.SetItem(_variant_t((LONG)column),_variant_t((LONG)2),_variant_t(temp)); 
		temp.Format("%d", m_dataList[i].num); 
		range.SetItem(_variant_t((LONG)column),_variant_t((LONG)3),_variant_t(temp)); 
		column++; 
 
	} 
	 
	range.SetItem(_variant_t((LONG)column),_variant_t((LONG)1),_variant_t("总计:")); 
	temp.Format("%g", m_dMoney); 
	range.SetItem(_variant_t((LONG)column),_variant_t((LONG)2),_variant_t(temp)); 
	temp.Format("%d", m_nNumb); 
	range.SetItem(_variant_t((LONG)column),_variant_t((LONG)3),_variant_t(temp)); 
	column++; 
 
	CString TempPath = GetPath(); 
	TempPath += "new.xls"; 
 
//	book.Save();					// 保存Excel的内容 
//	app.SetDisplayAlerts(false);	// 不弹出对话框询问是否保存 
	book.SaveAs(COleVariant(TempPath),covOptional,covOptional, 
		covOptional,covOptional,covOptional,0, 
                                covOptional,covOptional,covOptional,covOptional,covOptional); 
	app.Quit();						// 退出 
 
	//释放对象   
	range.ReleaseDispatch(); 
	sheet.ReleaseDispatch(); 
	sheets.ReleaseDispatch(); 
	book.ReleaseDispatch(); 
	books.ReleaseDispatch(); 
	app.ReleaseDispatch(); 
	MessageBox("导出成功!"); 
}