// DialCustomSptInput.cpp : implementation file // #include "stdafx.h" #include "geomative.h" #include "DialCustomSptInput.h" #include "CustomSptQueryDial.h" #include "excel.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif using namespace excel9; ///////////////////////////////////////////////////////////////////////////// // CDialCustomSptInput dialog extern HHOOK hHook; extern LRESULT __stdcall CBTHookProc(long nCode, WPARAM wParam, LPARAM lParam); extern bool bIsPostiveInteger(CString strTxt); extern bool bIsFloat(CString strTxt); extern int g_iUILanguage; //extern CString CovertVarToCString(const VARIANT& vtVal); CDialCustomSptInput::CDialCustomSptInput(CWnd* pParent /*=NULL*/) : CDialog(CDialCustomSptInput::IDD, pParent) { //{{AFX_DATA_INIT(CDialCustomSptInput) m_strA = _T(""); m_strB = _T(""); m_strStacking = _T("1"); m_strK = _T(""); m_strLayer = _T(""); m_strM = _T(""); m_strN = _T(""); //}}AFX_DATA_INIT m_iA = 0; m_iB = 0; m_iM = 0; m_iN = 0; m_iLayer = 0; m_iIteration = 1; m_fK = 0; m_iTsn = 0; // pComApp = NULL; } void CDialCustomSptInput::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDialCustomSptInput) DDX_Control(pDX, IDC_LIST_CUSTOM_SPT, m_listCustomSpt); DDX_Text(pDX, IDC_EDIT_CUSTOMSPT_A, m_strA); DDX_Text(pDX, IDC_EDIT_CUSTOMSPT_B, m_strB); DDX_Text(pDX, IDC_EDIT_CUSTOMSPT_ITERATION, m_strStacking); DDX_Text(pDX, IDC_EDIT_CUSTOMSPT_K, m_strK); DDX_Text(pDX, IDC_EDIT_CUSTOMSPT_LAYER, m_strLayer); DDX_Text(pDX, IDC_EDIT_CUSTOMSPT_M, m_strM); DDX_Text(pDX, IDC_EDIT_CUSTOMSPT_N, m_strN); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDialCustomSptInput, CDialog) //{{AFX_MSG_MAP(CDialCustomSptInput) ON_BN_CLICKED(IDC_BUTTON_ADD_LIST, OnButtonAddList) ON_BN_CLICKED(IDC_BUTTON_DEL_LIST, OnButtonDelList) ON_BN_CLICKED(IDC_BUTTON_MODIFY_LIST, OnButtonModifyList) ON_NOTIFY(NM_CLICK, IDC_LIST_CUSTOM_SPT, OnClickListCustomSpt) ON_BN_CLICKED(IDC_RADIO_HAND_INPUT, OnRadioHandInput) ON_BN_CLICKED(IDC_RADIO_AUTO_CALC, OnRadioAutoCalc) ON_BN_CLICKED(IDC_BUTTON_IMPORT, OnButtonImport) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDialCustomSptInput message handlers void CDialCustomSptInput::OnButtonAddList() { // TODO: Add your control notification handler code here if (!CheckInputIsValid()) { return; } if (0 == m_iA || 0 == m_iB || 0 == m_iM || 0 == m_iN) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("A,B,M,N的值不能为零!")); else MessageBoxEx(NULL, _T("The value of A,B,M,N cannot be zero!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return; } if (m_iLayer < 1) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("层数值错误!")); else MessageBoxEx(NULL, _T("Layer's value error!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return; } if (!CalculateK()) { return; } m_iTsn++; CString szLabel = _T(""); int iRecIndex = m_listCustomSpt.GetItemCount(); m_listCustomSpt.InsertItem(iRecIndex,_T("")); szLabel.Empty(); szLabel.Format(_T("%d"), m_iTsn); m_listCustomSpt.SetItemText(iRecIndex, 0, szLabel); szLabel.Empty(); szLabel.Format(_T("%d"), m_iA); m_listCustomSpt.SetItemText(iRecIndex, 1, szLabel); szLabel.Empty(); szLabel.Format(_T("%d"), m_iB); m_listCustomSpt.SetItemText(iRecIndex, 2, szLabel); szLabel.Empty(); szLabel.Format(_T("%d"), m_iM); m_listCustomSpt.SetItemText(iRecIndex, 3, szLabel); szLabel.Empty(); szLabel.Format(_T("%d"), m_iN); m_listCustomSpt.SetItemText(iRecIndex, 4, szLabel); szLabel.Empty(); szLabel.Format(_T("%f"), m_fK); m_listCustomSpt.SetItemText(iRecIndex, 5, szLabel); szLabel.Empty(); szLabel.Format(_T("%d"), m_iIteration); m_listCustomSpt.SetItemText(iRecIndex, 6, szLabel); szLabel.Empty(); szLabel.Format(_T("%d"), m_iLayer); m_listCustomSpt.SetItemText(iRecIndex, 7, szLabel); } void CDialCustomSptInput::OnButtonDelList() { // TODO: Add your control notification handler code here int iIndex = 0; POSITION pos = m_listCustomSpt.GetFirstSelectedItemPosition(); if (pos == NULL) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("请先选择需要删除的数据!")); else MessageBoxEx(NULL, _T("Please chose the data which you want to delete!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return ; } while (pos) { iIndex = m_listCustomSpt.GetNextSelectedItem(pos); m_listCustomSpt.DeleteItem(iIndex); pos = m_listCustomSpt.GetFirstSelectedItemPosition(); } } void CDialCustomSptInput::OnButtonModifyList() { // TODO: Add your control notification handler code here int nCnt = 0, nItem = 0; POSITION pos = m_listCustomSpt.GetFirstSelectedItemPosition(); if (NULL != pos) { while (pos) { nCnt++; nItem = m_listCustomSpt.GetNextSelectedItem(pos); } } if (0 == nCnt) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("请先选择数据!")); else MessageBoxEx(NULL, _T("Please chose data firstly!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return; } if (1 != nCnt) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("请选择一行数据!")); else MessageBoxEx(NULL, _T("Please chose one item data!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return; } if (!CheckInputIsValid()) { return; } if (!CalculateK()) { return; } CString szLabel; szLabel.Empty(); szLabel.Format(_T("%d"), m_iA); m_listCustomSpt.SetItemText(nItem, 1, szLabel); szLabel.Empty(); szLabel.Format(_T("%d"), m_iB); m_listCustomSpt.SetItemText(nItem, 2, szLabel); szLabel.Empty(); szLabel.Format(_T("%d"), m_iM); m_listCustomSpt.SetItemText(nItem, 3, szLabel); szLabel.Empty(); szLabel.Format(_T("%d"), m_iN); m_listCustomSpt.SetItemText(nItem, 4, szLabel); szLabel.Empty(); szLabel.Format(_T("%f"), m_fK); m_listCustomSpt.SetItemText(nItem, 5, szLabel); szLabel.Empty(); szLabel.Format(_T("%d"), m_iIteration); m_listCustomSpt.SetItemText(nItem, 6, szLabel); szLabel.Empty(); szLabel.Format(_T("%d"), m_iLayer); m_listCustomSpt.SetItemText(nItem, 7, szLabel); } void CDialCustomSptInput::OnOK() { // TODO: Add extra validation here if (0 == m_listCustomSpt.GetItemCount() ) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("脚本数据不能为空!")); else MessageBoxEx(NULL, _T("Script Data can't be zero!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return; } CDialog::OnOK(); } BOOL CDialCustomSptInput::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here InitalListHeaderInfo(); if (LANG_ZHCN == g_iUILanguage) { SetWindowText(_T("自定义脚本")); GetDlgItem(IDC_RADIO_AUTO_CALC)->SetWindowText(_T("自动")); GetDlgItem(IDC_RADIO_HAND_INPUT)->SetWindowText(_T("手动")); GetDlgItem(IDC_BUTTON_ADD_LIST)->SetWindowText(_T("增加")); GetDlgItem(IDC_BUTTON_MODIFY_LIST)->SetWindowText(_T("修改")); GetDlgItem(IDC_BUTTON_DEL_LIST)->SetWindowText(_T("删除")); GetDlgItem(IDC_BUTTON_IMPORT)->SetWindowText(_T("导入")); GetDlgItem(IDOK)->SetWindowText(_T("完成")); GetDlgItem(IDCANCEL)->SetWindowText(_T("取消")); } ((CButton *)GetDlgItem(IDC_RADIO_AUTO_CALC))->SetCheck(TRUE); ((CButton *)GetDlgItem(IDC_RADIO_HAND_INPUT))->SetCheck(FALSE); GetDlgItem(IDC_EDIT_CUSTOMSPT_K)->EnableWindow(FALSE); ((CEdit*)GetDlgItem(IDC_EDIT_CUSTOMSPT_A))->SetLimitText(7); ((CEdit*)GetDlgItem(IDC_EDIT_CUSTOMSPT_B))->SetLimitText(7); ((CEdit*)GetDlgItem(IDC_EDIT_CUSTOMSPT_M))->SetLimitText(7); ((CEdit*)GetDlgItem(IDC_EDIT_CUSTOMSPT_N))->SetLimitText(7); ((CEdit*)GetDlgItem(IDC_EDIT_CUSTOMSPT_ITERATION))->SetLimitText(7); ((CEdit*)GetDlgItem(IDC_EDIT_CUSTOMSPT_LAYER))->SetLimitText(7); ((CEdit*)GetDlgItem(IDC_EDIT_CUSTOMSPT_K))->SetLimitText(18); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CDialCustomSptInput::OnClickListCustomSpt(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here int nCnt = 0, nItem = 0; POSITION pos = m_listCustomSpt.GetFirstSelectedItemPosition(); if (NULL != pos) { while (pos) { nCnt++; nItem = m_listCustomSpt.GetNextSelectedItem(pos); } } if (0 == nCnt) { return; } if (1 != nCnt) { return; } CString strVal = _T(""); strVal.Empty(); strVal = m_listCustomSpt.GetItemText(nItem, 1); m_strA = strVal; m_iA = atoi(strVal); strVal.Empty(); strVal = m_listCustomSpt.GetItemText(nItem, 2); m_strB = strVal; m_iB = atoi(strVal); strVal.Empty(); strVal = m_listCustomSpt.GetItemText(nItem, 3); m_strM = strVal; m_iM = atoi(strVal); strVal.Empty(); strVal = m_listCustomSpt.GetItemText(nItem, 4); m_strN = strVal; m_iN = atoi(strVal); strVal.Empty(); strVal = m_listCustomSpt.GetItemText(nItem, 5); m_strK = strVal; m_fK = atof(strVal); strVal.Empty(); strVal = m_listCustomSpt.GetItemText(nItem, 6); m_strStacking = strVal; m_iIteration = atoi(strVal); strVal.Empty(); strVal = m_listCustomSpt.GetItemText(nItem, 7); m_strLayer = strVal; m_iLayer = atoi(strVal); UpdateData(FALSE); *pResult = 0; } void CDialCustomSptInput::InitalListHeaderInfo() { CString strColTitle=_T(""); int iColIndex = (int)VAL_ZERO; m_listCustomSpt.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP | LVS_EX_ONECLICKACTIVATE | LVS_EX_GRIDLINES|LVS_EX_MULTIWORKAREAS); iColIndex++; strColTitle.Empty(); strColTitle.LoadString(IDS_DB_SCRIPT_TSN); m_listCustomSpt.InsertColumn(iColIndex, strColTitle, LVCFMT_CENTER, 100); iColIndex++; strColTitle.Empty(); strColTitle.LoadString(IDS_DB_SCRIPT_C1); m_listCustomSpt.InsertColumn(iColIndex, strColTitle, LVCFMT_CENTER, 50); iColIndex++; strColTitle.Empty(); strColTitle.LoadString(IDS_DB_SCRIPT_C2); m_listCustomSpt.InsertColumn(iColIndex, strColTitle, LVCFMT_CENTER, 50); iColIndex++; strColTitle.Empty(); strColTitle.LoadString(IDS_DB_SCRIPT_P1); m_listCustomSpt.InsertColumn(iColIndex, strColTitle, LVCFMT_CENTER, 50); iColIndex++; strColTitle.Empty(); strColTitle.LoadString(IDS_DB_SCRIPT_P2); m_listCustomSpt.InsertColumn(iColIndex, strColTitle, LVCFMT_CENTER, 50); iColIndex++; strColTitle.Empty(); strColTitle.LoadString(IDS_DB_SCRIPT_K); m_listCustomSpt.InsertColumn(iColIndex, strColTitle, LVCFMT_CENTER, 100); iColIndex++; strColTitle.Empty(); strColTitle.LoadString(IDS_DB_SCRIPT_N); m_listCustomSpt.InsertColumn(iColIndex, strColTitle, LVCFMT_CENTER, 50); iColIndex++; strColTitle.Empty(); strColTitle.LoadString(IDS_DB_SCRIPT_LEVEL); m_listCustomSpt.InsertColumn(iColIndex, strColTitle, LVCFMT_CENTER, 50); } void CDialCustomSptInput::OnRadioHandInput() { // TODO: Add your control notification handler code here if (BST_CHECKED == ((CButton *)GetDlgItem(IDC_RADIO_HAND_INPUT))->GetCheck()) { ((CButton *)GetDlgItem(IDC_RADIO_AUTO_CALC))->SetCheck(FALSE); GetDlgItem(IDC_EDIT_CUSTOMSPT_K)->EnableWindow(TRUE); } } void CDialCustomSptInput::OnRadioAutoCalc() { // TODO: Add your control notification handler code here if (BST_CHECKED == ((CButton *)GetDlgItem(IDC_RADIO_AUTO_CALC))->GetCheck()) { ((CButton *)GetDlgItem(IDC_RADIO_HAND_INPUT))->SetCheck(FALSE); GetDlgItem(IDC_EDIT_CUSTOMSPT_K)->SetWindowText(_T("")); GetDlgItem(IDC_EDIT_CUSTOMSPT_K)->EnableWindow(FALSE); } } bool CDialCustomSptInput::CheckInputIsValid() { if (!UpdateData(TRUE)) { MessageBoxEx(NULL, _T("Retrieve data failed."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return false; } //A if ((-1 != atoi(m_strA)) && !bIsPostiveInteger(m_strA)) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("A的值须为正数或者-1!")); else MessageBoxEx(NULL, _T("A has to be positive or minus 1!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return false; } m_iA = atoi(m_strA); //B 无穷大只会是-1 if ((-1 != atoi(m_strB)) && !bIsPostiveInteger(m_strB)) { m_iB = atoi(m_strB); if (m_iB != -1) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("B的值须为正数或者-1!")); else MessageBoxEx(NULL, _T("B has to be positive or minus 1!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return false; } } m_iB = atoi(m_strB); //M if ((-1 != atoi(m_strM)) && !bIsPostiveInteger(m_strM)) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("M的值须为正数或者-1!")); else MessageBoxEx(NULL, _T("M has to be positive or minus 1!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return false; } m_iM = atoi(m_strM); //N if ((-1 != atoi(m_strN)) && !bIsPostiveInteger(m_strN)) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("N的值须为正数或者-1!")); else MessageBoxEx(NULL, _T("N has to be positive or minus 1!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return false; } m_iN = atoi(m_strN); //Layer if (!bIsPostiveInteger(m_strLayer)) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("层数的值须为正数!")); else MessageBoxEx(NULL, _T("Please input a positive number on Layer."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return false; } m_iLayer = atoi(m_strLayer); //Stacking if (!bIsPostiveInteger(m_strStacking)) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("迭代数的值须为正数!")); else MessageBoxEx(NULL, _T("Please input a positive number on Stacking."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return false; } m_iIteration = atoi(m_strStacking); if (m_iIteration < 1 || m_iIteration > 255) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("迭代数错误,请输入一个从1到255的整数!")); else MessageBoxEx(NULL, _T("Stacking number input error!please input an integer from 1 to 255!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return false; } //K if (BST_CHECKED == ((CButton *)GetDlgItem(IDC_RADIO_HAND_INPUT))->GetCheck()) { if (!bIsFloat(m_strK)) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("K值输入错误!")); else MessageBoxEx(NULL, _T("Please input number on K.!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return false; } m_fK = atof(m_strK);//m_fK = fabs(atof(m_strK));//by quyx 20180514 K值可为负 } return true; } bool CDialCustomSptInput::CalculateK() { //如果设置成自动计算则此时需要进行计算 if (BST_CHECKED == ((CButton *)GetDlgItem(IDC_RADIO_AUTO_CALC))->GetCheck()) { int iAM = fabs(float(m_iA - m_iM)); int iAN = fabs(float(m_iA - m_iN)); int iBM = fabs(float(m_iB - m_iM)); int iBN = fabs(float(m_iB - m_iN)); //AM装置,存在BN等于0的情况 if (0 == iAM || 0 == iAN || 0 == iBM /*|| 0 == iBN*/) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("任意2个电极之间的距离都不能为零!")); else MessageBoxEx(NULL, _T("The position of any two electrodes can not be equal!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return false; } float fAMCal = 1.0/iAM; float fANCal = 1.0/iAN; float fBMCal = 1.0/iBM; float fBNCal = 1.0/iBN; if (m_iA < 0) { fAMCal = 0; fANCal = 0; } if (m_iB < 0) { fBMCal = 0; fBNCal = 0; } if (m_iM < 0) { fAMCal = 0; fBMCal = 0; } if (m_iN < 0) { fANCal = 0; fBNCal = 0; } //float fTmp = fabs(fAMCal - fANCal - fBMCal + fBNCal); float fTmp = (fAMCal - fANCal - fBMCal + fBNCal);//by quyx 20180514 K值可为负 if (0 == fTmp) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("自动计算K值错误,请检查输入!")); else MessageBoxEx(NULL, _T("Automatic calculate K error,please check your input value!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return false; } m_fK = 1;//2*VAL_PI/fTmp; } /*if (m_fK <= 0) //by quyx 20180514 K值可为负 { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("K值计算错误!")); else AfxMessageBox(_T("The value of K error!")); return false; }*/ return true; } CString CDialCustomSptInput::GetListItemText(int iIndex, int iSubIndex) { CString strRes = _T(""); if (iIndex < 0 || iSubIndex < 0) { MessageBoxEx(NULL, _T("Input parameter error in GetListItemText!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return strRes; } strRes = m_listCustomSpt.GetItemText(iIndex, iSubIndex); return strRes; } void CDialCustomSptInput::OnButtonImport() { // TODO: Add your control notification handler code here CString strCaption = _T(""); CString strFileName = _T(""); CString strText = _T(""); strCaption.LoadString(IDS_OPEN); CFileDialog *pDlgOpenFile = NULL; pDlgOpenFile = new CFileDialog (TRUE , NULL, NULL, OFN_HIDEREADONLY|OFN_FILEMUSTEXIST|OFN_NOCHANGEDIR, _T("Excel file(*.xlsx)|*.xlsx||"),this); pDlgOpenFile->m_ofn.lpstrTitle = strCaption; if (IDOK != pDlgOpenFile->DoModal()) { return; } strFileName = pDlgOpenFile->GetPathName(); delete pDlgOpenFile; _Application ComApp; Workbooks comBooks; _Workbook comBook; Sheets comSheets; _Worksheet comSheet01; Range objRange; VARIANT vRet; COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); if( !ComApp.CreateDispatch(_T("Excel.Application"), NULL) ) { hHook = SetWindowsHookEx(WH_CBT,(HOOKPROC)CBTHookProc,AfxGetInstanceHandle(),NULL); MessageBoxEx(NULL, _T("Can't build excel application!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); ComApp.Quit(); return ; } comBooks = ComApp.GetWorkbooks(); comBook = comBooks.Open(strFileName, VOptional, VOptional, VOptional, VOptional, VOptional, VOptional, VOptional, VOptional, VOptional, VOptional, VOptional, VOptional, VOptional, VOptional); comSheets = comBook.GetWorksheets(); comSheet01 = comSheets.GetItem(COleVariant((short)1)); objRange = comSheet01.GetUsedRange(); // 取行列数 vRet = objRange.GetValue(VOptional); COleSafeArray oleSA(vRet); long lNumRows; long lNumCols = 7; oleSA.GetUBound(1, &lNumRows); if (lNumRows <= 1) { if (LANG_ZHCN == g_iUILanguage) { strText = (_T("在sheet1处没有数据!")); AfxMessageBox(strText); } else { strText.Format(_T("No data in sheet1(%s)"), strFileName); MessageBoxEx(NULL, strText, STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); } objRange.Clear(); comBook.Close(COleVariant((short)FALSE),VOptional,VOptional); comBooks.Close(); ComApp.Quit(); ComApp.ReleaseDispatch(); } int iRecIndex = 0; int iTsn = 0; if (m_listCustomSpt.GetItemCount() != 0) { CCustomSptQueryDial CustQueDial; CustQueDial.DoModal(); int nOperRes = CustQueDial.GetOperResult(); if (EN_QUE_APPEND == nOperRes) { iRecIndex = m_listCustomSpt.GetItemCount(); iTsn = atoi(m_listCustomSpt.GetItemText(iRecIndex-1, 0)); } else if (EN_QUE_REPLACE == nOperRes) { m_listCustomSpt.DeleteAllItems(); } else { objRange.Clear(); comBook.Close(COleVariant((short)FALSE),VOptional,VOptional); comBooks.Close(); ComApp.Quit(); ComApp.ReleaseDispatch(); return; } } //在这里固定取前7列 lNumCols = 7; TCHAR tCh = _T('A'); CString strTitle = _T(""); //行数从1开始,并且第一行是数据的标题 for (int i = 2; i <= lNumRows; i++) { m_listCustomSpt.InsertItem(iRecIndex+i-2,_T("")); iTsn++; strText.Empty(); strText.Format(_T("%d"),iTsn); m_listCustomSpt.SetItemText(iRecIndex+i-2, 0, strText); for (int j = 0; j < lNumCols; j++) { strTitle.Empty(); strTitle.Format(_T("%c%d"),tCh+j, i); strText.Empty(); Range rangeVal=comSheet01.GetRange(COleVariant(strTitle),COleVariant(strTitle)); COleVariant rValue; rValue=COleVariant(rangeVal.GetValue2()); rValue.ChangeType(VT_BSTR); strText = CString(rValue.bstrVal); if (strText.IsEmpty()) { if (LANG_ZHCN == g_iUILanguage) { strText.Format(_T("从Excel获取 %s 数据失败!"), strTitle); AfxMessageBox(strText); } else { strText.Format(_T("Get %s data in excel failed!"), strTitle); MessageBoxEx(NULL, strText, STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); } objRange.Clear(); comBook.Close(COleVariant((short)FALSE),VOptional,VOptional); comBooks.Close(); ComApp.Quit(); ComApp.ReleaseDispatch(); return; } m_listCustomSpt.SetItemText(iRecIndex+i-2, j+1, strText); } } objRange.Clear(); comBook.Close(COleVariant((short)FALSE),VOptional,VOptional); comBooks.Close(); ComApp.Quit(); ComApp.ReleaseDispatch(); if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("导入文件成功!")); else MessageBoxEx(NULL, _T("Import file success."), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return; }